Exemplo n.º 1
0
        protected virtual void OnConnectionOpened(TcpConnectedClient client)
        {
            //Log.LogText(LogLevel.INFO, "{0} Client {1} opened", Name, client);

            if (ConnectionOpened != null)
            {
                ConnectionOpened(client);
            }
        }
Exemplo n.º 2
0
        public int SendToExistingConnection(TcpConnectedClient client, byte[] packet, int offset, int size)
        {
            int retVal = 0;

            try
            {
                // Verify Connection Is Still Open Before Sending
                retVal = client.Send(packet, offset, size);
            }
            catch (Exception e)
            {
                // Notify Caller Of Exception
                RaiseException(e);
                retVal = -1;                  // Indicate An Error Occurred
            }
            return(retVal);
        }
Exemplo n.º 3
0
        // Called by the tcp client
        public virtual void OnConnectionClosed(TcpConnectedClient client)
        {
            TcpConnectedClient existingClient;

            try
            {
                _clientLock.Lock();

                // Make sure it is the corresponding client
                if (_clientList.TryGetValue(client.RemoteAddress.Address, out existingClient) &&
                    existingClient.GetHashCode() == client.GetHashCode())
                {
                    Log.LogText(LogLevel.DEBUG, "OnConnectionClosed  Found it");
                    _clientList.Remove(client.RemoteAddress.Address);

                    if (_connectionDebugLogging)
                    {
                        Log.LogText(LogLevel.DEBUG, "{0} Client {1} Removed", Name, client);
                    }
                }
                else
                {
                    Log.LogText(LogLevel.DEBUG, "OnConnectionClosed  Did not found it");
                    existingClient = null;
                }
            }
            finally
            {
                _clientLock.Unlock();
            }

            // Notify Consumer Of Event
            if (existingClient != null && ConnectionClosed != null)
            {
                ConnectionClosed(client);
            }
        }
Exemplo n.º 4
0
        private void OnConnectionRequestReceived(IAsyncResult ar)
        {
            TcpListener tcpServer = null;

            try
            {
                if ((State == ThreadState.Started || State == ThreadState.Starting) && ShuttingDown == false)
                {
                    tcpServer = (TcpListener)ar.AsyncState;
                    TcpClient tcpClient = tcpServer.EndAcceptTcpClient(ar);

                    if (_connectionDebugLogging)
                    {
                        Log.LogText(LogLevel.DEBUG, "{0} Incoming connection from {1}", Name, tcpClient.Client.RemoteEndPoint);
                    }

                    // Check If Connection From This Client Is Allowed
                    if (ConnectionAllowed(tcpClient))
                    {
                        // Set Client KeepAlive Parms
                        if (_keepAliveInterval != 0)
                        {
                            byte[] outValue = BitConverter.GetBytes(_keepAliveInterval);
                            byte[] options  = new byte[12];

                            // enable/disable
                            BinaryConverter.UInt32ToByteArrayHostOrder((UInt32)1, ref options, 0);
                            // idle timeout
                            BinaryConverter.UInt32ToByteArrayHostOrder(_keepAliveInterval, ref options, 4);
                            // poll interval
                            BinaryConverter.UInt32ToByteArrayHostOrder(_keepAliveInterval, ref options, 8);

                            /** added for mono compatibility */
                            try
                            {
                                tcpClient.Client.IOControl(IOControlCode.KeepAliveValues, options, outValue);
                            }
                            catch (Exception e)
                            {
                                Log.LogText(LogLevel.WARNING, "Could not set Keep-Alive: {0}", e.Message);
                            }
                        }

                        // Set Send timeout
                        tcpClient.Client.SendTimeout = _sendTimeout;

                        // Set Liinger option
                        tcpClient.Client.LingerState = new LingerOption(_lingerTime == 0 ? false : true, _lingerTime);

                        TotalConnectCount++;

                        // Add The To The List Of Clients
                        //      All disconnected will be shutdown, closed and disposed
                        //      after each connection is lost and when the server is stopped
                        TcpConnectedClient newClient      = OnConnectionReceived(tcpClient, tcpServer);
                        TcpConnectedClient existingClient = null;
                        if (newClient != null)
                        {
                            try
                            {
                                _clientLock.Lock();

                                // Remove existing client with same IPV4:Port
                                if (_clientList.TryGetValue(newClient.RemoteAddress.Address, out existingClient))
                                {
                                    _clientList.Remove(existingClient.RemoteAddress.Address);
                                }
                                else
                                {
                                    existingClient = null;
                                }

                                _clientList.Add(newClient.RemoteAddress.Address, newClient);
                            }
                            catch (Exception e)
                            {
                                Log.LogText(LogLevel.WARNING, "{0} Failure Adding Client {1} {2}", Name, newClient.RemoteAddress.Address, e.Message);
                            }
                            finally
                            {
                                _clientLock.Unlock();
                            }

                            // Close existing client connection
                            if (existingClient != null)
                            {
                                Log.LogText(LogLevel.DEBUG, "{0} Removing and Closing Existing Client {1} closed", Name, existingClient.RemoteAddress.Address);
                                existingClient.Close();
                            }

                            // Raise New Connection Event
                            OnConnectionOpened(newClient);

                            if (_connectionDebugLogging)
                            {
                                Log.LogText(LogLevel.DEBUG, "{0} Connection Accepted {1}", Name, newClient.RemoteAddress.Address);
                            }

                            /** start receive unless our clients will be handling their own I/O */
                            if (_clientsHandleIO == false)
                            {
                                newClient.BeginReceive();
                            }
                        }
                    }
                    else
                    {
                        Log.LogText(LogLevel.INFO, "{0} Connection Not Allowed {1}", Name, tcpClient.Client.RemoteEndPoint);
                        // Reject The Connection
                        tcpClient.Close();
                    }
                }
                else
                {
                    Log.LogText(LogLevel.DEBUG, "{0} Was Shutdown", Name);
                }
            }
            catch (Exception ex)
            {
                Log.LogText(LogLevel.ERROR, "{0} OnConnectionRequestReceived EXCEPTION: {1}", Name, ex.Message);
                RaiseException(ex);
            }

            if (tcpServer != null && tcpServer.Server != null && tcpServer.Server.IsBound && (State == ThreadState.Started || State == ThreadState.Starting))
            {
                tcpServer.BeginAcceptSocket(new AsyncCallback(OnConnectionRequestReceived), tcpServer);
            }
        }