Exemple #1
0
        // Endless Start reading loop
        private async void StartReading(Socket client)
        {
            var endpoint = client.RemoteEndPoint as IPEndPoint; // Get remote endpoint

            // Loop theoretically infinetly
            while (true)
            {
                try
                {
                    long size = await LeadingByteProcessor.ReadLeading(client).ConfigureAwait(false); // leading

                    byte[] bytes = new byte[size];
                    ArraySegment <byte> segment = new ArraySegment <byte>(bytes);
                    //TODO: Do something when receiving interrupts? Wait for client to come back?
                    // read until all data is read
                    int read = 0;
                    while (read < size)
                    {
                        long receive = size - read; // current buffer size
                        if (receive > ReceiveBufferSize)
                        {
                            receive = ReceiveBufferSize; // max size
                        }
                        ArraySegment <byte>
                        slice = segment.SliceEx(read, (int)receive);     // get buffered portion of array
                        read += await client.ReceiveAsync(slice, SocketFlags.None).ConfigureAwait(false);
                    }

                    if (read < 1)
                    {
                        throw new TransferException($"{read} bytes were read! " +
                                                    "Null bytes could mean a connection shutdown.");
                    }

                    var message = ZeroFormatterSerializer.Deserialize <T>(segment.Array);

                    ReceivedMessage?.Invoke(endpoint, message); // call event
                }
                catch (SocketException ex)
                {
                    Console.WriteLine(ex.ErrorCode);
                    bool success = DisconnectClient(endpoint); // try to disconnect
                    if (success)                               // Exit Reading loop once successfully disconnected
                    {
                        return;
                    }
                }
                catch (TransferException)
                {
                    // 0 read bytes = null byte
                    bool success = DisconnectClient(endpoint); // try to disconnect
                    if (success)
                    {
                        return;          // Exit Reading loop once successfully disconnected
                    }
                }
            } // Listen again after client connected
        }
Exemple #2
0
        // Endless Start reading loop
        private void StartReceiving()
        {
            // Loop theoretically infinetly
            while (true)
            {
                try
                {
                    // Read the leading "byte"
                    long size = LeadingByteProcessor.ReadLeading(Socket).GetAwaiter().GetResult();

                    byte[] bytes = new byte[size];
                    ArraySegment <byte> segment = new ArraySegment <byte>(bytes);
                    //TODO: Decide whether to catch errors in buffer-loop and continue once fixed or cancel whole receive?
                    // read until all data is read
                    int read = 0;
                    while (read < size)
                    {
                        long receive = size - read; // current buffer size
                        if (receive > ReceiveBufferSize)
                        {
                            receive = ReceiveBufferSize; // max size
                        }

                        ArraySegment <byte>
                        slice = segment.SliceEx(read, (int)receive);     // get buffered portion of array
                        read += Socket.ReceiveAsync(slice, SocketFlags.None).GetAwaiter().GetResult();
                    }

                    var message = ZeroFormatterSerializer.Deserialize <T>(segment.Array);

                    ReceivedMessage?.Invoke(EndPoint, message); // call event
                }
                catch (ObjectDisposedException)
                {
                    return; // Socket was closed & disposed -> exit
                }
                catch (SocketException)
                {
                    ConnectionLost?.Invoke(EndPoint);
                    if (!AutoReconnect)
                    {
                        Reconnect().GetAwaiter().GetResult(); // Try reconnecting on an error, then continue receiving
                    }
                }
                // Listen again after client connected
            }
        }