Пример #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());
        }
Пример #3
0
        // On the first iteration, this function will read as many data from the network as available
        // On the following iterations, it depends on the yet received data:
        // - Not enough bytes for header? Read from network again
        // - Packet is not complete and there is space left in the buffer? Read from network again
        // - Packet is not complete and no space available? Return and wait for XDR read
        // - Packet is complete and XDR read is not complete? Return and wait for XDF read
        // - Packet and XDR read is complete? Read next header. Or finish if previous packet was the last packet
        private NetworkReadResult FillBuffer()
        {
            bool readFromNetwork = false;

            while (true)
            {
                if (this.packetState == PacketState.Complete)
                {
                    return(NetworkReadResult.CreateSuccess());
                }

                if (this.packetState == PacketState.Header)
                {
                    this.ReadHeader(ref readFromNetwork);
                }

                if (this.packetState == PacketState.Body)
                {
                    if (this.ReadBody(ref readFromNetwork))
                    {
                        return(NetworkReadResult.CreateSuccess());
                    }
                }

                if (readFromNetwork)
                {
                    NetworkReadResult networkReadResult = this.ReadFromNetwork(ref readFromNetwork);
                    if (networkReadResult.HasError || networkReadResult.IsDisconnected)
                    {
                        return(networkReadResult);
                    }
                }

                this.ShiftData();
            }
        }