示例#1
0
        protected internal override void Receive(IClientMetadata state, int offset = 0)
        {
            try
            {
                state.SslStream = _sslStream;

                if (offset > 0)
                {
                    state.UnhandledBytes = state.Buffer;
                }

                if (state.Buffer.Length < state.BufferSize)
                {
                    state.ChangeBuffer(new byte[state.BufferSize]);
                    if (offset > 0)
                    {
                        Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length);
                    }
                }

                _mreRead.WaitOne();
                _mreRead.Reset();
                state.SslStream.BeginRead(state.Buffer, offset, state.BufferSize - offset, ReceiveCallback, state);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
示例#2
0
        protected internal override void Receive(IClientMetadata state, int offset = 0)
        {
            try
            {
                var firstRead = true;

                while (!Token.IsCancellationRequested)
                {
                    state.MreReceiving.WaitOne();
                    state.MreTimeout.Reset();
                    state.MreReceiving.Reset();

                    if (!firstRead)
                    {
                        offset = state.Buffer.Length;
                    }

                    if (offset > 0)
                    {
                        state.UnhandledBytes = state.Buffer;
                    }

                    if (state.Buffer.Length < state.BufferSize)
                    {
                        state.ChangeBuffer(new byte[state.BufferSize]);
                        if (offset > 0)
                        {
                            Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length);
                        }
                    }

                    firstRead = false;

                    state.Listener.BeginReceive(state.Buffer, offset, state.BufferSize - offset, SocketFlags.None, ReceiveCallback, state);
                    if (Timeout.TotalMilliseconds > 0 && !state.MreTimeout.WaitOne(Timeout, false))
                    {
                        throw new SocketException((int)SocketError.TimedOut);
                    }
                }
            }
            catch (SocketException se) {
                if (se.SocketErrorCode == SocketError.TimedOut)
                {
                    Log("Client with id: " + state.Id + " and guid: " + state.Guid + " has timed out.");
                    RaiseClientDisconnected(state, DisconnectReason.Timeout);
                }
                else
                {
                    RaiseErrorThrown(se);
                }
                Close(state.Id);
            }
            catch (Exception ex)
            {
                RaiseErrorThrown(ex);
                Close(state.Id);
            }
        }
        private async Task <bool> Authenticate(IClientMetadata state)
        {
            try
            {
                SslProtocols protocol;

                switch (_tlsProtocol)
                {
                case TlsProtocol.Tls10:
                    protocol = SslProtocols.Tls;
                    break;

                case TlsProtocol.Tls11:
                    protocol = SslProtocols.Tls11;
                    break;

                case TlsProtocol.Tls12:
                    protocol = SslProtocols.Tls12;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                await state.SslStream.AuthenticateAsServerAsync(_serverCertificate, true, protocol, false);

                if (!state.SslStream.IsEncrypted)
                {
                    throw new Exception("Stream from client " + state.Id + " is not encrypted.");
                }

                if (!state.SslStream.IsAuthenticated)
                {
                    throw new Exception("Stream from client " + state.Id + " not authenticated.");
                }

                if (MutualAuthentication && !state.SslStream.IsMutuallyAuthenticated)
                {
                    throw new AuthenticationException("Failed to mutually authenticate.");
                }

                RaiseAuthSuccess(state);
                return(true);
            }
            catch (Exception ex)
            {
                RaiseAuthFailed(state);
                RaiseErrorThrown(ex);
                RaiseLog("Failed to authenticate ssl certificate.");
                return(false);
            }
        }
        protected internal override void Receive(IClientMetadata state, int offset = 0)
        {
            try
            {
                var firstRead = true;

                while (!Token.IsCancellationRequested)
                {
                    state.MreReceiving.WaitOne();
                    state.MreTimeout.Reset();
                    state.MreReceiving.Reset();

                    if (!firstRead && state.Buffer.Length != state.BufferSize)
                    {
                        offset = state.Buffer.Length;
                    }

                    if (offset > 0)
                    {
                        state.UnhandledBytes = state.Buffer;
                    }

                    if (state.Buffer.Length < state.BufferSize)
                    {
                        state.ChangeBuffer(new byte[state.BufferSize]);
                        if (offset > 0)
                        {
                            Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length);
                        }
                    }

                    firstRead = false;

                    var sslStream = state.SslStream;
                    state.MreRead.WaitOne();
                    state.MreRead.Reset();
                    sslStream.BeginRead(state.Buffer, offset, state.BufferSize - offset, ReceiveCallback, state);
                    if (Timeout.TotalMilliseconds > 0 && !state.MreTimeout.WaitOne(Timeout, false))
                    {
                        throw new SocketException((int)SocketError.TimedOut);
                    }
                }
            }
            catch (Exception ex)
            {
                Log(ex);
                Log("Error trying to receive from client " + state.Id);
                throw new Exception(ex.Message, ex);
            }
        }
        //Check if the server should allow the client that is attempting to connect.
        internal bool IsConnectionAllowed(IClientMetadata state)
        {
            if (WhiteList.Count > 0)
            {
                return(CheckWhitelist(state.RemoteIPv4) || CheckWhitelist(state.RemoteIPv6));
            }

            if (BlackList.Count > 0)
            {
                return(!CheckBlacklist(state.RemoteIPv4) && !CheckBlacklist(state.RemoteIPv6));
            }

            return(true);
        }
示例#6
0
        protected internal override void Receive(IClientMetadata state, int offset = 0)
        {
            bool firstRead = true;

            while (!Token.IsCancellationRequested)
            {
                _mreReceiving.WaitOne();

                if (_sslStream == null)
                {
                    if (IsRunning == true)
                    {
                        RaiseDisconnected();
                    }
                    Close();
                    throw new SocketException((int)SocketError.NotConnected);
                }

                _mreReceiving.Reset();

                state.SslStream = _sslStream;

                if (!firstRead && state.Buffer.Length != state.BufferSize)
                {
                    offset = state.Buffer.Length;
                }

                if (offset > 0)
                {
                    state.UnhandledBytes = state.Buffer;
                }

                if (state.Buffer.Length < state.BufferSize)
                {
                    state.ChangeBuffer(new byte[state.BufferSize]);
                    if (offset > 0)
                    {
                        Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length);
                    }
                }

                firstRead = false;

                _mreRead.WaitOne();
                _mreRead.Reset();
                state.SslStream.BeginRead(state.Buffer, offset, state.BufferSize - offset, ReceiveCallback, state);
            }
        }
示例#7
0
        protected internal override void Receive(IClientMetadata state, int offset = 0)
        {
            try
            {
                bool firstRead = true;

                while (!Token.IsCancellationRequested)
                {
                    _mreReceiving.WaitOne();
                    _mreReceiving.Reset();

                    state.SslStream = _sslStream;

                    if (!firstRead && state.Buffer.Length != state.BufferSize)
                    {
                        offset = state.Buffer.Length;
                    }

                    if (offset > 0)
                    {
                        state.UnhandledBytes = state.Buffer;
                    }

                    if (state.Buffer.Length < state.BufferSize)
                    {
                        state.ChangeBuffer(new byte[state.BufferSize]);
                        if (offset > 0)
                        {
                            Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length);
                        }
                    }

                    firstRead = false;

                    _mreRead.WaitOne();
                    _mreRead.Reset();
                    state.SslStream.BeginRead(state.Buffer, offset, state.BufferSize - offset, ReceiveCallback, state);
                }
            }
            catch (SocketException se) {
                Log("Socket error thrown: " + se.Message);
            }
            catch (Exception ex)
            {
                RaiseErrorThrown(ex);
            }
        }
        protected internal override void Receive(IClientMetadata state, int offset = 0)
        {
            if (offset > 0)
            {
                state.UnhandledBytes = state.Buffer;
            }

            if (state.Buffer.Length < state.BufferSize)
            {
                state.ChangeBuffer(new byte[state.BufferSize]);
                if (offset > 0)
                {
                    Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length);
                }
            }

            state.Listener.BeginReceive(state.Buffer, offset, state.BufferSize - offset, SocketFlags.None, ReceiveCallback, state);
        }
        protected internal override void Receive(IClientMetadata state, int offset = 0)
        {
            bool firstRead = true;

            while (!Token.IsCancellationRequested)
            {
                _mreReceiving.WaitOne();

                if (state.Listener == null || Listener == null)
                {
                    throw new SocketException((int)SocketError.NotConnected);
                }

                _mreReceiving.Reset();

                if (!firstRead)
                {
                    offset = state.Buffer.Length;
                }

                if (offset > 0)
                {
                    state.UnhandledBytes = state.Buffer;
                }

                if (state.Buffer.Length < state.BufferSize)
                {
                    state.ChangeBuffer(new byte[state.BufferSize]);
                    if (offset > 0)
                    {
                        Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length);
                    }
                }

                firstRead = false;

                state.Listener.BeginReceive(state.Buffer, offset, state.BufferSize - offset, SocketFlags.None, this.ReceiveCallback, state);
            }
        }
示例#10
0
 public void Dispose()
 {
     Data  = null;
     State = null;
 }
示例#11
0
 public MessageWrapper(byte[] data, IClientMetadata state, bool partial)
 {
     Data    = data;
     State   = state;
     Partial = partial;
 }
        protected internal override void Receive(IClientMetadata state, int offset = 0)
        {
            try
            {
                var firstRead = true;

                while (!Token.IsCancellationRequested)
                {
                    state.MreTimeout.Reset();
                    state.MreReceiving.WaitOne();
                    state.MreReceiving.Reset();

                    if (!firstRead && state.Buffer.Length != state.BufferSize)
                    {
                        offset = state.Buffer.Length;
                    }

                    if (offset > 0)
                    {
                        state.UnhandledBytes = state.Buffer;
                    }

                    if (state.Buffer.Length < state.BufferSize)
                    {
                        state.ChangeBuffer(new byte[state.BufferSize]);
                        if (offset > 0)
                        {
                            Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length);
                        }
                    }

                    firstRead = false;

                    var sslStream = state.SslStream;
                    state.MreRead.WaitOne();
                    state.MreRead.Reset();
                    sslStream.BeginRead(state.Buffer, offset, state.BufferSize - offset, ReceiveCallback, state);
                    if (Timeout.TotalMilliseconds > 0 && !state.MreTimeout.WaitOne((int)Timeout.TotalMilliseconds, false))
                    {
                        throw new SocketException((int)SocketError.TimedOut);
                    }
                }
            }
            catch (SocketException se) {
                if (se.SocketErrorCode == SocketError.TimedOut)
                {
                    Log("Socket has timed out.");
                    RaiseClientTimedOut(state);
                }
                else
                {
                    RaiseErrorThrown(se);
                }
            }
            catch (Exception ex)
            {
                Log("Error trying to receive from client with id:" + state.Id + " and Guid: " + state.Guid);
                RaiseErrorThrown(ex);
            }
            finally {
                Log("Closing socket from client with id:" + state.Id + " and Guid: " + state.Guid);
                Close(state.Id);
                Log("Socket from client with id:" + state.Id + " and Guid: " + state.Guid + " has been closed.");
            }
        }