Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoMemory is passed to panic if memory cannot be allocated to store data in a buffer. ErrNoMemory = errors.New("xtcp.buffer: no memory") // ErrSpaceLimit means that the required space beyond the upper limit. ErrSpaceLimit = errors.New("xtcp.buffer: required space beyond the upper limit") // ErrNegativeCount means that negative count. ErrNegativeCount = errors.New("xtcp.buffer: negative count") )
var ( // DefaultSendListLen is the default length of send list. DefaultSendListLen = 16 // channel size // DefaultRecvBufInitSize is the default init size of recv buf. DefaultRecvBufInitSize = 1 << 10 // 1k // DefaultRecvBufMaxSize is the default max size of recv buf. DefaultRecvBufMaxSize = 4 << 10 // 4k // DefaultSendBufInitSize is the default init size of send buf. DefaultSendBufInitSize = 1 << 10 // 1k // DefaultSendBufMaxSize is the default max size of send buf. DefaultSendBufMaxSize = 2 << 10 // 4k )
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
A Buffer is a variable-sized buffer of bytes.
func NewBuffer ¶
NewBuffer create a new Buffer. If initSize == 0 or initSize > maxSize it will return nil. If create buffer failed it will panic with ErrNoMemory.
func (*Buffer) Advance ¶
Advance discard the n bytes from the last read. If n is negative return errNegativeCount. If the buffer can't advance it will return ErrSpaceLimit.
func (*Buffer) Cap ¶
Cap returns the capacity of the buffer's underlying byte slice, that is, the total space allocated for the buffer's data.
func (*Buffer) Grow ¶
Grow grows the buffer's capacity until to max size. After Grow(n), at least n bytes can be written to the buffer without another allocation. return ErrNegativeCount if n is negative. return ErrSpaceLimit if need space size grater than the max size. If the buffer can't alloc memory it will panic with ErrNoMemory.
func (*Buffer) UnreadBytes ¶
UnreadBytes returns a slice of length b.Len() holding the unread portion of the buffer. The slice is valid for use only until the next buffer modification.
type Conn ¶
type Conn struct { Opts *Options RawConn net.Conn UserData interface{} // contains filtered or unexported fields }
A Conn represents the server side of an tcp connection.
func (*Conn) DialAndServe ¶
DialAndServe connects to the addr and serve.
type Handler ¶
Handler is the event callback. p will be nil when event is EventAccept/EventConnected/EventClosed
type Options ¶
type Options struct { Handler Handler Protocol Protocol SendListLen int // default is DefaultSendListLen if you don't set. RecvBufInitSize int // default is DefaultRecvBufInitSize if you don't set. RecvBufMaxSize int // default is DefaultRecvBufMaxSize if you don't set. SendBufInitSize int // default is DefaultSendBufInitSize if you don't set. SendBufMaxSize int // default is DefaultSendBufMaxSize if you don't set. }
Options is the options used for net conn.
func NewOpts ¶
NewOpts create a new options and set some default value. will panic if handler or protocol is nil. eg: opts := NewOpts().SetSendListLen(len).SetRecvBufInitSize(len)...
func (*Options) SetRecvBufInitSize ¶
SetRecvBufInitSize set init size of the recv buf, 0 mean DefaultRecvBufInitSize.
func (*Options) SetRecvBufMaxSize ¶
SetRecvBufMaxSize set max size of the recv buf, 0 mean DefaultRecvBufMaxSize.
func (*Options) SetSendBufInitSize ¶
SetSendBufInitSize set init size of the recv buf, 0 mean DefaultSendBufInitSize.
func (*Options) SetSendBufMaxSize ¶
SetSendBufMaxSize set max size of the recv buf, 0 mean DefaultSendBufMaxSize.
func (*Options) SetSendListLen ¶
SetSendListLen set init size of the recv buf, 0 mean DefaultSendListLen.
type Protocol ¶
type Protocol interface { // return the size need for pack the Packet. PackSize(p Packet) int // PackTo pack the Packet to w. // The return value n is the number of bytes written; // Any error encountered during the write is also returned. PackTo(p Packet, w io.Writer) (int, error) // Pack pack the Packet to new created buf. Pack(p Packet) ([]byte, error) // try to unpack the buf to Packet. If return len > 0, then buf[:len] will be discard. // The following return conditions must be implement: // (nil, 0, nil) : buf size not enough for unpack one Packet. // (nil, len, err) : buf size enough but error encountered. // (p, len, nil) : unpack succeed. Unpack(buf []byte) (Packet, int, error) }
Protocol use to pack/unpack Packet.
type Server ¶
type Server struct { Opts *Options // contains filtered or unexported fields }
Server used for running a tcp server.
func NewServer ¶
NewServer create a tcp server but not start to accept. The opts will set to all accept conns.
func (*Server) ListenAndServe ¶
ListenAndServe listens on the TCP network address addr and then calls Serve to handle requests on incoming connections.
func (*Server) Stop ¶
Stop stops the tcp server. StopImmediately: immediately closes all open connections and listener. StopGracefullyButNotWait: stops the server to accept new connections. StopGracefullyAndWait: stops the server to accept new connections and blocks until all connections are closed.
Directories ¶
Path | Synopsis |
---|---|
exampleproto
Package exampleproto is a generated protocol buffer package.
|
Package exampleproto is a generated protocol buffer package. |