Ejemplo n.º 1
0
 void OnRecievePacket(IAsyncResult result)
 {
     try {
         Socket socket = result.AsyncState as Socket;
         int    len    = socket.EndReceive(result);
         if (len != packetSize)
         {
             throw new OverflowException("packet size not equal!");
         }
         PostNotification(() => {
             int typeid = PacketHelper.UnpackHeader(buffer);
             if (!handlerMap.ContainsKey(typeid))
             {
                 Debug.LogWarning("type id not registered: " + typeid.ToString());
                 return;
             }
             handlerMap[typeid](typeid, buffer);
         });
         //recieve next packet
         RecievePacket(socket);
     } catch (SocketException ex) {
         Close();
         PostNotification(() => { networkErrorCallback(ex.Message); });
         Debug.LogException(ex);
     } catch (Exception ex) {
         //pass
     }
 }
Ejemplo n.º 2
0
 void OnRecieveHeader(IAsyncResult result)
 {
     try {
         Socket socket  = result.AsyncState as Socket;
         int    recvLen = socket.EndReceive(result);
         if (recvLen == 0)
         {
             throw new SocketException(10054);
         }
         packetSize = PacketHelper.UnpackHeader(header);
         buffer     = new byte[packetSize];
         socket.BeginReceive(buffer, 0, packetSize, SocketFlags.None, OnRecievePacket, socket);
     } catch (SocketException ex) {
         Close();
         PostNotification(() => { networkErrorCallback(ex.Message); });
         Debug.LogException(ex);
     } catch (Exception ex) {
         //pass
     }
 }