func (client *TCPClient) init() { client.Lock() defer client.Unlock() if client.ConnNum <= 0 { client.ConnNum = 1 log.Release("invalid ConnNum, reset to %v", client.ConnNum) } if client.ConnectInterval <= 0 { client.ConnectInterval = 3 * time.Second log.Release("invalid ConnectInterval, reset to %v", client.ConnectInterval) } if client.PendingWriteNum <= 0 { client.PendingWriteNum = 100 log.Release("invalid PendingWriteNum, reset to %v", client.PendingWriteNum) } if client.NewAgent == nil { log.Fatal("NewAgent must not be nil") } if client.conns != nil { log.Fatal("client is running") } client.conns = make(ConnSet) client.closeFlag = false // msg parser msgParser := NewMsgParser() msgParser.SetMsgLen(client.LenMsgLen, client.MinMsgLen, client.MaxMsgLen) msgParser.SetByteOrder(client.LittleEndian) client.msgParser = msgParser }
func (server *TCPServer) init() { ln, err := net.Listen("tcp", server.Addr) if err != nil { log.Fatal("%v", err) } if server.MaxConnNum <= 0 { server.MaxConnNum = 100 log.Release("invalid MaxConnNum, reset to %v", server.MaxConnNum) } if server.PendingWriteNum <= 0 { server.PendingWriteNum = 100 log.Release("invalid PendingWriteNum, reset to %v", server.PendingWriteNum) } if server.NewAgent == nil { log.Fatal("NewAgent must not be nil") } server.ln = ln server.conns = make(ConnSet) // msg parser msgParser := NewMsgParser() msgParser.SetMsgLen(server.LenMsgLen, server.MinMsgLen, server.MaxMsgLen) msgParser.SetByteOrder(server.LittleEndian) server.msgParser = msgParser }
// It's dangerous to call the method on routing or marshaling (unmarshaling) func (p *Processor) SetHandler(msg interface{}, msgHandler MsgHandler) { msgType := reflect.TypeOf(msg) if msgType == nil || msgType.Kind() != reflect.Ptr { log.Fatal("json message pointer required") } msgID := msgType.Elem().Name() i, ok := p.msgInfo[msgID] if !ok { log.Fatal("message %v not registered", msgID) } i.msgHandler = msgHandler }
func (server *WSServer) Start() { ln, err := net.Listen("tcp", server.Addr) if err != nil { log.Fatal("%v", err) } if server.MaxConnNum <= 0 { server.MaxConnNum = 100 log.Release("invalid MaxConnNum, reset to %v", server.MaxConnNum) } if server.PendingWriteNum <= 0 { server.PendingWriteNum = 100 log.Release("invalid PendingWriteNum, reset to %v", server.PendingWriteNum) } if server.MaxMsgLen <= 0 { server.MaxMsgLen = 4096 log.Release("invalid MaxMsgLen, reset to %v", server.MaxMsgLen) } if server.HTTPTimeout <= 0 { server.HTTPTimeout = 10 * time.Second log.Release("invalid HTTPTimeout, reset to %v", server.HTTPTimeout) } if server.NewAgent == nil { log.Fatal("NewAgent must not be nil") } server.ln = ln server.handler = &WSHandler{ maxConnNum: server.MaxConnNum, pendingWriteNum: server.PendingWriteNum, maxMsgLen: server.MaxMsgLen, newAgent: server.NewAgent, conns: make(WebsocketConnSet), upgrader: websocket.Upgrader{ HandshakeTimeout: server.HTTPTimeout, CheckOrigin: func(_ *http.Request) bool { return true }, }, } httpServer := &http.Server{ Addr: server.Addr, Handler: server.handler, ReadTimeout: server.HTTPTimeout, WriteTimeout: server.HTTPTimeout, MaxHeaderBytes: 1024, } go httpServer.Serve(ln) }
// It's dangerous to call the method on routing or marshaling (unmarshaling) func (p *Processor) Register(msg proto.Message) { msgType := reflect.TypeOf(msg) if msgType == nil || msgType.Kind() != reflect.Ptr { log.Fatal("protobuf message pointer required") } if _, ok := p.msgID[msgType]; ok { log.Fatal("message %s is already registered", msgType) } if len(p.msgInfo) >= math.MaxUint16 { log.Fatal("too many protobuf messages (max = %v)", math.MaxUint16) } i := new(MsgInfo) i.msgType = msgType p.msgInfo = append(p.msgInfo, i) p.msgID[msgType] = uint16(len(p.msgInfo) - 1) }
// It's dangerous to call the method on routing or marshaling (unmarshaling) func (p *Processor) Register(msg interface{}) { msgType := reflect.TypeOf(msg) if msgType == nil || msgType.Kind() != reflect.Ptr { log.Fatal("json message pointer required") } msgID := msgType.Elem().Name() if msgID == "" { log.Fatal("unnamed json message") } if _, ok := p.msgInfo[msgID]; ok { log.Fatal("message %v is already registered", msgID) } i := new(MsgInfo) i.msgType = msgType p.msgInfo[msgID] = i }
// It's dangerous to call the method on routing or marshaling (unmarshaling) func (p *Processor) SetHandler(msg proto.Message, msgHandler MsgHandler) { msgType := reflect.TypeOf(msg) id, ok := p.msgID[msgType] if !ok { log.Fatal("message %s not registered", msgType) } p.msgInfo[id].msgHandler = msgHandler }
// It's dangerous to call the method on routing or marshaling (unmarshaling) func (p *Processor) SetRouter(msg proto.Message, msgRouter *chanrpc.Server) { msgType := reflect.TypeOf(msg) id, ok := p.msgID[msgType] if !ok { log.Fatal("message %s not registered", msgType) } p.msgInfo[id].msgRouter = msgRouter }
func (client *WSClient) init() { client.Lock() defer client.Unlock() if client.ConnNum <= 0 { client.ConnNum = 1 log.Release("invalid ConnNum, reset to %v", client.ConnNum) } if client.ConnectInterval <= 0 { client.ConnectInterval = 3 * time.Second log.Release("invalid ConnectInterval, reset to %v", client.ConnectInterval) } if client.PendingWriteNum <= 0 { client.PendingWriteNum = 100 log.Release("invalid PendingWriteNum, reset to %v", client.PendingWriteNum) } if client.MaxMsgLen <= 0 { client.MaxMsgLen = 4096 log.Release("invalid MaxMsgLen, reset to %v", client.MaxMsgLen) } if client.HandshakeTimeout <= 0 { client.HandshakeTimeout = 10 * time.Second log.Release("invalid HandshakeTimeout, reset to %v", client.HandshakeTimeout) } if client.NewAgent == nil { log.Fatal("NewAgent must not be nil") } if client.conns != nil { log.Fatal("client is running") } client.conns = make(WebsocketConnSet) client.closeFlag = false client.dialer = websocket.Dialer{ HandshakeTimeout: client.HandshakeTimeout, } }
// you must call the function before calling console.Init // goroutine not safe func Register(name string, help string, f interface{}, server *chanrpc.Server) { for _, c := range commands { if c.name() == name { log.Fatal("command %v is already registered", name) } } server.Register(name, f) c := new(ExternalCommand) c._name = name c._help = help c.server = server commands = append(commands, c) }