Ejemplo n.º 1
0
        /// <summary>
        /// Tcp客户端连接断开,通知业务层
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="clientPoint"></param>
        public void OnTcpClientClosed(object sender, EndPoint clientPoint)
        {
            var        token = ClientConnectManager.GetToken(clientPoint.ToString());
            PacketHead head  = new PacketHead(ConnectType.Tcp, PacketMsgType.Closed);

            head.GameId   = token.GameId;
            head.ServerId = token.ServerId;
            head.Uid      = token.Uid;
            var session = GameSessionManager.GetSession(head.GameId, head.ServerId);

            if (session == null)
            {
                return;
            }
            head.Address    = session.GameAddress;
            head.EnableGzip = false;
            PacketMessage packet = new PacketMessage();

            packet.Head    = head;
            packet.Content = new byte[0];
            if (CheckConnected(head.Address))
            {
                OnSendToGame(head.Address, packet.ToByte());
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 从客户端接收数据
        /// </summary>
        /// <param name="clientAddress"></param>
        /// <param name="data"></param>
        public override void Receive(string clientAddress, byte[] data)
        {
            try
            {
                //解析头部信息,转换成统一的流格式:head[MsgType(1)+uid(4)+gameId(4)+serverId(4)+gzip(1)] + (len(4)+data)
                BufferReader reader      = new BufferReader(data);
                string       paramString = reader.ReadPacketString();
                paramString = HttpUtility.UrlDecode(paramString, Encoding.UTF8);
                int index = paramString.IndexOf("?d=");
                if (index != -1)
                {
                    index      += 3;
                    paramString = paramString.Substring(index, paramString.Length - index);
                }
                PacketMessage packet = ParsePacketMessage(clientAddress, paramString, ConnectType.Tcp);
                var           token  = new UserToken();
                token.GameId   = packet.Head.GameId;
                token.ServerId = packet.Head.ServerId;
                token.Uid      = packet.Head.Uid;
                ClientConnectManager.Push(clientAddress, token);

                if (ReceiveCompleted != null)
                {
                    //分发送到游戏服
                    byte[] packData   = packet.ToByte();
                    string successMsg = string.Format("{0}>>{1}接收到{2}字节!",
                                                      DateTime.Now.ToString("HH:mm:ss:ms"), clientAddress, data.Length);
                    ReceiveCompleted.BeginInvoke(clientAddress, packData, OnReceiveCompleted, successMsg);
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("Receive form client {0} error:{1}", clientAddress, ex);
            }
        }
Ejemplo n.º 3
0
        public void Request(HttpConnection connection, string clientAddress, byte[] data)
        {
            string paramString = string.Empty;

            try
            {
                paramString = Encoding.ASCII.GetString(data);
                PacketMessage packet = ParsePacketMessage(clientAddress, paramString, ConnectType.Http);
                packet.Head.SSID = connection.SSID.ToString("N");
                HttpConnectionManager.Push(connection);

                if (ReceiveCompleted != null)
                {
                    //分发送到游戏服
                    byte[] packData   = packet.ToByte();
                    string successMsg = string.Format("{0}>>{1}接收到{2}字节!",
                                                      DateTime.Now.ToString("HH:mm:ss:ms"), clientAddress, data.Length);
                    ReceiveCompleted.BeginInvoke(clientAddress, packData, OnReceiveCompleted, successMsg);
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("Receive form http request:{0} error:{1}", paramString, ex);
            }
        }
Ejemplo n.º 4
0
 private void OnSocketClosing(object sender, EndPoint gamePoint)
 {
     if (sender is byte[])
     {
         PacketMessage packet = PacketMessage.Parse(sender as byte[]);
         PacketMessage error  = PacketMessage.CreateError(packet.Head, MessageError.SystemError, LanguageHelper.GetLang().ServerBusy);
         OnSendToClient(packet.Head.Address, error.ToByte());
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// 从游戏服接收数据
        /// </summary>
        /// <param name="gameAddress">游戏服的地址</param>
        /// <param name="data"></param>
        public override void Receive(string gameAddress, byte[] data)
        {
            try
            {
                PacketMessage packet  = PacketMessage.Parse(data);
                var           head    = packet.Head;
                GameSession   session = null;
                switch (head.MsgType)
                {
                case PacketMsgType.None:
                    //心跳包
                    session = GameSessionManager.GetSession(head.GameId, head.ServerId);
                    if (session != null)
                    {
                        session.GameAddress = gameAddress;
                        session.AccessTime  = DateTime.Now;
                        Console.WriteLine("{0}>>Connect to host of game server {1}-{2} from {3}", DateTime.Now.ToLongTimeString(), head.GameId, head.ServerId, gameAddress);
                    }
                    break;

                case PacketMsgType.Register:
                    session             = new GameSession(head.GameId, head.ServerId);
                    session.GameAddress = gameAddress;
                    session.AccessTime  = DateTime.Now;
                    GameSessionManager.Register(session);
                    break;

                case PacketMsgType.Push:
                    OnSendToClient(head.Address, data);
                    break;

                case PacketMsgType.SendTo:
                case PacketMsgType.Request:
                    //发送到其它通道
                    Send(head.Address, packet.ToByte());
                    break;

                case PacketMsgType.Broadcast:
                    //广播到客户端
                    var clientPointList = ClientConnectManager.FindAll(head.GameId, head.ServerId);
                    foreach (var endPoint in clientPointList)
                    {
                        OnSendToClient(endPoint.ToString(), data);
                    }
                    break;

                default:
                    throw new Exception(string.Format("The message type:{0} is not supported.", head.MsgType));
                }
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("Receive game {0} error:{1}", gameAddress, ex);
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Tcp客户端连接断开,通知业务层
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="clientPoint"></param>
 public void OnTcpClientClosed(object sender, EndPoint clientPoint)
 {
     var token = ClientConnectManager.GetToken(clientPoint.ToString());
     PacketHead head = new PacketHead(ConnectType.Tcp, PacketMsgType.Closed);
     head.GameId = token.GameId;
     head.ServerId = token.ServerId;
     head.Uid = token.Uid;
     var session = GameSessionManager.GetSession(head.GameId, head.ServerId);
     if (session == null)
     {
         return;
     }
     head.Address = session.GameAddress;
     head.EnableGzip = false;
     PacketMessage packet = new PacketMessage();
     packet.Head = head;
     packet.Content = new byte[0];
     if (CheckConnected(head.Address))
     {
         OnSendToGame(head.Address, packet.ToByte());
     }
 }