Пример #1
0
        public NetworkReadResult BeginReading()
        {
            this.readIndex = 0;
            try
            {
                this.totalLength = this.udpClient.ReceiveFrom(this.buffer, SocketFlags.None, ref this.remoteEndPoint);
                if (this.totalLength == 0)
                {
                    return(NetworkReadResult.CreateDisconnected());
                }

                return(NetworkReadResult.CreateSuccess((IPEndPoint)this.remoteEndPoint));
            }
            catch (SocketException e)
            {
                return(NetworkReadResult.CreateError(e.SocketErrorCode));
            }
        }
Пример #2
0
        private NetworkReadResult ReadFromNetwork(ref bool readFromNetwork)
        {
            int         receivedLength;
            SocketError socketError;

            try
            {
                receivedLength = this.tcpClient.Receive(
                    this.buffer,
                    this.writeIndex,
                    this.buffer.Length - this.writeIndex,
                    SocketFlags.None,
                    out socketError);
            }
            catch (SocketException exception)
            {
                return(NetworkReadResult.CreateError(exception.SocketErrorCode));
            }
            catch (Exception exception)
            {
                this.logger?.Error(
                    $"Unexpected error while receiving TCP data from {this.tcpClient?.RemoteEndPoint}: {exception}");
                return(NetworkReadResult.CreateError(SocketError.SocketError));
            }

            if (socketError != SocketError.Success)
            {
                return(NetworkReadResult.CreateError(socketError));
            }

            if (receivedLength == 0)
            {
                return(NetworkReadResult.CreateDisconnected());
            }

            this.writeIndex += receivedLength;
            readFromNetwork  = false;
            return(NetworkReadResult.CreateSuccess());
        }