Exemple #1
0
 private void NetProcess()
 {
     byte[] sendBuff = new byte[1024];
     byte[] recvBuff = new byte[1024];
     while (true)
     {
         do
         {
             if (clientSocket == null)
             {
                 break;
             }
             if (!clientSocket.poll(0, SelectMode.SelectRead))
             {
                 break;
             }
             int len = clientSocket.ReceiveFrom(recvBuff, ref serverPoint);
             if (len <= 0)
             {
                 break;
             }
             if (len < UdpConst.udpHeadByteAll)
             {
                 break;
             }
             int size = NetTool.BytesToInt8(ref recvBuff, 0);
             if (len < size + UdpConst.udpHeadByteAll)
             {
                 break;
             }
             UdpChunk c = new UdpChunk();
             c.size = NetTool.BytesToInt8(ref recvBuff, 0);
             c.type = NetTool.BytesToInt8(ref recvBuff, 1);
             c.seq  = NetTool.BytesToInt32(ref recvBuff, 2);
             c.ack  = NetTool.BytesToInt32(ref recvBuff, 6);
             Array.Copy(recvBuff, 10, c.buff, 0, c.size);
             Monitor.Enter(rudp);
             rudp.RecvBuffIn(c);
             Monitor.Exit(rudp);
         } while (false);
         do
         {
             if (clientSocket == null)
             {
                 break;
             }
             Monitor.Enter(rudp);
             UdpChunk c = rudp.SendBuffOut();
             Monitor.Exit(rudp);
             if (c == null)
             {
                 break;
             }
             if (!clientSocket.poll(0, SelectMode.SelectWrite))
             {
                 Monitor.Enter(rudp);
                 UdpChunk c = rudp.SendChunkForce(c);
                 Monitor.Exit(rudp);
                 break;
             }
             NetTool.Int8ToBytes(sendBuff, 0, c.size);
             NetTool.Int8ToBytes(sendBuff, 1, c.type);
             NetTool.Int32ToBytes(sendBuff, 2, c.seq);
             NetTool.Int32ToBytes(sendBuff, 6, c.ack);
             Array.Copy(c.buff, 0, sendBuff, 10, c.size);
             int dataLen = c.size + UdpConst.udpHeadByteAll;
             int len     = clientSocket.SendTo(sendBuff, dataLen, SocketFlags.None, serverPoint);
             if (len < dataLen)
             {
                 Monitor.Enter(rudp);
                 UdpChunk c = rudp.SendChunkForce(c);
                 Monitor.Exit(rudp);
             }
         } while (false);
         Thread.Sleep(10);
     }
 }