Ejemplo n.º 1
0
        private void Send(byte[] data, int length, bool skip_encryption)
        {
            if (_tcpClient == null || IsDisposed)
            {
                return;
            }

            if (_netStream == null || !_tcpClient.Connected)
            {
                return;
            }

            if (data != null && data.Length != 0 && length > 0)
            {
                if (CUOEnviroment.PacketLog)
                {
                    LogPacket(data, length, true);
                }

                if (!skip_encryption)
                {
                    EncryptionHelper.Encrypt(_is_login_socket, ref data, ref data, length);
                }

                try
                {
                    _netStream.Write(data, 0, length);
                    _netStream.Flush();

                    Statistics.TotalBytesSent += (uint)length;
                    Statistics.TotalPacketsSent++;
                }
                catch (SocketException ex)
                {
                    Log.Error("socket error when sending:\n" + ex);
                    _logFile?.Write($"disconnection  -  error during writing to the socket buffer: {ex}");

                    Disconnect(ex.SocketErrorCode);
                }
                catch (Exception ex)
                {
                    if (ex.InnerException is SocketException socketEx)
                    {
                        Log.Error("socket error when sending:\n" + socketEx);

                        _logFile?.Write($"disconnection  -  error during writing to the socket buffer [2]: {socketEx}");
                        Disconnect(socketEx.SocketErrorCode);
                    }
                    else
                    {
                        Log.Error("fatal error when receiving:\n" + ex);

                        _logFile?.Write($"disconnection  -  error during writing to the socket buffer [3]: {ex}");

                        Disconnect();

                        throw;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void ProcessRecv()
        {
            if (IsDisposed || Status != ClientSocketStatus.Connected)
            {
                return;
            }

            if (!IsConnected && !IsDisposed)
            {
                Disconnect();

                return;
            }

            if (!_netStream.DataAvailable)
            {
                return;
            }

            int available = _tcpClient.Available;

            if (available <= 0)
            {
                return;
            }

            try
            {
                int received = _netStream.Read(_recvBuffer, 0, available);

                if (received > 0)
                {
                    Statistics.TotalBytesReceived += (uint)received;

                    byte[] buffer = _recvBuffer;

                    if (!_is_login_socket)
                    {
                        EncryptionHelper.Decrypt(ref buffer, ref buffer, received);
                    }

                    if (_isCompressionEnabled)
                    {
                        DecompressBuffer(ref buffer, ref received);
                    }

                    lock (_circularBuffer)
                    {
                        _circularBuffer.Enqueue(buffer, 0, received);
                    }

                    ExtractPackets();
                }
                else
                {
                    Log.Warn("Server sent 0 bytes. Closing connection");

                    _logFile?.Write($"disconnection  -  received {received} bytes from server");

                    Disconnect(SocketError.SocketError);
                }
            }
            catch (SocketException socketException)
            {
                Log.Error("socket error when receiving:\n" + socketException);

                _logFile?.Write($"disconnection  -  error while reading from socket: {socketException}");

                Disconnect(socketException.SocketErrorCode);
            }
            catch (Exception ex)
            {
                if (ex.InnerException is SocketException socketEx)
                {
                    Log.Error("socket error when receiving:\n" + socketEx);
                    _logFile?.Write($"disconnection  -  error while reading from socket [1]: {socketEx}");

                    Disconnect(socketEx.SocketErrorCode);
                }
                else
                {
                    Log.Error("fatal error when receiving:\n" + ex);
                    _logFile?.Write($"disconnection  -  error while reading from socket [2]: {ex}");

                    Disconnect();

                    throw;
                }
            }
        }
Ejemplo n.º 3
0
        private void ProcessRecv()
        {
            if (IsDisposed || Status != ClientSocketStatus.Connected)
            {
                return;
            }

            if (!IsConnected && !IsDisposed)
            {
                Disconnect();

                return;
            }

            int available = _socket.Available;

            if (available <= 0)
            {
                return;
            }

            const int SIZE_TO_READ = 4096;

            byte[] buffer = System.Buffers.ArrayPool <byte> .Shared.Rent(SIZE_TO_READ * 4 + 2);

            while (available > 0)
            {
                try
                {
                    int wanted   = Math.Min(SIZE_TO_READ, available);
                    int received = _socket.Receive(buffer, 0, wanted, SocketFlags.None, out var errorCode);

                    if (received > 0 && errorCode == SocketError.Success)
                    {
                        available -= received;

                        Statistics.TotalBytesReceived += (uint)received;

                        if (!_is_login_socket)
                        {
                            EncryptionHelper.Decrypt(buffer, buffer, received);
                        }

                        if (_isCompressionEnabled)
                        {
                            DecompressBuffer(buffer, ref received);
                        }

                        _circularBuffer.Enqueue(buffer.AsSpan(0, received));

                        ExtractPackets();
                    }
                    else
                    {
                        Log.Warn("Server sent 0 bytes. Closing connection");

                        _logFile?.Write($"disconnection  -  received {received} bytes from server. ErrorCode: {errorCode}");

                        Disconnect(SocketError.SocketError);

                        break;
                    }
                }
                catch (SocketException socketException)
                {
                    Log.Error("socket error when receiving:\n" + socketException);

                    _logFile?.Write($"disconnection  -  error while reading from socket: {socketException}");

                    Disconnect(socketException.SocketErrorCode);

                    break;
                }
                catch (Exception ex)
                {
                    if (ex.InnerException is SocketException socketEx)
                    {
                        Log.Error("socket error when receiving:\n" + socketEx);
                        _logFile?.Write($"disconnection  -  error while reading from socket [1]: {socketEx}");

                        Disconnect(socketEx.SocketErrorCode);

                        break;
                    }
                    else
                    {
                        Log.Error("fatal error when receiving:\n" + ex);
                        _logFile?.Write($"disconnection  -  error while reading from socket [2]: {ex}");

                        Disconnect();

                        throw;
                    }
                }
            }

            System.Buffers.ArrayPool <byte> .Shared.Return(buffer, false); // TODO: clear?
        }