예제 #1
0
func (c *Client) initWormhole(dp *RoutePacket, conn IConnection) {
     //接到连接方hello包
    if dp.Guin == 0 {
        //这hello不正常,关掉连接
        c.Close()
        return
    }

    //TODO:重连处理
    //如果客户端hello是发送的guin有,则表示是重连,需要处理重连逻辑
    //比如在一定时间内可以重新连接会原有wormhole

    if c.wormhole == nil {
        c.wormhole = c.makeWormhole(dp.Guin, c.wormholes, c.routepack)
    }

    //将该连接绑定到wormhole,
    //并且connection的receivebytes将被wormhole接管
    //该函数将不会被该connection调用
    c.wormhole.AddConnection(conn, ECONN_TYPE_DATA)
    if c.wormholes != nil {
        c.wormholes.Add(c.wormhole)
        gts.Debug("has clients:",c.wormholes.Length())
    }
}
예제 #2
0
func (wh *ClientWormhole) ProcessPackets(dps []*wormhole.RoutePacket) {
	//gts.Trace("clientwormhole processpacket receive %d route packets",len(dps))
	for _, dp := range dps {
		gts.Debug(gutils.ByteString(dp.Data))
		//println(gutils.ByteString(dp.Data))
		//gts.Trace("%q", dp)
	}

}
예제 #3
0
func (s *TcpServer) Start() {
	gts.Info(s.Name + " tcpserver is starting...")

	s.Stop_ = false
	//todo: MaxConns don't proccess
	netListen, error := net.Listen("tcp", s.Addr)
	if error != nil {
		gts.Error(error)
		return
	}

	gts.Info("listen with :", s.Addr)
	gts.Info(s.Name + " tcpserver is started !!!")

	go func() {
		//defer函数退出时执行
		defer netListen.Close()

		for {
			gts.Trace("Waiting for connection")
			if s.Stop_ {
				break
			}

			connection, err := netListen.Accept()

			if err != nil {
				gts.Error("Transport error: ", err)
			} else {
				gts.Debug("%v is connection!", connection.RemoteAddr())

				newcid := s.AllocId()
				if newcid == 0 {
					gts.Warn("connection num is more than ", s.MaxConns)
				} else {
					gts.Trace("//////////////////////newcid:", newcid)
					tcpConn := s.makeConn(newcid, connection,
						s.RoutePackHandle.GetEndianer())
					tcpConn.SetReceiveCallback(s.receiveBytes)
				}
			}
		}
	}()
}
예제 #4
0
func (acw *AgentToClientWormhole) ProcessPackets(dps []*RoutePacket) {
	gts.Trace("agenttoclientwormhole processpackets receive %d packets", len(dps))

	for _, dp := range dps {
		gts.Debug("%q", dp)
		gts.Trace("guin:", acw.GetGuin())

		dp.Guin = acw.GetGuin()
		dp.Type = dp.Type | 1

		acw.SendPacket(dp)

		//转发给logic server
		//根据guin进行hash运算非配到相应的logic server
		if server, ok := acw.GetManager().GetServer().(*Agent); ok {
			server.LogicWormholes.(*LogicManager).Delay(dp)
		}
	}
}
예제 #5
0
func (s *TcpServer) receivePackets(conn IConnection, dps []*RoutePacket) {
	for _, dp := range dps {
		if dp.Type == EPACKET_TYPE_HELLO {
			gts.Trace("server receive tcp hello:%q", dp)

			var guin int
			var wh IWormhole

			if dp.Guin > 0 {
				//TODO:重连处理
				//如果客户端hello是发送的guin有,则表示是重连,需要处理重连逻辑
				//比如在一定时间内可以重新连接会原有wormhole

				guin = dp.Guin
				if wh, ok := s.Wormholes.Get(guin); ok {
					if wh.GetState() == ECONN_STATE_SUSPEND {
						wh.SetState(ECONN_STATE_ACTIVE)
					} else if wh.GetState() == ECONN_STATE_DISCONNTCT {
						wh = nil
					}
				}
			}
			if wh == nil {
				guin = GenerateGuin(s.ServerId, int(conn.GetId()))
				wh = s.makeWormhole(guin, s.Wormholes, s.RoutePackHandle)
			}

			//将该连接绑定到wormhole,
			//并且connection的receivebytes将被wormhole接管
			//该函数将不会被该connection调用
			wh.AddConnection(conn, ECONN_TYPE_DATA)
			s.Wormholes.Add(wh)
			gts.Debug("has clients:", s.Wormholes.Length())

			fromType := EWormholeType(dp.Data[0])
			wh.SetFromType(fromType)

			data := make([]byte, len(s.udpAddr)+1)
			data[0] = byte(s.ServerType)
			copy(data[1:], []byte(s.udpAddr))

			//hello to client
			packet := &RoutePacket{
				Type: EPACKET_TYPE_HELLO,
				Guin: guin,
				//Data:   []byte{byte(s.ServerType)},
				Data: data,
			}
			wh.SendPacket(packet)

			wh.Init()

			/*
			   //hello udp addr to client
			   if len(s.udpAddr) > 0 {
			       packet.Type = EPACKET_TYPE_UDP_SERVER
			       packet.Data = []byte(s.udpAddr)
			       wh.SendPacket(packet)
			   }
			*/

			gts.Trace("server send back hello:,%s,%q", s.udpAddr, packet)
			break
		}
	}
}