gotcp

package module
v0.0.0-...-cbe29f7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2015 License: MIT Imports: 13 Imported by: 0

README

gotcp

A Go package for quickly building tcp servers

Usage

###Install

go get github.com/gansidui/gotcp

###Examples

Document

Doc

Documentation

Overview

通讯协议处理,主要处理封包和解包的过程

Index

Constants

View Source
const (
	ConstHeader                  = "SeanCommu"
	ConstHeaderLength      int32 = 9
	ConstSaveDataLength    int32 = 4
	ConstEndCheckECCLength int32 = 4

	TempToken = "e675d1b7e23c8a9ca3a3479d6cf29232"
)
View Source
const (
	ConstPackNoHeader  = 0
	ConstPackHasHeader = 1
)

Variables

View Source
var (
	ErrConnClosing   = errors.New("use of closed network connection")
	ErrWriteBlocking = errors.New("write packet was blocking")
	ErrReadBlocking  = errors.New("read packet was blocking")
)

Error type

Functions

func BytesToInt

func BytesToInt(b []byte) int32

字节转换成整形

func BytesTouInt32

func BytesTouInt32(b []byte) uint32

字节转换成整形

func CheckECC

func CheckECC(data []byte) uint32

func DoPacket

func DoPacket(message []byte) []byte

封包

func GetUniq

func GetUniq() int

func IntToBytes

func IntToBytes(n int32) []byte

整形转换成字节

func UInt32ToBytes

func UInt32ToBytes(n uint32) []byte

整形转换成字节

func Unpack

func Unpack(buffer []byte, readerChannel chan Packet) (leftbuffer []byte)

解包

Types

type Config

type Config struct {
	PacketSendChanLimit    uint32 // the limit of packet send channel
	PacketReceiveChanLimit uint32 // the limit of packet receive channel
	ReadTimeOut            uint32
	WriteTimeOut           uint32
}

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Conn exposes a set of callbacks for the various events that occur on a connection

func (*Conn) AsyncReadPacket

func (c *Conn) AsyncReadPacket(timeout time.Duration) (Packet, error)

AsyncReadPacket async reads a packet, this method will never block

func (*Conn) AsyncWritePacket

func (c *Conn) AsyncWritePacket(p Packet, timeout time.Duration) error

AsyncWritePacket async writes a packet, this method will never block

func (*Conn) Close

func (c *Conn) Close()

Close closes the connection

func (*Conn) Do

func (c *Conn) Do()

Do it

func (*Conn) GetExtraData

func (c *Conn) GetExtraData() string

GetExtraData gets the extra data from the Conn

func (*Conn) GetRawConn

func (c *Conn) GetRawConn() *net.TCPConn

GetRawConn returns the raw net.TCPConn from the Conn

func (*Conn) IsClosed

func (c *Conn) IsClosed() bool

IsClosed indicates whether or not the connection is closed

func (*Conn) PutExtraData

func (c *Conn) PutExtraData(data string)

PutExtraData puts the extra data with the Conn

type ConnCallback

type ConnCallback interface {
	// OnConnect is called when the connection was accepted,
	// If the return value of false is closed
	OnConnect(*Conn) bool

	// OnMessage is called when the connection receives a packet,
	// If the return value of false is closed
	OnMessage(*Conn, Packet) bool

	// OnClose is called when the connection closed
	OnClose(*Conn)
}

ConnCallback is an interface of methods that are used as callbacks on a connection

type ConnManager

type ConnManager struct {
	// contains filtered or unexported fields
}

func NewConnManager

func NewConnManager() *ConnManager

func (*ConnManager) AddConn

func (m *ConnManager) AddConn(id string, conn *Conn)

func (*ConnManager) Broadcast

func (m *ConnManager) Broadcast(packet Packet)

func (*ConnManager) DelConn

func (m *ConnManager) DelConn(id string)

type Packet

type Packet interface {
	Serialize() []byte
}

type Protocol

type Protocol interface {
	ReadPacket(conn *net.TCPConn) (Packet, error)
}

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(config *Config, callback ConnCallback, protocol Protocol) *Server

NewServer creates a server

func (*Server) Start

func (s *Server) Start(listener *net.TCPListener, acceptTimeout time.Duration)

Start starts service

func (*Server) Stop

func (s *Server) Stop()

Stop stops service

type StreamPacket

type StreamPacket struct {
	// contains filtered or unexported fields
}

func NewStreamPacket

func NewStreamPacket(buff []byte) *StreamPacket

func (*StreamPacket) GetBody

func (this *StreamPacket) GetBody() []byte

func (*StreamPacket) GetLength

func (this *StreamPacket) GetLength() uint32

func (*StreamPacket) Serialize

func (this *StreamPacket) Serialize() []byte

Directories

Path Synopsis
examples
echo/server/comm
通讯协议处理,主要处理封包和解包的过程
通讯协议处理,主要处理封包和解包的过程

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL