├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── _example ├── README.md ├── exampleproto │ ├── build_proto.sh │ ├── example.pb.go │ └── example.proto ├── main.go └── protocol.go ├── go.mod ├── go.sum ├── xconn.go ├── xserver.go └── xtcp.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | go: 3 | - master 4 | - tip 5 | 6 | script: 7 | - cd ./_example && go build && ./_example -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 xfx.dev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # xtcp 2 | 3 | A TCP Server Framework with graceful shutdown,custom protocol. 4 | 5 | [![Build Status](https://travis-ci.org/xfxdev/xtcp.svg?branch=master)](https://travis-ci.org/xfxdev/xtcp) 6 | [![Go Report Card](https://goreportcard.com/badge/github.com/xfxdev/xtcp)](https://goreportcard.com/report/github.com/xfxdev/xtcp) 7 | [![GoDoc](https://godoc.org/github.com/xfxdev/xtcp?status.svg)](https://godoc.org/github.com/xfxdev/xtcp) 8 | 9 | 10 | ## Usage 11 | 12 | ### Define your protocol format: 13 | Before create server and client, you need define the protocol format first. 14 | 15 | ```go 16 | // Packet is the unit of data. 17 | type Packet interface { 18 | fmt.Stringer 19 | } 20 | 21 | // Protocol use to pack/unpack Packet. 22 | type Protocol interface { 23 | // return the size need for pack the Packet. 24 | PackSize(p Packet) int 25 | // PackTo pack the Packet to w. 26 | // The return value n is the number of bytes written; 27 | // Any error encountered during the write is also returned. 28 | PackTo(p Packet, w io.Writer) (int, error) 29 | // Pack pack the Packet to new created buf. 30 | Pack(p Packet) ([]byte, error) 31 | // try to unpack the buf to Packet. If return len > 0, then buf[:len] will be discard. 32 | // The following return conditions must be implement: 33 | // (nil, 0, nil) : buf size not enough for unpack one Packet. 34 | // (nil, len, err) : buf size enough but error encountered. 35 | // (p, len, nil) : unpack succeed. 36 | Unpack(buf []byte) (Packet, int, error) 37 | } 38 | ``` 39 | 40 | ### Set your logger(optional): 41 | ```go 42 | func SetLogger(l Logger) 43 | ``` 44 | Note: xtcp will not output any log by default unless you implement your own logger. 45 | 46 | 47 | ### Provide event handler: 48 | In xtcp, there are some events to notify the state of net conn, you can handle them according your need: 49 | 50 | ```go 51 | const ( 52 | // EventAccept mean server accept a new connect. 53 | EventAccept EventType = iota 54 | // EventConnected mean client connected to a server. 55 | EventConnected 56 | // EventRecv mean conn recv a packet. 57 | EventRecv 58 | // EventClosed mean conn is closed. 59 | EventClosed 60 | ) 61 | ``` 62 | 63 | To handle the event, just implement the OnEvent interface. 64 | 65 | ```go 66 | // Handler is the event callback. 67 | // p will be nil when event is EventAccept/EventConnected/EventClosed 68 | type Handler interface { 69 | OnEvent(et EventType, c *Conn, p Packet) 70 | } 71 | ``` 72 | 73 | ### Create server: 74 | 75 | ```go 76 | // 1. create protocol and handler. 77 | // ... 78 | 79 | // 2. create opts. 80 | opts := xtcp.NewOpts(handler, protocol) 81 | 82 | // 3. create server. 83 | server := xtcp.NewServer(opts) 84 | 85 | // 4. start. 86 | go server.ListenAndServe("addr") 87 | ``` 88 | 89 | ### Create client: 90 | 91 | ```go 92 | // 1. create protocol and handler. 93 | // ... 94 | 95 | // 2. create opts. 96 | opts := xtcp.NewOpts(handler, protocol) 97 | 98 | // 3. create client. 99 | client := NewConn(opts) 100 | 101 | // 4. start 102 | go client.DialAndServe("addr") 103 | ``` 104 | 105 | ### Send and recv packet. 106 | To send data, just call the 'Send' function of Conn. You can safe call it in any goroutines. 107 | 108 | ```go 109 | func (c *Conn) Send(buf []byte) error 110 | ``` 111 | 112 | To recv a packet, implement your handler function: 113 | 114 | ```go 115 | func (h *myhandler) OnEvent(et EventType, c *Conn, p Packet) { 116 | switch et { 117 | case EventRecv: 118 | ... 119 | } 120 | } 121 | ``` 122 | 123 | ### Stop 124 | 125 | xtcp have three stop modes, stop gracefully mean conn will stop until all cached data sended. 126 | 127 | ```go 128 | // StopMode define the stop mode of server and conn. 129 | type StopMode uint8 130 | 131 | const ( 132 | // StopImmediately mean stop directly, the cached data maybe will not send. 133 | StopImmediately StopMode = iota 134 | // StopGracefullyButNotWait stop and flush cached data. 135 | StopGracefullyButNotWait 136 | // StopGracefullyAndWait stop and block until cached data sended. 137 | StopGracefullyAndWait 138 | ) 139 | ``` 140 | 141 | ## Example 142 | The example define a protocol format which use protobuf inner. 143 | You can see how to define the protocol and how to create server and client. 144 | 145 | [example](https://github.com/xfxdev/xtcp/tree/master/_example) 146 | -------------------------------------------------------------------------------- /_example/README.md: -------------------------------------------------------------------------------- 1 | # xtcp example 2 | 3 | The example define a protocol format which use protobuf inner. 4 | 5 | In this example you can see: 6 | 7 | * how to define the protocol format. 8 | * how to create server and client. 9 | * how to custom the logger. 10 | * how to handle event. 11 | * how to stop the server and client. -------------------------------------------------------------------------------- /_example/exampleproto/build_proto.sh: -------------------------------------------------------------------------------- 1 | protoc --go_out=./ *.proto -------------------------------------------------------------------------------- /_example/exampleproto/example.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. 2 | // source: example.proto 3 | // DO NOT EDIT! 4 | 5 | /* 6 | Package exampleproto is a generated protocol buffer package. 7 | 8 | It is generated from these files: 9 | example.proto 10 | 11 | It has these top-level messages: 12 | LoginRequest 13 | LoginResponse 14 | Chat 15 | */ 16 | package exampleproto 17 | 18 | import proto "github.com/golang/protobuf/proto" 19 | import fmt "fmt" 20 | import math "math" 21 | 22 | // Reference imports to suppress errors if they are not otherwise used. 23 | var _ = proto.Marshal 24 | var _ = fmt.Errorf 25 | var _ = math.Inf 26 | 27 | // This is a compile-time assertion to ensure that this generated file 28 | // is compatible with the proto package it is being compiled against. 29 | // A compilation error at this line likely means your copy of the 30 | // proto package needs to be updated. 31 | const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package 32 | 33 | type LoginRequest struct { 34 | Account *string `protobuf:"bytes,1,req,name=account" json:"account,omitempty"` 35 | Password *string `protobuf:"bytes,2,req,name=password" json:"password,omitempty"` 36 | XXX_unrecognized []byte `json:"-"` 37 | } 38 | 39 | func (m *LoginRequest) Reset() { *m = LoginRequest{} } 40 | func (m *LoginRequest) String() string { return proto.CompactTextString(m) } 41 | func (*LoginRequest) ProtoMessage() {} 42 | func (*LoginRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} } 43 | 44 | func (m *LoginRequest) GetAccount() string { 45 | if m != nil && m.Account != nil { 46 | return *m.Account 47 | } 48 | return "" 49 | } 50 | 51 | func (m *LoginRequest) GetPassword() string { 52 | if m != nil && m.Password != nil { 53 | return *m.Password 54 | } 55 | return "" 56 | } 57 | 58 | type LoginResponse struct { 59 | Ret *int32 `protobuf:"varint,1,req,name=ret" json:"ret,omitempty"` 60 | XXX_unrecognized []byte `json:"-"` 61 | } 62 | 63 | func (m *LoginResponse) Reset() { *m = LoginResponse{} } 64 | func (m *LoginResponse) String() string { return proto.CompactTextString(m) } 65 | func (*LoginResponse) ProtoMessage() {} 66 | func (*LoginResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} } 67 | 68 | func (m *LoginResponse) GetRet() int32 { 69 | if m != nil && m.Ret != nil { 70 | return *m.Ret 71 | } 72 | return 0 73 | } 74 | 75 | type Chat struct { 76 | Msg *string `protobuf:"bytes,1,req,name=msg" json:"msg,omitempty"` 77 | XXX_unrecognized []byte `json:"-"` 78 | } 79 | 80 | func (m *Chat) Reset() { *m = Chat{} } 81 | func (m *Chat) String() string { return proto.CompactTextString(m) } 82 | func (*Chat) ProtoMessage() {} 83 | func (*Chat) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} } 84 | 85 | func (m *Chat) GetMsg() string { 86 | if m != nil && m.Msg != nil { 87 | return *m.Msg 88 | } 89 | return "" 90 | } 91 | 92 | func init() { 93 | proto.RegisterType((*LoginRequest)(nil), "exampleproto.LoginRequest") 94 | proto.RegisterType((*LoginResponse)(nil), "exampleproto.LoginResponse") 95 | proto.RegisterType((*Chat)(nil), "exampleproto.Chat") 96 | } 97 | 98 | func init() { proto.RegisterFile("example.proto", fileDescriptor0) } 99 | 100 | var fileDescriptor0 = []byte{ 101 | // 139 bytes of a gzipped FileDescriptorProto 102 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0xe2, 0x4d, 0xad, 0x48, 0xcc, 103 | 0x2d, 0xc8, 0x49, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x81, 0x72, 0xc1, 0x3c, 0x25, 104 | 0x17, 0x2e, 0x1e, 0x9f, 0xfc, 0xf4, 0xcc, 0xbc, 0xa0, 0xd4, 0xc2, 0xd2, 0xd4, 0xe2, 0x12, 0x21, 105 | 0x09, 0x2e, 0xf6, 0xc4, 0xe4, 0xe4, 0xfc, 0xd2, 0xbc, 0x12, 0x09, 0x46, 0x05, 0x26, 0x0d, 0xce, 106 | 0x20, 0x18, 0x57, 0x48, 0x8a, 0x8b, 0xa3, 0x20, 0xb1, 0xb8, 0xb8, 0x3c, 0xbf, 0x28, 0x45, 0x82, 107 | 0x09, 0x2c, 0x05, 0xe7, 0x2b, 0x29, 0x72, 0xf1, 0x42, 0x4d, 0x29, 0x2e, 0xc8, 0xcf, 0x2b, 0x4e, 108 | 0x15, 0x12, 0xe0, 0x62, 0x2e, 0x4a, 0x85, 0x18, 0xc1, 0x1a, 0x04, 0x62, 0x2a, 0x49, 0x70, 0xb1, 109 | 0x38, 0x67, 0x24, 0x96, 0x80, 0x64, 0x72, 0x8b, 0xd3, 0xa1, 0x86, 0x83, 0x98, 0x80, 0x00, 0x00, 110 | 0x00, 0xff, 0xff, 0xef, 0xfa, 0xa7, 0x6d, 0xa0, 0x00, 0x00, 0x00, 111 | } 112 | -------------------------------------------------------------------------------- /_example/exampleproto/example.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package exampleproto; 4 | 5 | 6 | message LoginRequest { 7 | required string account = 1; 8 | required string password = 2; 9 | } 10 | 11 | message LoginResponse { 12 | required int32 ret = 1; // 0: failed, 1: success. 13 | } 14 | 15 | message Chat { 16 | required string msg = 1; 17 | } -------------------------------------------------------------------------------- /_example/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "net" 7 | 8 | "github.com/golang/protobuf/proto" 9 | "github.com/xfxdev/xtcp" 10 | "github.com/xfxdev/xtcp/_example/exampleproto" 11 | ) 12 | 13 | type processor func(c *xtcp.Conn, msg proto.Message) 14 | 15 | type exampleLogger struct {} 16 | func (*exampleLogger)Log(l xtcp.LogLevel, v ...interface{}) { 17 | if l != xtcp.Debug { 18 | log.Output(2, logLevel2Str[l]+" "+fmt.Sprint(v...)) 19 | } 20 | } 21 | func (*exampleLogger)Logf(l xtcp.LogLevel, format string, v ...interface{}) { 22 | if l != xtcp.Debug { 23 | log.Output(2, logLevel2Str[l]+" "+fmt.Sprintf(format, v...)) 24 | } 25 | } 26 | 27 | var ( 28 | logger = &exampleLogger{} 29 | protocol = &ProtobufProtocol{} 30 | mapProcessor = make(map[string]processor) 31 | msghello = "client : Hello" 32 | msgByeBye = "client : ByeBye" 33 | msgHi = "server : Hi" 34 | msgBye = "server : Bye" 35 | 36 | logLevel2Str = []string{ 37 | "[P]", 38 | "[F]", 39 | "[E]", 40 | "[W]", 41 | "[I]", 42 | "[D]", 43 | } 44 | ) 45 | 46 | func init() { 47 | mapProcessor[proto.MessageName((*exampleproto.LoginRequest)(nil))] = loginRequestProcess 48 | mapProcessor[proto.MessageName((*exampleproto.LoginResponse)(nil))] = loginResponseProcess 49 | mapProcessor[proto.MessageName((*exampleproto.Chat)(nil))] = chatProcess 50 | xtcp.SetLogger(logger) 51 | } 52 | func loginRequestProcess(c *xtcp.Conn, msg proto.Message) { 53 | if request, ok := msg.(*exampleproto.LoginRequest); ok { 54 | logger.Logf(xtcp.Info, "login request - account : %v, pw : %v", request.GetAccount(), request.GetPassword()) 55 | 56 | response := &exampleproto.LoginResponse{ 57 | Ret: proto.Int32(1), 58 | } 59 | buf, err := protocol.Pack(NewProtobufPacket(response)) 60 | if err != nil { 61 | logger.Log(xtcp.Info, "failed to pack msg : ", err) 62 | return 63 | } 64 | c.Send(buf) 65 | } 66 | } 67 | func loginResponseProcess(c *xtcp.Conn, msg proto.Message) { 68 | if response, ok := msg.(*exampleproto.LoginResponse); ok { 69 | if response.GetRet() == 1 { 70 | logger.Log(xtcp.Info, "login success.") 71 | 72 | // start chat after login success. 73 | chatMsg := &exampleproto.Chat{ 74 | Msg: proto.String(msghello), 75 | } 76 | c.SendPacket(NewProtobufPacket(chatMsg)) 77 | } else { 78 | logger.Log(xtcp.Info, "login failed.") 79 | } 80 | } 81 | } 82 | func chatProcess(c *xtcp.Conn, msg proto.Message) { 83 | if chatMsg, ok := msg.(*exampleproto.Chat); ok { 84 | 85 | logger.Log(xtcp.Info, " - ", chatMsg.GetMsg()) 86 | 87 | var strResponseMsg string 88 | switch c.UserData.(string) { 89 | case "server": 90 | switch chatMsg.GetMsg() { 91 | case msghello: 92 | strResponseMsg = msgHi 93 | case msgByeBye: 94 | strResponseMsg = msgBye 95 | } 96 | case "client": 97 | switch chatMsg.GetMsg() { 98 | case msgHi: 99 | strResponseMsg = msgByeBye 100 | case msgBye: 101 | c.Stop(xtcp.StopGracefullyButNotWait) 102 | return 103 | } 104 | } 105 | 106 | msgChatResponse := &exampleproto.Chat{ 107 | Msg: proto.String(strResponseMsg), 108 | } 109 | c.SendPacket(NewProtobufPacket(msgChatResponse)) 110 | } 111 | } 112 | 113 | type myHandler struct { 114 | } 115 | 116 | func (h *myHandler) OnAccept(c *xtcp.Conn) { 117 | logger.Log(xtcp.Info, "accept : ", c) 118 | c.UserData = "server" 119 | } 120 | func (h *myHandler) OnConnect(c *xtcp.Conn) { 121 | logger.Log(xtcp.Info, "connected : ", c) 122 | c.UserData = "client" 123 | } 124 | func (h *myHandler) OnRecv(c *xtcp.Conn, p xtcp.Packet) { 125 | if protobufPacket, ok := p.(*ProtobufPacket); ok { 126 | proc := mapProcessor[proto.MessageName(protobufPacket.Msg)] 127 | if proc != nil { 128 | proc(c, protobufPacket.Msg) 129 | } else { 130 | logger.Log(xtcp.Info, "no processor for Packet : ", p) 131 | } 132 | } 133 | } 134 | func (h *myHandler) OnUnpackErr(c *xtcp.Conn, buf []byte, err error) { 135 | 136 | } 137 | func (h *myHandler) OnClose(c *xtcp.Conn) { 138 | logger.Logf(xtcp.Info, "close : %v", c.RawConn.RemoteAddr()) 139 | } 140 | 141 | func main() { 142 | h := &myHandler{} 143 | opts := xtcp.NewOpts(h, protocol) 144 | l, err := net.Listen("tcp", ":") 145 | if err != nil { 146 | logger.Log(xtcp.Info, "listen err : ", err) 147 | return 148 | } 149 | server := xtcp.NewServer(opts) 150 | go func() { 151 | server.Serve(l) 152 | }() 153 | 154 | client := xtcp.NewConn(opts) 155 | clientClosed := make(chan struct{}) 156 | go func() { 157 | err := client.DialAndServe(l.Addr().String()) 158 | if err != nil { 159 | logger.Log(xtcp.Info, "client dial err : ", err) 160 | } 161 | close(clientClosed) 162 | }() 163 | 164 | // send login request. 165 | msgLoginRequest := &exampleproto.LoginRequest{ 166 | Account: proto.String("123"), 167 | Password: proto.String("456"), 168 | } 169 | client.SendPacket(NewProtobufPacket(msgLoginRequest)) 170 | 171 | <-clientClosed 172 | server.Stop(xtcp.StopGracefullyAndWait) 173 | logger.Log(xtcp.Info, "server and client stoped. thanks.") 174 | } 175 | -------------------------------------------------------------------------------- /_example/protocol.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bytes" 5 | "encoding/binary" 6 | "errors" 7 | "github.com/golang/protobuf/proto" 8 | "github.com/xfxdev/xtcp" 9 | "io" 10 | "reflect" 11 | ) 12 | 13 | var ( 14 | errUnknownProtobufMsgType = errors.New("Unknown protobuf message type") 15 | errBufSizeNotEnoughToPack = errors.New("buf size not enough") 16 | errInvalidPacket = errors.New("invalid packet") 17 | ) 18 | 19 | // ProtobufPacket size : msglen + msgNameLen + len(msgName) + len(msgData) 20 | type ProtobufPacket struct { 21 | Msg proto.Message 22 | } 23 | 24 | // NewProtobufPacket create new ProtobufPacket by proto.Message 25 | func NewProtobufPacket(msg proto.Message) *ProtobufPacket { 26 | p := &ProtobufPacket{ 27 | Msg: msg, 28 | } 29 | 30 | return p 31 | } 32 | 33 | // ProtobufProtocol type. 34 | type ProtobufProtocol struct { 35 | } 36 | 37 | func (p *ProtobufPacket) String() string { 38 | if p.Msg == nil { 39 | return "" 40 | } 41 | 42 | // return proto.MarshalTextString(p.Msg) 43 | return proto.CompactTextString(p.Msg) 44 | } 45 | 46 | // PackSize return size need for pack ProtobufPacket. 47 | func (pro *ProtobufProtocol) PackSize(p xtcp.Packet) int { 48 | pp := p.(*ProtobufPacket) 49 | if pp == nil { 50 | return 0 51 | } 52 | 53 | msgName := proto.MessageName(pp.Msg) 54 | msgNameLen := len(msgName) 55 | if msgNameLen == 0 { 56 | return 0 57 | } 58 | return 4 + 4 + msgNameLen + proto.Size(pp.Msg) 59 | } 60 | 61 | // PackTo : 62 | func (pro *ProtobufProtocol) PackTo(p xtcp.Packet, w io.Writer) (int, error) { 63 | pp := p.(*ProtobufPacket) 64 | if pp == nil { 65 | return 0, errUnknownProtobufMsgType 66 | } 67 | 68 | msgName := proto.MessageName(pp.Msg) 69 | msgNameLen := len(msgName) 70 | if msgNameLen == 0 { 71 | return 0, errUnknownProtobufMsgType 72 | } 73 | msgLen := 4 + 4 + msgNameLen + proto.Size(pp.Msg) 74 | 75 | data, err := proto.Marshal(pp.Msg) 76 | if err != nil { 77 | return 0, err 78 | } 79 | 80 | wl := 0 81 | err = binary.Write(w, binary.BigEndian, uint32(msgLen)) 82 | if err != nil { 83 | return wl, err 84 | } 85 | wl += 4 86 | err = binary.Write(w, binary.BigEndian, uint32(msgNameLen)) 87 | if err != nil { 88 | return wl, err 89 | } 90 | wl += 4 91 | n, err := w.Write([]byte(msgName)) 92 | wl += n 93 | if err != nil { 94 | return wl, err 95 | } 96 | n, err = w.Write(data) 97 | wl += n 98 | if err != nil { 99 | return wl, err 100 | } 101 | return wl, nil 102 | } 103 | 104 | // Pack : 105 | func (pro *ProtobufProtocol) Pack(p xtcp.Packet) ([]byte, error) { 106 | len := pro.PackSize(p) 107 | if len != 0 { 108 | buf := bytes.NewBuffer(nil) 109 | _, err := pro.PackTo(p, buf) 110 | return buf.Bytes(), err 111 | } 112 | return nil, errUnknownProtobufMsgType 113 | } 114 | 115 | // Unpack : 116 | func (pro *ProtobufProtocol) Unpack(buf []byte) (xtcp.Packet, int, error) { 117 | if len(buf) < 4 { 118 | return nil, 0, nil 119 | } 120 | 121 | msgLen := int(binary.BigEndian.Uint32(buf[:4])) 122 | if len(buf) < msgLen { 123 | return nil, 0, nil 124 | } 125 | 126 | msgNameLen := binary.BigEndian.Uint32(buf[4:8]) 127 | if int(msgNameLen)+8 > msgLen { 128 | return nil, msgLen, errInvalidPacket 129 | } 130 | msgName := string(buf[8 : 8+msgNameLen]) 131 | 132 | t := proto.MessageType(msgName) 133 | if t == nil { 134 | return nil, msgLen, errUnknownProtobufMsgType 135 | } 136 | msg := reflect.New(t.Elem()).Interface().(proto.Message) 137 | err := proto.Unmarshal(buf[8+msgNameLen:msgLen], msg) 138 | if err != nil { 139 | return nil, msgLen, err 140 | } 141 | 142 | p := NewProtobufPacket(msg) 143 | return p, msgLen, nil 144 | } 145 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/xfxdev/xtcp 2 | 3 | go 1.14 4 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xfxdev/xtcp/2a44c141a854b9ae1a32c6906b2aa1e89afc8037/go.sum -------------------------------------------------------------------------------- /xconn.go: -------------------------------------------------------------------------------- 1 | package xtcp 2 | 3 | import ( 4 | "bytes" 5 | "errors" 6 | "io" 7 | "net" 8 | "sync" 9 | "sync/atomic" 10 | "time" 11 | ) 12 | 13 | const ( 14 | connStateNormal int32 = iota 15 | connStateStopping 16 | connStateStopped 17 | ) 18 | 19 | var ( 20 | errSendToClosedConn = errors.New("send to closed conn") 21 | errSendListFull = errors.New("send list full") 22 | 23 | bufferPool1K = &sync.Pool{ 24 | New: func() interface{} { 25 | return make([]byte, 1<<10) 26 | }, 27 | } 28 | bufferPool2K = &sync.Pool{ 29 | New: func() interface{} { 30 | return make([]byte, 2<<10) 31 | }, 32 | } 33 | bufferPool4K = &sync.Pool{ 34 | New: func() interface{} { 35 | return make([]byte, 4<<10) 36 | }, 37 | } 38 | bufferPoolBig = &sync.Pool{} 39 | ) 40 | 41 | func getBufferFromPool(targetSize int) []byte { 42 | var buf []byte 43 | if targetSize <= 1<<10 { 44 | buf = bufferPool1K.Get().([]byte) 45 | } else if targetSize <= 2<<10 { 46 | buf = bufferPool2K.Get().([]byte) 47 | } else if targetSize <= 4<<10 { 48 | buf = bufferPool4K.Get().([]byte) 49 | } else { 50 | itr := bufferPoolBig.Get() 51 | if itr != nil { 52 | buf = itr.([]byte) 53 | if cap(buf) < targetSize { 54 | bufferPoolBig.Put(itr) 55 | buf = make([]byte, targetSize) 56 | } 57 | } else { 58 | buf = make([]byte, targetSize) 59 | } 60 | } 61 | buf = buf[:targetSize] 62 | return buf 63 | } 64 | 65 | func putBufferToPool(buf []byte) { 66 | cap := cap(buf) 67 | if cap <= 1<<10 { 68 | bufferPool1K.Put(buf) 69 | } else if cap <= 2<<10 { 70 | bufferPool2K.Put(buf) 71 | } else if cap <= 4<<10 { 72 | bufferPool4K.Put(buf) 73 | } else { 74 | bufferPoolBig.Put(buf) 75 | } 76 | } 77 | 78 | // A Conn represents the server side of an tcp connection. 79 | type Conn struct { 80 | sync.Mutex 81 | Opts *Options 82 | RawConn net.Conn 83 | UserData interface{} 84 | sendBufList chan []byte 85 | closed chan struct{} 86 | state int32 87 | wg sync.WaitGroup 88 | once sync.Once 89 | SendDropped uint32 90 | sendBytes uint64 91 | recvBytes uint64 92 | dropped uint32 93 | } 94 | 95 | // NewConn return new conn. 96 | func NewConn(opts *Options) *Conn { 97 | if opts.RecvBufSize <= 0 { 98 | logger.Logf(Warn, "Invalid Opts.RecvBufSize : %v, use DefaultRecvBufSize instead", opts.RecvBufSize) 99 | opts.RecvBufSize = DefaultRecvBufSize 100 | } 101 | if opts.SendBufListLen <= 0 { 102 | logger.Logf(Warn, "Invalid Opts.SendBufListLen : %v, use DefaultRecvBufSize instead", opts.SendBufListLen) 103 | opts.SendBufListLen = DefaultSendBufListLen 104 | } 105 | c := &Conn{ 106 | Opts: opts, 107 | sendBufList: make(chan []byte, opts.SendBufListLen), 108 | closed: make(chan struct{}), 109 | state: connStateNormal, 110 | } 111 | 112 | return c 113 | } 114 | 115 | func (c *Conn) String() string { 116 | return c.RawConn.LocalAddr().String() + " -> " + c.RawConn.RemoteAddr().String() 117 | } 118 | 119 | // SendBytes return the total send bytes. 120 | func (c *Conn) SendBytes() uint64 { 121 | return atomic.LoadUint64(&c.sendBytes) 122 | } 123 | 124 | // RecvBytes return the total receive bytes. 125 | func (c *Conn) RecvBytes() uint64 { 126 | return atomic.LoadUint64(&c.recvBytes) 127 | } 128 | 129 | // DroppedPacket return the total dropped packet. 130 | func (c *Conn) DroppedPacket() uint32 { 131 | return atomic.LoadUint32(&c.dropped) 132 | } 133 | 134 | // Stop stops the conn. 135 | func (c *Conn) Stop(mode StopMode) { 136 | c.once.Do(func() { 137 | if mode == StopImmediately { 138 | atomic.StoreInt32(&c.state, connStateStopped) 139 | c.RawConn.Close() 140 | //close(c.sendBufList) // leave channel open, because other goroutine maybe use it in Send. 141 | close(c.closed) 142 | } else { 143 | atomic.StoreInt32(&c.state, connStateStopping) 144 | // c.RawConn.Close() // will close in sendLoop 145 | // close(c.sendBufList) 146 | close(c.closed) 147 | if mode == StopGracefullyAndWait { 148 | c.wg.Wait() 149 | } 150 | } 151 | }) 152 | } 153 | 154 | // IsStoped return true if Conn is stopped, otherwise return false. 155 | func (c *Conn) IsStoped() bool { 156 | return atomic.LoadInt32(&c.state) != connStateNormal 157 | } 158 | 159 | func (c *Conn) serve() { 160 | tcpConn := c.RawConn.(*net.TCPConn) 161 | tcpConn.SetNoDelay(c.Opts.NoDelay) 162 | tcpConn.SetKeepAlive(c.Opts.KeepAlive) 163 | if c.Opts.KeepAlivePeriod != 0 { 164 | tcpConn.SetKeepAlivePeriod(c.Opts.KeepAlivePeriod) 165 | } 166 | 167 | if c.Opts.AsyncWrite { 168 | c.wg.Add(2) 169 | go c.sendLoop() 170 | } else { 171 | c.wg.Add(1) 172 | } 173 | c.recvLoop() 174 | 175 | c.Opts.Handler.OnClose(c) 176 | } 177 | 178 | func (c *Conn) recvLoop() { 179 | var tempDelay time.Duration 180 | tempBuf := make([]byte, c.Opts.RecvBufSize) 181 | recvBuf := bytes.NewBuffer(make([]byte, 0, c.Opts.RecvBufSize)) 182 | maxDelay := 1 * time.Second 183 | 184 | defer func() { 185 | logger.Log(Debug, "XTCP - Conn recv loop exit : ", c.RawConn.RemoteAddr()) 186 | c.wg.Done() 187 | }() 188 | 189 | for { 190 | if c.Opts.ReadDeadline != 0 { 191 | c.RawConn.SetReadDeadline(time.Now().Add(c.Opts.ReadDeadline)) 192 | } 193 | 194 | n, err := c.RawConn.Read(tempBuf) 195 | if err != nil { 196 | if nerr, ok := err.(net.Error); ok { 197 | if nerr.Timeout() { 198 | // timeout 199 | } else if nerr.Temporary() { 200 | if tempDelay == 0 { 201 | tempDelay = 5 * time.Millisecond 202 | } else { 203 | tempDelay *= 2 204 | } 205 | if tempDelay > maxDelay { 206 | tempDelay = maxDelay 207 | } 208 | logger.Logf(Error, "XTCP - Conn[%v] recv error : %v; retrying in %v", c.RawConn.RemoteAddr(), err, tempDelay) 209 | time.Sleep(tempDelay) 210 | continue 211 | } 212 | } 213 | 214 | if !c.IsStoped() { 215 | if err != io.EOF { 216 | logger.Logf(Error, "XTCP - Conn[%v] recv error : %v", c.RawConn.RemoteAddr(), err) 217 | } 218 | c.Stop(StopImmediately) 219 | } 220 | 221 | return 222 | } 223 | 224 | recvBuf.Write(tempBuf[:n]) 225 | atomic.AddUint64(&c.recvBytes, uint64(n)) 226 | tempDelay = 0 227 | 228 | for recvBuf.Len() > 0 { 229 | p, pl, err := c.Opts.Protocol.Unpack(recvBuf.Bytes()) 230 | if err != nil { 231 | c.Opts.Handler.OnUnpackErr(c, recvBuf.Bytes(), err) 232 | } 233 | 234 | if pl > 0 { 235 | _ = recvBuf.Next(pl) 236 | } 237 | 238 | if p != nil { 239 | c.Opts.Handler.OnRecv(c, p) 240 | } else { 241 | break 242 | } 243 | } 244 | } 245 | } 246 | 247 | func (c *Conn) sendBuf(buf []byte) (int, error) { 248 | sended := 0 249 | var tempDelay time.Duration 250 | maxDelay := 1 * time.Second 251 | for sended < len(buf) { 252 | if c.Opts.WriteDeadline != 0 { 253 | c.RawConn.SetWriteDeadline(time.Now().Add(c.Opts.WriteDeadline)) 254 | } 255 | wn, err := c.RawConn.Write(buf[sended:]) 256 | if wn > 0 { 257 | sended += wn 258 | atomic.AddUint64(&c.sendBytes, uint64(wn)) 259 | } 260 | 261 | if err != nil { 262 | if nerr, ok := err.(net.Error); ok { 263 | if nerr.Timeout() { 264 | // timeout 265 | } else if nerr.Temporary() { 266 | if tempDelay == 0 { 267 | tempDelay = 5 * time.Millisecond 268 | } else { 269 | tempDelay *= 2 270 | } 271 | if tempDelay > maxDelay { 272 | tempDelay = maxDelay 273 | } 274 | logger.Logf(Error, "XTCP - Conn[%v] Send error: %v; retrying in %v", c.RawConn.RemoteAddr(), err, tempDelay) 275 | time.Sleep(tempDelay) 276 | continue 277 | } 278 | } 279 | 280 | if !c.IsStoped() { 281 | logger.Logf(Error, "XTCP - Conn[%v] Send error : %v", c.RawConn.RemoteAddr(), err) 282 | c.Stop(StopImmediately) 283 | } 284 | return sended, err 285 | } 286 | tempDelay = 0 287 | } 288 | return sended, nil 289 | } 290 | 291 | func (c *Conn) sendLoop() { 292 | defer func() { 293 | logger.Log(Debug, "XTCP - Conn send loop exit : ", c.RawConn.RemoteAddr()) 294 | c.wg.Done() 295 | }() 296 | for { 297 | if atomic.LoadInt32(&c.state) == connStateStopped { 298 | return 299 | } 300 | 301 | select { 302 | case buf, ok := <-c.sendBufList: 303 | if !ok { 304 | return 305 | } 306 | _, err := c.sendBuf(buf) 307 | if err != nil { 308 | return 309 | } 310 | putBufferToPool(buf) 311 | case <-c.closed: 312 | if atomic.LoadInt32(&c.state) == connStateStopping { 313 | if len(c.sendBufList) == 0 { 314 | atomic.SwapInt32(&c.state, connStateStopped) 315 | c.RawConn.Close() 316 | return 317 | } 318 | } 319 | } 320 | } 321 | } 322 | 323 | // Send use for send data, can be call in any goroutines. 324 | func (c *Conn) Send(buf []byte) (int, error) { 325 | if atomic.LoadInt32(&c.state) != connStateNormal { 326 | return 0, errSendToClosedConn 327 | } 328 | bufLen := len(buf) 329 | if bufLen <= 0 { 330 | return 0, nil 331 | } 332 | 333 | if c.Opts.AsyncWrite { 334 | buffer := getBufferFromPool(len(buf)) 335 | copy(buffer, buf) 336 | select { 337 | case c.sendBufList <- buffer: 338 | return bufLen, nil 339 | default: 340 | atomic.AddUint32(&c.dropped, 1) 341 | return 0, errSendListFull 342 | } 343 | } else { 344 | c.Lock() // Ensure entirety of buf is written together 345 | n, err := c.sendBuf(buf) 346 | c.Unlock() 347 | return n, err 348 | } 349 | } 350 | 351 | // SendPacket use for send packet, can be call in any goroutines. 352 | func (c *Conn) SendPacket(p Packet) (int, error) { 353 | if atomic.LoadInt32(&c.state) != connStateNormal { 354 | return 0, errSendToClosedConn 355 | } 356 | buf, err := c.Opts.Protocol.Pack(p) 357 | if err != nil { 358 | return 0, err 359 | } 360 | 361 | return c.Send(buf) 362 | } 363 | 364 | // DialAndServe connects to the addr and serve. 365 | func (c *Conn) DialAndServe(addr string) error { 366 | rawConn, err := net.Dial("tcp", addr) 367 | if err != nil { 368 | return err 369 | } 370 | 371 | c.RawConn = rawConn 372 | 373 | c.Opts.Handler.OnConnect(c) 374 | 375 | c.serve() 376 | 377 | return nil 378 | } 379 | -------------------------------------------------------------------------------- /xserver.go: -------------------------------------------------------------------------------- 1 | package xtcp 2 | 3 | import ( 4 | "net" 5 | "sync" 6 | "time" 7 | ) 8 | 9 | // Server used for running a tcp server. 10 | type Server struct { 11 | Opts *Options 12 | stopped chan struct{} 13 | wg sync.WaitGroup 14 | mu sync.Mutex 15 | once sync.Once 16 | lis net.Listener 17 | conns map[*Conn]bool 18 | } 19 | 20 | // ListenAndServe listens on the TCP network address addr and then 21 | // calls Serve to handle requests on incoming connections. 22 | func (s *Server) ListenAndServe(addr string) error { 23 | l, err := net.Listen("tcp", addr) 24 | if err != nil { 25 | return err 26 | } 27 | 28 | s.Serve(l) 29 | 30 | return nil 31 | } 32 | 33 | // Serve start the tcp server to accept. 34 | func (s *Server) Serve(l net.Listener) { 35 | defer s.wg.Done() 36 | 37 | s.wg.Add(1) 38 | 39 | s.mu.Lock() 40 | s.lis = l 41 | s.mu.Unlock() 42 | 43 | logger.Log(Info, "XTCP - Server listen on: ", l.Addr().String()) 44 | 45 | var tempDelay time.Duration // how long to sleep on accept failure 46 | maxDelay := 1 * time.Second 47 | 48 | for { 49 | conn, err := l.Accept() 50 | if err != nil { 51 | if nerr, ok := err.(net.Error); ok && nerr.Temporary() { 52 | if tempDelay == 0 { 53 | tempDelay = 5 * time.Millisecond 54 | } else { 55 | tempDelay *= 2 56 | } 57 | if tempDelay > maxDelay { 58 | tempDelay = maxDelay 59 | } 60 | logger.Logf(Error, "XTCP - Server Accept error: %v; retrying in %v", err, tempDelay) 61 | select { 62 | case <-time.After(tempDelay): 63 | continue 64 | case <-s.stopped: 65 | return 66 | } 67 | } 68 | 69 | if !s.IsStopped() { 70 | logger.Logf(Error, "XTCP - Server Accept error: %v; server closed!", err) 71 | s.Stop(StopImmediately) 72 | } 73 | 74 | return 75 | } 76 | 77 | tempDelay = 0 78 | go s.handleRawConn(conn) 79 | } 80 | } 81 | 82 | // IsStopped check if server is stopped. 83 | func (s *Server) IsStopped() bool { 84 | select { 85 | case <-s.stopped: 86 | return true 87 | default: 88 | return false 89 | } 90 | } 91 | 92 | // Stop stops the tcp server. 93 | // StopImmediately: immediately closes all open connections and listener. 94 | // StopGracefullyButNotWait: stops the server and stop all connections gracefully. 95 | // StopGracefullyAndWait: stops the server and blocks until all connections are stopped gracefully. 96 | func (s *Server) Stop(mode StopMode) { 97 | s.once.Do(func() { 98 | close(s.stopped) 99 | 100 | s.mu.Lock() 101 | lis := s.lis 102 | s.lis = nil 103 | conns := s.conns 104 | s.conns = nil 105 | s.mu.Unlock() 106 | 107 | if lis != nil { 108 | lis.Close() 109 | } 110 | 111 | m := mode 112 | if m == StopGracefullyAndWait { 113 | // don't wait each conn stop. 114 | m = StopGracefullyButNotWait 115 | } 116 | for c := range conns { 117 | c.Stop(m) 118 | } 119 | 120 | if mode == StopGracefullyAndWait { 121 | s.wg.Wait() 122 | } 123 | 124 | logger.Log(Info, "XTCP - Server stopped.") 125 | }) 126 | } 127 | 128 | func (s *Server) handleRawConn(conn net.Conn) { 129 | s.mu.Lock() 130 | if s.conns == nil { // s.conns == nil mean server stopped 131 | s.mu.Unlock() 132 | conn.Close() 133 | return 134 | } 135 | s.mu.Unlock() 136 | 137 | tcpConn := NewConn(s.Opts) 138 | tcpConn.RawConn = conn 139 | 140 | if !s.addConn(tcpConn) { 141 | tcpConn.Stop(StopImmediately) 142 | return 143 | } 144 | 145 | s.wg.Add(1) 146 | defer func() { 147 | s.removeConn(tcpConn) 148 | s.wg.Done() 149 | }() 150 | 151 | s.Opts.Handler.OnAccept(tcpConn) 152 | 153 | tcpConn.serve() 154 | } 155 | 156 | func (s *Server) addConn(conn *Conn) bool { 157 | s.mu.Lock() 158 | if s.conns == nil { 159 | s.mu.Unlock() 160 | return false 161 | } 162 | s.conns[conn] = true 163 | s.mu.Unlock() 164 | return true 165 | } 166 | 167 | func (s *Server) removeConn(conn *Conn) { 168 | s.mu.Lock() 169 | if s.conns != nil { 170 | delete(s.conns, conn) 171 | } 172 | s.mu.Unlock() 173 | } 174 | 175 | // CurClientCount return current client count. 176 | func (s *Server) CurClientCount() int { 177 | s.mu.Lock() 178 | defer s.mu.Unlock() 179 | return len(s.conns) 180 | } 181 | 182 | // NewServer create a tcp server but not start to accept. 183 | // The opts will set to all accept conns. 184 | func NewServer(opts *Options) *Server { 185 | if opts.RecvBufSize <= 0 { 186 | logger.Logf(Warn, "Invalid Opts.RecvBufSize : %v, use DefaultRecvBufSize instead", opts.RecvBufSize) 187 | opts.RecvBufSize = DefaultRecvBufSize 188 | } 189 | if opts.SendBufListLen <= 0 { 190 | logger.Logf(Warn, "Invalid Opts.SendBufListLen : %v, use DefaultSendBufListLen instead", opts.SendBufListLen) 191 | opts.SendBufListLen = DefaultSendBufListLen 192 | } 193 | s := &Server{ 194 | Opts: opts, 195 | stopped: make(chan struct{}), 196 | conns: make(map[*Conn]bool), 197 | } 198 | return s 199 | } 200 | -------------------------------------------------------------------------------- /xtcp.go: -------------------------------------------------------------------------------- 1 | package xtcp 2 | 3 | import ( 4 | "fmt" 5 | "io" 6 | "time" 7 | ) 8 | 9 | var ( 10 | // DefaultRecvBufSize is the default size of recv buf. 11 | DefaultRecvBufSize = 4 << 10 // 4k 12 | // DefaultSendBufListLen is the default length of send buf list. 13 | DefaultSendBufListLen = 1 << 10 // 1k 14 | // DefaultAsyncWrite is enable async write or not. 15 | DefaultAsyncWrite = true 16 | ) 17 | 18 | // StopMode define the stop mode of server and conn. 19 | type StopMode uint8 20 | 21 | const ( 22 | // StopImmediately mean stop directly, the cached data maybe will not send. 23 | StopImmediately StopMode = iota 24 | // StopGracefullyButNotWait stop and flush cached data. 25 | StopGracefullyButNotWait 26 | // StopGracefullyAndWait stop and block until cached data sended. 27 | StopGracefullyAndWait 28 | ) 29 | 30 | // LogLevel used to filter log message by the Logger. 31 | type LogLevel uint8 32 | 33 | // logging levels. 34 | const ( 35 | Panic LogLevel = iota 36 | Fatal 37 | Error 38 | Warn 39 | Info 40 | Debug 41 | ) 42 | // Logger is the log interface 43 | type Logger interface { 44 | Log(l LogLevel, v ...interface{}) 45 | Logf(l LogLevel, format string, v ...interface{}) 46 | } 47 | type emptyLogger struct {} 48 | func (*emptyLogger)Log(l LogLevel, v ...interface{}) {} 49 | func (*emptyLogger)Logf(l LogLevel, format string, v ...interface{}) {} 50 | var logger Logger = &emptyLogger{} 51 | // SetLogger set the logger 52 | func SetLogger(l Logger) { 53 | logger = l 54 | } 55 | 56 | // Handler is the event callback. 57 | // Note : don't block in event handler. 58 | type Handler interface { 59 | // OnAccept mean server accept a new connect. 60 | OnAccept(*Conn) 61 | // OnConnect mean client connected to a server. 62 | OnConnect(*Conn) 63 | // OnRecv mean conn recv a packet. 64 | OnRecv(*Conn, Packet) 65 | // OnUnpackErr mean failed to unpack recved data. 66 | OnUnpackErr(*Conn, []byte, error) 67 | // OnClose mean conn is closed. 68 | OnClose(*Conn) 69 | } 70 | 71 | // Packet is the unit of data. 72 | type Packet interface { 73 | fmt.Stringer 74 | } 75 | 76 | // Protocol use to pack/unpack Packet. 77 | type Protocol interface { 78 | // PackSize return the size need for pack the Packet. 79 | PackSize(p Packet) int 80 | // PackTo pack the Packet to w. 81 | // The return value n is the number of bytes written; 82 | // Any error encountered during the write is also returned. 83 | PackTo(p Packet, w io.Writer) (int, error) 84 | // Pack pack the Packet to new created buf. 85 | Pack(p Packet) ([]byte, error) 86 | // Unpack try to unpack the buf to Packet. If return len > 0, then buf[:len] will be discard. 87 | // The following return conditions must be implement: 88 | // (nil, 0, nil) : buf size not enough for unpack one Packet. 89 | // (nil, len, err) : buf size enough but error encountered. 90 | // (p, len, nil) : unpack succeed. 91 | Unpack(buf []byte) (Packet, int, error) 92 | } 93 | 94 | // Options is the options used for net conn. 95 | type Options struct { 96 | Handler Handler 97 | Protocol Protocol 98 | RecvBufSize int // default is DefaultRecvBufSize if you don't set. 99 | SendBufListLen int // default is DefaultSendBufListLen if you don't set. 100 | AsyncWrite bool // default is DefaultAsyncWrite if you don't set. 101 | NoDelay bool // default is true 102 | KeepAlive bool // default is false 103 | KeepAlivePeriod time.Duration // default is 0, mean use system setting. 104 | ReadDeadline time.Duration // default is 0, means Read will not time out. 105 | WriteDeadline time.Duration // default is 0, means Write will not time out. 106 | } 107 | 108 | // NewOpts create a new options and set some default value. 109 | // will panic if handler or protocol is nil. 110 | // eg: opts := NewOpts().SetSendListLen(len).SetRecvBufInitSize(len)... 111 | func NewOpts(h Handler, p Protocol) *Options { 112 | if h == nil || p == nil { 113 | panic("xtcp.NewOpts: nil handler or protocol") 114 | } 115 | return &Options{ 116 | Handler: h, 117 | Protocol: p, 118 | RecvBufSize: DefaultRecvBufSize, 119 | SendBufListLen: DefaultSendBufListLen, 120 | AsyncWrite: DefaultAsyncWrite, 121 | NoDelay: true, 122 | KeepAlive: false, 123 | } 124 | } 125 | --------------------------------------------------------------------------------