protected override void OnClientConnect(IAsyncResult result)
        {
            try
            {
                IClientMetadata state;
                int             id;

                lock (ConnectedClients)
                {
                    id = !ConnectedClients.Any() ? 1 : ConnectedClients.Keys.Max() + 1;

                    state = new ClientMetadata(((Socket)result.AsyncState).EndAccept(result), id);
                    CanAcceptConnections.Set();

                    ConnectedClients.Add(id, state);
                }

                //If the server shouldn't accept the IP do nothing.
                if (!IsConnectionAllowed(state))
                {
                    Log("A blacklisted ip tried to connect to the server: ipv4:" + state.RemoteIPv4 + " ipv6: " + state.RemoteIPv6);
                    lock (ConnectedClients)
                    {
                        ConnectedClients.Remove(id);
                    }
                    return;
                }

                Task.Run(() =>
                {
                    var stream = new NetworkStream(state.Listener);

                    //Create SslStream
                    state.SslStream = new SslStream(stream, false, AcceptCertificate);

                    var success = Authenticate(state).Result;

                    if (success)
                    {
                        RaiseClientConnected(state);
                        Receive(state);
                    }
                    else
                    {
                        lock (ConnectedClients)
                        {
                            ConnectedClients.Remove(id);
                        }
                        Log("Unable to authenticate server.");
                    }
                }, new CancellationTokenSource(10000).Token);
            }
            catch (Exception ex)
            {
                CanAcceptConnections.Set();
                RaiseLog(ex);
                RaiseErrorThrown(ex);
            }
        }
 public static bool AddClient(Client client)
 {
     if (!ConnectedClients.Any(tmp_client => tmp_client.ip == client.ip))
     {
         ConnectedClients.Add(client);
         return(true);
     }
     else
     {
         return(false);
     }
 }
        protected override void OnClientConnect(IAsyncResult result)
        {
            if (Token.IsCancellationRequested)
            {
                return;
            }

            CanAcceptConnections.Set();
            try
            {
                ISocketState state;

                lock (ConnectedClients)
                {
                    var id = !ConnectedClients.Any() ? 1 : ConnectedClients.Keys.Max() + 1;

                    state = new SocketState(((Socket)result.AsyncState).EndAccept(result), id);


                    //If the server shouldn't accept the IP do nothing.
                    if (!IsConnectionAllowed(state))
                    {
                        return;
                    }

                    var client = ConnectedClients.FirstOrDefault(x => x.Value == state);

                    if (client.Value == state)
                    {
                        id = client.Key;
                        ConnectedClients.Remove(id);
                        ConnectedClients.Add(id, state);
                    }
                    else
                    {
                        ConnectedClients.Add(id, state);
                    }

                    ClientConnectedInvoke(id, state);
                }

                StartReceiving(state);
            }
            catch (ObjectDisposedException ode)
            {
                InvokeErrorThrown(ode);
            }
            catch (SocketException se)
            {
                this.InvokeErrorThrown(se);
            }
        }
Beispiel #4
0
        protected override void OnClientConnect(IAsyncResult result)
        {
            CanAcceptConnections.Set();
            try
            {
                ISocketState state;
                int          id;

                lock (ConnectedClients)
                {
                    id = !ConnectedClients.Any() ? 1 : ConnectedClients.Keys.Max() + 1;

                    state = new SocketState(((Socket)result.AsyncState).EndAccept(result), id);
                }

                //If the server shouldn't accept the IP do nothing.
                if (!IsConnectionAllowed(state))
                {
                    return;
                }

                var stream = new NetworkStream(state.Listener);

                //Create SslStream
                state.SslStream = new SslStream(stream, false, new RemoteCertificateValidationCallback(AcceptCertificate));

                Task.Run(() =>
                {
                    var success = Authenticate(state).Result;

                    if (success)
                    {
                        lock (ConnectedClients)
                        {
                            ConnectedClients.Add(id, state);
                        }
                        ClientConnectedInvoke(id, state);
                        StartReceiving(state);
                    }
                    else
                    {
                        throw new AuthenticationException("Unable to authenticate server.");
                    }
                }, new CancellationTokenSource(10000).Token);
            }
            catch (SocketException se)
            {
                throw new Exception(se.ToString());
            }
        }
Beispiel #5
0
        /// <summary>
        /// try to add a new client's mac address.
        /// </summary>
        /// <param name="deviceInfo">The new client's info.</param>
        /// <returns><c>true</c> if the client was added successfully, <c>false</c> if it was already registered.</returns>
        public bool TryAddClient(DeviceInfo deviceInfo)
        {
            if (ConnectedClients.Any(d => d.MacAddress == deviceInfo.MacAddress))
            {
                return(false);
            }

            lock (ConnectedClients)
            {
                ConnectedClients.Add(deviceInfo);
            }

            // Initialize the pending messages for this client
            if (!PendingMessages.ContainsKey(deviceInfo.MacAddress))
            {
                lock (PendingMessages)
                {
                    PendingMessages.Add(deviceInfo.MacAddress, new Queue <ChatMessage>());
                }
            }

            return(true);
        }