Exemplo n.º 1
0
        private void MonitorIncomingConnection()
        {
            listener.Start();
            while (true)
            {
                try
                {
                    if (Abort)
                    {
                        //Terminating Server and Monitoring Loop
                        listener.Stop();
                        Trace.WriteLine(Name + ": TCP Server Stopped.");
                        DisconnectAllClients();
                        return;
                    }

                    if (!listener.Pending())
                    {
                        Thread.Sleep(10);
                    }
                    else
                    {
                        System.Net.Sockets.TcpClient client = listener.AcceptTcpClient();
                        Trace.WriteLine(Name + ": New Connection Detected...");
                        TcpServerConnectEventArgs eArgs = new TcpServerConnectEventArgs()
                        {
                            Client = client
                        };
                        ClientConnecting?.Invoke(this, eArgs);

                        if (eArgs.Accept)
                        {
                            //Connection Accepted
                            TcpServerConnection newConnection = null;

                            newConnection = new TcpServerConnection(this, client, Certificate);
                            newConnection.MessageDelimiter = MessageDelimiter;
                            Trace.WriteLine(Name + ": Connection Accepted: Client = " + newConnection.ClientIPAddress);
                            newConnection.ClientDisconnected += OnClientDisconnected;
                            lock (ActiveConnections) { ActiveConnections.Add(newConnection); }
                            ClientConnected?.Invoke(this, new TcpServerEventArgs()
                            {
                                Client = newConnection
                            });
                        }
                        else
                        {
                            //Connection Refused
                            Trace.WriteLine(Name + ": Connection Rejected.");
                            client.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("[WARNING] " + Name + ": Exception raised from Connection Monitoring Thread!\n" + ex.Message);
                    continue;
                }
            }
        }
Exemplo n.º 2
0
        private void ConnectionCallback(IAsyncResult result)
        {
            TcpClient tcpClient = tcpListener.EndAcceptTcpClient(result);

            tcpListener.BeginAcceptTcpClient(ConnectionCallback, null);

            Debugger.Log(1, null, $"{nameof(SimpleServer)}: Client from {((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString()} asks to connect to the server from port {((IPEndPoint)tcpClient.Client.RemoteEndPoint).Port}\n");
            ClientConnecting?.Invoke(this, tcpClient);
        }
Exemplo n.º 3
0
        private void MonitorIncomingConnection()
        {
            listener.Start();
            while (true)
            {
                try
                {
                    if (Abort)
                    {
                        //Terminating Server and Monitoring Loop
                        listener.Stop();
                        Trace.WriteLine(Name + ": TCP Server Stopped.");
                        DisconnectAllClients();
                        return;
                    }

                    if (!listener.Pending())
                    {
                        Thread.Sleep(10);
                    }
                    else
                    {
                        System.Net.Sockets.TcpClient client = listener.AcceptTcpClient();
                        Trace.WriteLine(Name + ": New Connection Detected...");

                        if (MaxClients > 0)
                        {
                            //MAX Clients Restriction applied
                            if (Clients.Count >= MaxClients)
                            {
                                //Number of connection exceeds, reject incoming connection.
                                Trace.WriteLine(Name + ": Connection Rejected, exceed MAX allowed clients!");
                                byte[] msgBuffer = Encoding.ASCII.GetBytes("Connection Rejected, exceed MAX allowed clients!\r\n");
                                client.GetStream().Write(msgBuffer, 0, msgBuffer.Length);
                                client.GetStream().Flush();
                                client.Close();
                                continue;
                            }
                        }
                        TcpServerConnectEventArgs eArgs = new TcpServerConnectEventArgs()
                        {
                            Client = client
                        };
                        ClientConnecting?.Invoke(this, eArgs);

                        if (eArgs.Accept)
                        {
                            //Connection Accepted
                            TcpServerConnection newConnection = new TcpServerConnection(this, client)
                            {
                                MessageDelimiter = MessageDelimiter
                            };
                            Trace.WriteLine(Name + ": Connection Accepted: Client = " + newConnection.ClientIPAddress);
                            newConnection.ClientDisconnected += OnClientDisconnected;
                            lock (ActiveConnections) { ActiveConnections.Add(newConnection); }
                            ClientConnected?.Invoke(this, new TcpServerEventArgs()
                            {
                                Client = newConnection
                            });
                        }
                        else
                        {
                            //Connection Refused
                            Trace.WriteLine(Name + ": Connection Rejected.");
                            client.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("[WARNING] " + Name + ": Exception raised from Connection Monitoring Thread!\n" + ex.Message);
                    continue;
                }
            }
        }
Exemplo n.º 4
0
 internal virtual void OnClientConnecting(EventArgs e) => ClientConnecting?.Invoke(this, e);
Exemplo n.º 5
0
 protected virtual void OnClientConnecting(EventArgs e) => ClientConnecting?.Invoke(this, e);
 public void OnClientConnecting(ConnectionStatusEventArgs e)
 {
     ClientConnecting?.Invoke(this, e);
 }
Exemplo n.º 7
0
        private void MonitorIncomingConnection()
        {
            listener.Start();
            while (true)
            {
                try
                {
                    if (Abort)
                    {
                        //Terminating Server and Monitoring Loop
                        listener.Stop();
                        Trace.WriteLine(Name + ": TCP Server Stopped.");
                        DisconnectAllClients();
                        return;
                    }

                    if (!listener.Pending())
                    {
                        Thread.Sleep(10);
                    }
                    else
                    {
                        System.Net.Sockets.TcpClient client = listener.AcceptTcpClient();

                        var stream = client.GetStream();

                        byte[] wsData = null;

                        Thread.Sleep(10); // 停10ms,否则获取握手时数据有可能取不到

                        if (stream.DataAvailable)
                        {
                            var buffer  = new byte[client.ReceiveBufferSize];
                            var factLen = stream.Read(buffer, 0, buffer.Length);
                            wsData = new byte[factLen];
                            Array.Copy(buffer, wsData, factLen);
                        }

                        Trace.WriteLine(Name + ": New Connection Detected...");
                        TcpServerConnectEventArgs eArgs = new TcpServerConnectEventArgs()
                        {
                            Client = client
                        };

                        ClientConnecting?.Invoke(this, eArgs);


                        if (eArgs.Accept)
                        {
                            //Connection Accepted
                            TcpServerConnection newConnection = new TcpServerConnection(this, client)
                            {
                                MessageDelimiter = MessageDelimiter,
                                WsData           = wsData
                            };
                            Trace.WriteLine(Name + ": Connection Accepted: Client = " + newConnection.ClientIPAddress);

                            newConnection.ClientDisconnected += OnClientDisconnected;
                            lock (ActiveConnections) { ActiveConnections.Add(newConnection); }
                            ClientConnected?.Invoke(this, new TcpServerEventArgs()
                            {
                                Client = newConnection
                            });
                        }
                        else
                        {
                            //Connection Refused
                            Trace.WriteLine(Name + ": Connection Rejected.");
                            client.Close();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("[WARNING] " + Name + ": Exception raised from Connection Monitoring Thread!\n" + ex.Message);
                    continue;
                }
            }
        }