func (g *gateService) StartService(superRpcAddr string) { superClient, err := msg.DialSuperService("tcp", superRpcAddr) maxRetry := 10 for err != nil { if maxRetry > 0 { maxRetry = maxRetry - 1 } else { log.Fatal("连接SuperService失败") return } log.Error("连接SuperService失败,1秒后重试 :%v", err) time.Sleep(time.Second * 1) superClient, err = msg.DialSuperService("tcp", superRpcAddr) } res, err := superClient.Login(&msg.LoginRequst{ServiceIp: proto.String("127.0.0.1")}) if err != nil { log.Fatal("[GATE] 登录SuperService失败 rpc:%s", superRpcAddr) return } g.SuperClient = superClient g.gateway.Addr = string(res.GetServiceIp()) + ":" + strconv.Itoa(int(res.GetExterPort())) g.gateway.HTTPTimeout = 3 * 60 g.gateway.MaxConnNum = 1000 g.gateway.PendingWriteNum = 1000 g.gateway.ProtobufProcessor = g.processor log.Trace("[GATE] 网关服务在%s:%d 启动", string(res.GetServiceIp()), res.GetExterPort()) g.gateway.Run(utils.CloseSig) }
func (p *Processor) Handler(reciver network.IAgent, data []byte, clientData interface{}) (bool, error) { if len(data) < 2 { return false, errors.New("protobuf data too short") } nameLen := int(data[0])*265 + int(data[1]) if len(data) < 2+nameLen { return false, errors.New("protobuf data too short") } msgName := string(data[2 : nameLen+2]) //log.Dev("Recv Msg: %s", msgName) i, ok := p.msgInfo[msgName] if !ok { s := fmt.Sprintf("message %s not registered", msgName) log.Fatal(s) log.Debug("%v", p.msgInfo) return false, errors.New(s) } if i.msgHandler == nil { s := fmt.Sprintf("message %s not handler", msgName) log.Fatal(s) return false, errors.New(s) } msg := reflect.New(i.msgType.Elem()).Interface().(proto.Message) if e := proto.Unmarshal(data[nameLen+2:], msg); e != nil { return false, e } if i.msgHandler != nil { i.msgHandler(reciver, i.msgType.Elem(), msg, []interface{}{data, clientData}) } return true, nil }
//启动一个HTTP服务器,在Addr指定的地址 func (server *WSServer) Start() { //监听端口 ln, err := net.Listen("tcp", server.Addr) if err != nil { log.Fatal("%v", err) } server.checkValid() server.ln = ln server.handler = &WSHandler{ maxConnNum: server.MaxConnNum, pendingWriteNum: server.PendingWriteNum, newAgent: server.NewAgent, conns: make(WebsocketConnSet), upgrader: websocket.Upgrader{ //Upgrader对象 ReadBufferSize: 64 * 1024, //读缓冲 WriteBufferSize: 64 * 1024, //写缓冲 HandshakeTimeout: server.HTTPTimeout, //设置各种超时 CheckOrigin: func(_ *http.Request) bool { return true }, //忽略跨域访问限制 }, } //创建http服务器 httpServer := &http.Server{ Addr: server.Addr, Handler: server.handler, ReadTimeout: server.HTTPTimeout, //设置各种超时 WriteTimeout: server.HTTPTimeout, //设置各种超时 MaxHeaderBytes: 1024, } //启动HTTP服务器 go httpServer.Serve(ln) }
func (p *Processor) Register(msg proto.Message, msgHandler network.MsgHandler) { msgType := reflect.TypeOf(msg) msgName := msgType.String()[1:] if msgType == nil || msgType.Kind() != reflect.Ptr { log.Fatal("protobuf message pointer required") } if len(p.msgInfo) >= math.MaxUint16 { log.Fatal("too many protobuf messages (max = %v)", math.MaxUint16) } i := new(MsgInfo) i.msgType = msgType i.msgName = msgName i.msgHandler = msgHandler p.msgInfo[msgName] = i log.Trace("Register Msg Handle: %s", msgName) }
func (server *TCPServer) init() { ln, err := net.Listen("tcp", server.Addr) if err != nil { log.Fatal("%v", err) } server.check_valid() server.ln = ln server.conns = make(ConnSet) server.closeFlag = false server.msgParser = NewMsgParser() }
//检查参数合法性 func (server *TCPServer) check_valid() { if server.MaxConnNum <= 0 { server.MaxConnNum = 100 log.Trace("invalid MaxConnNum, reset to %v", server.MaxConnNum) } if server.PendingWriteNum <= 0 { server.PendingWriteNum = 100 log.Trace("invalid PendingWriteNum, reset to %v", server.PendingWriteNum) } if server.NewAgent == nil { log.Fatal("NewAgent must not be nil") } }
//配置合理性检查 func (server *WSServer) checkValid() { if server.MaxConnNum <= 0 { server.MaxConnNum = 100 log.Trace("invalid MaxConnNum, reset to %v", server.MaxConnNum) } if server.PendingWriteNum <= 0 { server.PendingWriteNum = 100 log.Trace("invalid PendingWriteNum, reset to %v", server.PendingWriteNum) } if server.NewAgent == nil { log.Fatal("NewAgent must not be nil") } if server.HTTPTimeout <= 0 { server.HTTPTimeout = 10 * time.Second log.Trace("invalid HTTPTimeout, reset to %v", server.HTTPTimeout) } }
func (a *TCPAgent) Fatal(format string, d ...interface{}) { log.Fatal(a.description()+format, d...) }