예제 #1
0
func (agent *gateway_agent) Run() {
	defer util.LogPanicStack()
	for {
		agent.conn.SetReadDeadline(time.Now().Add(120 * time.Second))
		dataBytes, err := agent.msgParser.Read(agent.conn)
		if err != nil {
			if !agent.isClosed {
				if ne, ok := err.(net.Error); ok {
					logger.Error("网关消息接收错误:%v", ne)
				}
			}
			return
		}
		if agent.check_msg_speed_lager() {
			logger.Error("发送消息太快")
			agent.Close(4)
			return
		}
		id, msg, err := proto.DecodeProto(dataBytes)
		if err != nil {
			logger.Error("网关协议解析错误:%s", err.Error())
			return
		}
		logger.Debug("receive msg:%d  %#v", id, msg)
		agent.dispatcher(id, msg)
	}
}
예제 #2
0
//发送proto消息
func send_msg(conn net.Conn, msg proto.Messager, pk *proto.Packet) {
	logger.Debug("发送消息:%#v", msg)
	pk.SeekTo(2)
	data := proto.EncodeProtoPacket(msg, pk)
	bs := data[0:2]
	binary.BigEndian.PutUint16(bs, uint16(len(data)-2))
	conn.Write(data)
}
예제 #3
0
func NewAgent(conn net.Conn, msgParser msg.MsgParser) network.TCPAgent {
	agent := new(gateway_agent)
	agent.conn = conn
	agent.msgParser = msgParser.Clone()
	agent.lastCheckSpeedSec = time.Now().Unix()
	agent.pk = proto.NewWriter()
	agent.exitCnt = make(chan struct{})
	logger.Debug("socket:%v", conn.RemoteAddr())
	return agent
}
예제 #4
0
func (agent *gateway_agent) Close(reason int8) {
	if !agent.isClosed {
		agent.closeMut.Lock()
		if !agent.isClosed {
			agent.isClosed = true
			agent.closeMut.Unlock()
			cgw.DeleteGW(agent.roleID)
			//不是异地登陆
			if reason != 3 {
				unRegisterAgent(agent)
			}
			//发送关闭消息
			if reason != 0 {
				send_msg(agent.conn, &proto.Sc_account_kick{Reason: reason}, agent.pk)
			}
			agent.conn.Close()
			close(agent.exitCnt)
			agent.wgExitCnt.Wait()
			logger.Debug("gw exit====:%d===reason:%d", agent.roleID, reason)
		} else {
			agent.closeMut.Unlock()
		}
	}
}