private void ReceivePayloadStreamCallback(IAsyncResult ar)
        {
            try {
                //Attempt to finish the async read.
                var read = socket.EndReceive(ar);

                //Same as above
                if (read <= 0)
                {
                    throw new SocketException((int)SocketError.ConnectionAborted);
                }

                CheckFlood();
                //Subtract what we read from the payload size.
                dataExpected -= read;

                //Write the data to the payload stream.
                payloadStream.Write(buffer, 0, read);

                ReceiveProgressChanged?.Invoke(this, (int)payloadStream.Length, dataExpected);
                //Update global how many bytes we have received.
                TotalBytesReceived += read;

                //If there is more data to receive, keep the loop going.
                if (dataExpected > 0)
                {
                    //See how much data we need to receive like the initial receive.
                    int receiveSize = dataExpected > ClientOptions.BufferSize ?
                                      ClientOptions.BufferSize : dataExpected;

                    socket.BeginReceive(buffer, 0, receiveSize, 0,
                                        ReceivePayloadStreamCallback, null);
                    //If we received everything
                }
                else
                {
                    //Close the payload stream
                    payloadStream.Close();

                    //Get the full payload
                    byte[] payload = payloadStream.ToArray();

                    //Dispose the stream
                    payloadStream = null;

                    //Start receiving size header again
                    BeginReceive();

                    //Call the event method
                    PacketReceived?.Invoke(this, new PacketReceivedEventArgs(payload));
                }
            } catch (NullReferenceException) {
                return;
            } catch (ObjectDisposedException) {
                return;
            } catch (Exception ex) {
                HandleDisconnect(ex);
            }
        }
Exemple #2
0
        private void ReceivePayloadCallBack(IAsyncResult ar)
        {
            try {
                //Attempt to finish the async read.
                var read = socket.EndReceive(ar);

                //Same as above
                if (read <= 0)
                {
                    throw new SocketException((int)SocketError.ConnectionAborted);
                }

                CheckFlood();

                //Subtract what we read from the payload size.
                payloadSize -= read;

                //Write the data to the payload stream.
                payloadStream.Write(buffer, 0, read);

                ReceiveProgressChanged?.Invoke(this, (int)payloadStream.Length, totalPayloadSize);

                //If there is more data to receive, keep the loop going.
                if (payloadSize > 0)
                {
                    //See how much data we need to receive like the initial receive.
                    int receiveSize = payloadSize > BUFFER_SIZE ? BUFFER_SIZE :
                                      payloadSize;
                    socket.BeginReceive(buffer, 0, receiveSize, 0,
                                        ReceivePayloadCallBack, null);
                }
                else //If we received everything
                {
                    //Close the payload stream
                    payloadStream.Close();

                    //Get the full payload
                    byte[] payload = payloadStream.ToArray();

                    //Dispose the stream
                    payloadStream = null;

                    //Start reading
                    BeginRead();
                    //Call the event method
                    PacketReceived?.Invoke(this, new PacketReceivedArgs(payload));
                }
            }
            catch (NullReferenceException) { return; }
            catch (ObjectDisposedException) { return; }
            catch (Exception ex) {
                HandleDisconnect(ex, false);
            }
        }
        private void ReceivePayloadBufferedCallback(IAsyncResult ar)
        {
            try {
                //Attempt to finish the async read.
                var read = socket.EndReceive(ar);

                //Same as above
                if (read <= 0)
                {
                    throw new SocketException((int)SocketError.ConnectionAborted);
                }

                CheckFlood();

                //Increment how much data we have read
                dataRead += read;

                //Update global how many bytes we have received.
                TotalBytesReceived += read;

                ReceiveProgressChanged?.Invoke(this, dataRead, dataExpected);

                //If there is more data to receive, keep the loop going.
                if (dataRead < dataExpected)
                {
                    socket.BeginReceive(buffer, dataRead, dataExpected - dataRead, 0,
                                        ReceivePayloadBufferedCallback, null);

                    //If we received everything
                }
                else
                {
                    //Reset dataRead for receiving size
                    dataRead = 0;

                    byte[] packet = new byte[dataExpected];
                    Buffer.BlockCopy(buffer, 0, packet, 0, packet.Length);

                    //Call the event method
                    PacketReceived?.Invoke(this, new PacketReceivedEventArgs(packet));

                    //Start receiving the size again
                    BeginReceive();
                }
            } catch (NullReferenceException) {
                return;
            } catch (ObjectDisposedException) {
                return;
            } catch (Exception ex) {
                HandleDisconnect(ex);
            }
        }
 private void TcpSocket_ReceiveProgressChanged(TcpSocket sender, int Received, int BytesToReceive)
 {
     ReceiveProgressChanged?.Invoke(sender, Received, BytesToReceive);
 }
Exemple #5
0
 private void ProtocolClient_ReceiveProgressChanged(ProtocolClient sender, int bytesReceived, int bytesToReceive)
 {
     ReceiveProgressChanged?.Invoke(sender, bytesReceived, bytesToReceive);
 }