Пример #1
0
 private void StartListeningAsync()
 {
     Connected?.Invoke(serverIPEndPoint);
     while (true)
     {
         try{
             int    dataSize = TCPSegmentSizeFormater.ReceiveTCPSegmentSize(serverSocket).GetAwaiter().GetResult();
             byte[] data     = new byte[dataSize];
             ArraySegment <byte> dataSegment = new ArraySegment <byte>(data);
             int recievedData = 0;
             while (recievedData < dataSize)
             {
                 int lost = dataSize - recievedData;
                 if (lost > RecieveBufferLength)
                 {
                     lost = RecieveBufferLength;
                 }
                 ArraySegment <byte> tempSegment = dataSegment.SliceEx(recievedData, lost);
                 recievedData += serverSocket.ReceiveAsync(tempSegment, SocketFlags.None).GetAwaiter().GetResult();
             }
             RecieveDataEvent?.Invoke(dataSegment.Array, serverIPEndPoint);
         }catch (ObjectDisposedException) {
             return;
         }catch (SocketException) {
             ConnectionShutdown?.Invoke(serverIPEndPoint);
             if (!AutoReconnect)
             {
                 Reconnect().Wait();
             }
         }
     }
 }
Пример #2
0
        private async void StartReceivingAsync(Socket clientSocket)
        {
            IPEndPoint iPEndPoint = clientSocket.RemoteEndPoint as IPEndPoint;

            while (true)
            {
                try
                {
                    int dataSize = await TCPSegmentSizeFormater.ReceiveTCPSegmentSize(clientSocket);

                    byte[] data = new byte[dataSize];
                    ArraySegment <byte> dataSegment = new ArraySegment <byte>(data);
                    int recieveSize = 0;
                    while (recieveSize < dataSize)
                    {
                        int bufferSize = dataSize - recieveSize;
                        if (bufferSize > RecieveBufferLength)
                        {
                            bufferSize = RecieveBufferLength;
                        }
                        ArraySegment <byte> partData = dataSegment.SliceEx(recieveSize, bufferSize);
                        recieveSize += await clientSocket.ReceiveAsync(partData, SocketFlags.None).ConfigureAwait(false);
                    }
                    if (recieveSize < 1)
                    {
                        throw new TCPProtocolException("Receive transfer data was damaged.");
                    }
                    RecieveDataEvent?.Invoke(dataSegment.Array, iPEndPoint);
                }
                catch (SocketException)
                {
                    bool success = DisconnectClient(iPEndPoint);
                    if (success)
                    {
                        return;
                    }
                }
                catch (TransferException)
                {
                    bool success = DisconnectClient(iPEndPoint);
                    if (success)
                    {
                        return;
                    }
                }
            }
        }