Пример #1
0
 /// <summary>
 /// 主线程中调用
 /// </summary>
 /// <param name="packet"></param>
 public void HandlePacket(MsgPacket packet)
 {
     if (_packetHandler != null)
     {
         _packetHandler.Invoke(this.SocketClientType, packet);
     }
 }
Пример #2
0
 private void Run()
 {
     while (true)
     {
         Socket socket = _socketClient.Socket;
         try
         {
             //获取到数据包头
             int headCount = socket.Receive(packetHeadBuff, packetHeadBuff.Length, SocketFlags.None);
             if (headCount == 0)
             {
                 SocketTools.LogError("SocketClientType:" + _socketClient.SocketClientType + "\n" + "receive headData is 0");
                 LostConnect();
                 break;
             }
             else
             {
                 MsgPacket packet     = new MsgPacket(packetHeadBuff, packetLengthBuff, packetIDBuff, packetStatusBuff);
                 int       bodyLength = packet.Length - MsgPacket.PacketHeadSize;
                 if (bodyLength < 0)
                 {
                     SocketTools.LogError("SocketClientType:" + _socketClient.SocketClientType + "\n" + "packet body data is <0");
                     LostConnect();
                     break;
                 }
                 byte[] buff = zeroBuff;
                 if (bodyLength != 0)
                 {
                     buff = new byte[bodyLength];
                     int bodyCount = socket.Receive(buff, buff.Length, SocketFlags.None);
                     if (bodyCount == 0)
                     {
                         SocketTools.LogError("SocketClientType:" + _socketClient.SocketClientType + "\n" + "receive bodyData is 0");
                         LostConnect();
                         break;
                     }
                 }
                 Type type = _socketClient.SocketClientMgr.GetProtoType(packet.ID);
                 packet.Deserialize(type, buff, _stream);
                 lock (_queuePacket)
                 {
                     _queuePacket.Enqueue(packet);
                 }
             }
         }
         catch (Exception e)
         {
             SocketTools.LogError("SocketClientType:" + _socketClient.SocketClientType + "\n" + e.ToString());
             LostConnect();
             break;
         }
     }
 }
        public void SendMsg(SocketClientType socketClientType, ushort ID, byte[] buff)
        {
            SocketClient socketClient;

            _map.TryGetValue(socketClientType, out socketClient);
            if (socketClient != null && socketClient.Status == SocketStatus.Connected)
            {
                MsgPacket packet = new MsgPacket(ID, buff);
                socketClient.SendPacket(packet);
            }
            else
            {
                SocketTools.LogError("socketClientType:" + socketClientType + " is not connected!");
            }
        }
        private void PacketHandler(SocketClientType socketClientType, MsgPacket packet)
        {
            Dictionary <ushort, List <MsgPacketCallback> > map;

            _mapListener.TryGetValue(socketClientType, out map);
            if (map == null)
            {
                return;
            }
            List <MsgPacketCallback> list;

            map.TryGetValue(packet.ID, out list);
            if (list == null)
            {
                return;
            }
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Invoke(packet);
            }
            //SocketTools.Log(string.Format("receive,packetID:{0} packetStatus:{1} packetData:{2}", packet.ID, packet.Status, ((Msg.Info)packet.ProtoBufObj).msg));
        }
Пример #5
0
 /// <summary>
 /// 主线程中调用
 /// </summary>
 public void Update()
 {
     lock (this._queuePacket)
     {
         while (_queuePacket.Count > 0)
         {
             _queuePacketHandler.Enqueue(_queuePacket.Dequeue());
         }
     }
     while (_queuePacketHandler.Count > 0)
     {
         MsgPacket packet = _queuePacketHandler.Dequeue();
         if (packet != null)
         {
             _socketClient.HandlePacket(packet);
         }
         else
         {
             _socketClient.Disconnect();
         }
     }
 }
Пример #6
0
 public void SendPacket(MsgPacket packet)
 {
     this._sender.SendMsg(packet);
 }