Esempio n. 1
0
        private void CheckAndChangeStateAgainstConnectionCount()
        {
            if (TCPConnections.Count >= _MaxConnectionsAllowed)
            {
                _serverState = TCPServerState.ConnectionThresholdReached;

                _Logger.Debug("Connection threshold reached");
                _Logger.Debug("Stopping listener");

                _tcplistener.Stop();
                return;
            }

            if (TCPConnections.Count < _MaxConnectionsAllowed)
            {
                if (_serverState != TCPServerState.Listening)
                {
                    _serverState = TCPServerState.Listening;

                    _Logger.Debug("Starting listener again");
                    _tcplistener.Start();
                    return;
                }
            }
        }
Esempio n. 2
0
        public void Start()
        {
            AsyncCallback connectionrequestcallback = new AsyncCallback(this.OnConnectRequest);

            this.tcplistener.Start();



            //Main loop to keep the server going
            while (true)
            {
                if (_serverState == TCPServerState.ConnectionThresholdReached)
                {
                    continue;
                }

                if (_serverState != TCPServerState.ConnectionThresholdReached && _serverState != TCPServerState.AcceptConnectionRequestOperationStarted)
                {
                    Logger.Debug($"Starts listening for connection request number: {TCPConnections.Count + 1}");



                    this.tcplistener.BeginAcceptTcpClient(connectionrequestcallback, tcplistener);
                    _serverState = TCPServerState.AcceptConnectionRequestOperationStarted;
                }


                //Just tells us on which thread the main loop is running, and updates us if it should changed (it doesn't, it's always ThreadID 1)
                if (threads != System.Diagnostics.Process.GetCurrentProcess().Threads.Count)
                {
                    threads = System.Diagnostics.Process.GetCurrentProcess().Threads.Count;
                    Logger.Debug($"Main loop is running thread id : {Thread.CurrentThread.ManagedThreadId}");
                }
            }
        }
Esempio n. 3
0
        private async void OnConnectRequest(IAsyncResult result)
        {
            Logger.Debug($"OnConnectRequest is running thread id : {Thread.CurrentThread.ManagedThreadId}");

            TcpListener sock = (TcpListener)result.AsyncState;

            WorkingTCPConnection_Threaded newConn = new WorkingTCPConnection_Threaded(sock.Server.EndAccept(result), Logger);

            Logger.Info($"Connection made with {newConn.WorkSocket.RemoteEndPoint.ToString()}");

            TCPConnections.Add(newConn);

            newConn.ConnectionClosedEvent += this.OnConnectionClosed;

            if (TCPConnections.Count >= MaxConnectionsAllowed)
            {
                _serverState = TCPServerState.ConnectionThresholdReached;

                Logger.Info("Max allowed connections has been reached");
            }
            else
            {
                //Reset server state to default

                _serverState = TCPServerState.Listening;
            }

            await newConn.StartListen();
        }
Esempio n. 4
0
        private void OnConnectionClosed(IWorkingTCPConnection connection)
        {
            if (TCPConnections.Contains(connection))
            {
                //connection.ConnectionClosedEvent -= this.OnConnectionClosed;

                TCPConnections.Remove(connection);

                Logger.Info($"Connection closed and removed");
            }

            if (TCPConnections.Count < MaxConnectionsAllowed)
            {
                _serverState = TCPServerState.Listening;
            }
        }