Exemple #1
0
 public void readBuffer(byte[] buffer, int bufferSize, int readLen)
 {
     if (!readCheck(readLen))
     {
         return;
     }
     BinaryUtility.readBytes(mBuffer, ref mIndex, buffer, -1, bufferSize, readLen);
 }
Exemple #2
0
 public override void readFromBuffer(byte[] buffer, ref int index)
 {
     BinaryUtility.readBytes(buffer, ref index, mValue);
 }
Exemple #3
0
    // 接收Socket消息
    protected void receiveSocket()
    {
        IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 0);
        EndPoint   ep       = (EndPoint)endpoint;

        while (mRun)
        {
            byte[] recvBuff = new byte[mMaxReceiveCount];
            int    nRecv    = mServerSocket.ReceiveFrom(recvBuff, ref ep);
            if (nRecv < 0)
            {
                UnityUtility.logInfo("网络连接中断!");
                return;
            }
            else if (nRecv == 0)
            {
                UnityUtility.logInfo("已与服务器断开连接!");
                return;
            }
            int index = 0;
            while (true)
            {
                if (index + sizeof(short) > nRecv)
                {
                    break;
                }
                // 读取包类型(short)
                PACKET_TYPE type = (PACKET_TYPE)BinaryUtility.readShort(recvBuff, ref index);
                // 客户端接收到的必须是SC类型的
                if (type <= PACKET_TYPE.PT_SC_MIN || type >= PACKET_TYPE.PT_SC_MAX)
                {
                    UnityUtility.logInfo("packet type error : " + type);
                    break;
                }
                int packetSize = mSocketFactoryManager.getPacketSize(type);
                if (packetSize >= 0)
                {
                    // 读取消息长度(short)
                    short realDataSize = BinaryUtility.readShort(recvBuff, ref index);
                    if (realDataSize != packetSize)
                    {
                        UnityUtility.logError("error : wrong packet size! type : " + type + "readed : " + realDataSize + ", packet size : " + packetSize, false);
                        break;
                    }
                    if (packetSize > nRecv - sizeof(short))
                    {
                        UnityUtility.logError("error : wrong packet data! packet : " + type + ", need size : " + packetSize + ", receive size : " + (nRecv - sizeof(PACKET_TYPE)), false);
                        break;
                    }
                    else
                    {
                        UnityUtility.logInfo("receive : client : " + endpoint.Address.ToString() + ", type : " + type + ", size : " + packetSize);
                    }
                    lock (mRecieveList)
                    {
                        if (packetSize != 0)
                        {
                            byte[] recvData = new byte[packetSize];
                            // 读取消息内容(byte[])
                            BinaryUtility.readBytes(recvBuff, ref index, recvData);
                            mRecieveList.Add(new INPUT_ELEMENT(type, recvData));
                        }
                        else
                        {
                            byte[] recvData = null;
                            mRecieveList.Add(new INPUT_ELEMENT(type, recvData));
                        }
                        UnityUtility.logInfo("receive : type : " + type + ", count : " + nRecv + ", client ip : " + endpoint.Address.ToString());
                    }
                    // 该段消息内存已经解析完了
                    if (index == nRecv)
                    {
                        break;
                    }
                }
                // 如果消息解析发生错误,则不再解析
                else
                {
                    break;
                }
            }
            Thread.Sleep(10);
        }
    }
Exemple #4
0
 // 接收Socket消息
 protected bool receiveSocket()
 {
     try
     {
         IPEndPoint endpoint = null;
         if (endpoint == null)
         {
             endpoint = new IPEndPoint(IPAddress.Any, 0);
         }
         EndPoint ep       = (EndPoint)endpoint;
         byte[]   recvBuff = null;
         if (recvBuff == null)
         {
             recvBuff = new byte[mMaxReceiveCount];
         }
         int nRecv = mServerSocket.ReceiveFrom(recvBuff, ref ep);
         if (nRecv <= 0)
         {
             CommandSocketManagerNetState cmd = newCmd(out cmd, true, true);
             cmd.mNetState = NET_STATE.NS_NET_CLOSE;
             pushDelayCommand(cmd, this);
             return(false);
         }
         int index = 0;
         while (true)
         {
             if (index + sizeof(short) > nRecv)
             {
                 break;
             }
             // 读取包类型(short)
             PACKET_TYPE type = (PACKET_TYPE)BinaryUtility.readShort(recvBuff, ref index);
             // 客户端接收到的必须是SC类型的
             if (type <= PACKET_TYPE.PT_SC_MIN || type >= PACKET_TYPE.PT_SC_MAX)
             {
                 UnityUtility.logError("packet type error : " + type);
                 break;
             }
             int packetSize = mSocketFactory.getPacketSize(type);
             if (packetSize >= 0)
             {
                 // 读取消息长度(short)
                 short realDataSize = BinaryUtility.readShort(recvBuff, ref index);
                 if (realDataSize != packetSize)
                 {
                     UnityUtility.logError("error : wrong packet size! type : " + type + "readed : " + realDataSize + ", packet size : " + packetSize, false);
                     break;
                 }
                 if (packetSize > nRecv - sizeof(short))
                 {
                     UnityUtility.logError("error : wrong packet data! packet : " + type + ", need size : " + packetSize + ", receive size : " + (nRecv - sizeof(PACKET_TYPE)), false);
                     break;
                 }
                 mReceiveLock.waitForUnlock();
                 if (packetSize != 0)
                 {
                     byte[] recvData = new byte[packetSize];
                     // 读取消息内容(byte[])
                     BinaryUtility.readBytes(recvBuff, ref index, recvData);
                     mRecieveList.Add(new INPUT_ELEMENT(type, recvData));
                 }
                 else
                 {
                     byte[] recvData = null;
                     mRecieveList.Add(new INPUT_ELEMENT(type, recvData));
                 }
                 mReceiveLock.unlock();
                 // 该段消息内存已经解析完了
                 if (index == nRecv)
                 {
                     break;
                 }
             }
             // 如果消息解析发生错误,则不再解析
             else
             {
                 break;
             }
         }
     }
     catch (SocketException)
     {
         CommandSocketManagerNetState cmd = newCmd(out cmd, true, true);
         cmd.mNetState = NET_STATE.NS_SERVER_CLOSE;
         pushDelayCommand(cmd, this);
         return(false);
     }
     return(true);
 }