func ClientRawMessage(conn *GxTcpConn, msg *GxMessage) error { Debug("new client message, remote: %s %s", conn.Conn.RemoteAddr().String(), msg.String()) _, server := GetServerByCmd(msg.GetCmd()) if server == nil { Debug("msg is not register, remote: %s, msg: %s", conn.Conn.RemoteAddr().String(), msg.String()) return errors.New("close") } msg.SetId(conn.Id) server.Send(msg) return nil }
func (server *GxTcpServer) runConn(gxConn *GxTcpConn) { go gxConn.runHeartbeat() for { var msg *GxMessage = nil var err error empty := gxConn.GetProcessMsgNull() if empty { //没有消息正在处理,查找当前缓存的消息 msg = gxConn.GetUnprocessMsg() } if msg == nil { //如果没有缓存的消息,则读取新信息 msg, err = gxConn.Recv() if err != nil { server.closeConn(gxConn) return } if !empty { //如果有消息正在处理,缓存刚刚收到的消息 Debug("client has processing message, remote: %s, msg: %s", gxConn.Conn.RemoteAddr().String(), msg.String()) gxConn.SaveUnprocessMsg(msg) continue } } if msg.GetCmd() != CmdHeartbeat { Debug("recv buff msg, info: %s", msg.String()) } if cb, ok := server.clientCmds[msg.GetCmd()]; ok { //消息已经被注册 err = cb(gxConn, msg) if err != nil { //回调返回值不为空,则关闭连接 server.closeConn(gxConn) return } continue } //消息没有被注册 err = server.Rm(gxConn, msg) if err != nil { server.closeConn(gxConn) return } } }
func ServerRawMessage(conn *GxTcpConn, msg *GxMessage) error { Debug("new server message, remote: %s %s", conn.Conn.RemoteAddr().String(), msg.String()) client := clientRouter.FindConnById(msg.GetId()) if client == nil { Debug("msg cannot find target, remote: %s, msg: %s", conn.Conn.RemoteAddr().String(), msg.String()) return nil } client.Send(msg) if msg.GetMask(MessageMaskDisconn) { client.Conn.Close() } return nil }