public void OnConnectionEstablished(System.Net.Sockets.Socket connectedSocket)
        {
            try
            {
                if (connectedSocket != null && connectedSocket.Connected)
                {
                    byte[] dataBuffer = new byte[DATA_BUFFER_LENGTH];

                    NetworkUtil.ReadFromTcpSocket(connectedSocket, dataBuffer);

                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(dataBuffer);
                    }

                    SessionTypes sessionType = (SessionTypes)BitConverter.ToInt32(dataBuffer, 0);

                    ISessionListener sessionListioner = null;

                    if (sessionType == SessionTypes.Management)
                    {
                        if (_sessiionListioners.ContainsKey(sessionType))
                        {
                            sessionListioner = _sessiionListioners[SessionTypes.Management];
                        }
                    }

                    if (sessionListioner != null)
                    {
                        IPEndPoint localEndPoint  = (IPEndPoint)connectedSocket.LocalEndPoint;
                        IPEndPoint remoteEndPoint = (IPEndPoint)connectedSocket.RemoteEndPoint;

                        IConnection connection;
                        connection = new TcpConnection(connectedSocket, sessionType);

                        sessionListioner.OnSessionEstablished(new Session(sessionType, connection, localEndPoint.Port, remoteEndPoint.Port, remoteEndPoint.Address));
                    }
                }
                else
                {
                    connectedSocket.Close();
                }
            }
            catch (Exception ex)
            {
                if (connectedSocket != null)
                {
                    connectedSocket.Close();
                }
                if (LoggerManager.Instance.ServerLogger != null && LoggerManager.Instance.ServerLogger.IsErrorEnabled)
                {
                    LoggerManager.Instance.ServerLogger.Error("ManagementShardServer.OnConnectionEstablished()", ex);
                }
            }
        }
Exemple #2
0
        public void OnConnectionEstablished(System.Net.Sockets.Socket connectedSocket)
        {
            try
            {
                if (connectedSocket != null && connectedSocket.Connected)
                {
                    //Read session type; A session type is a number encoded as UTF8 String of 10 bytes
                    byte[] dataBuffer = new byte[DATA_BUFFER_LENGTH];

                    NetworkUtil.ReadFromTcpSocket(connectedSocket, dataBuffer);
                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(dataBuffer);
                    }

                    SessionTypes sessionType = (SessionTypes)BitConverter.ToInt32(dataBuffer, 0);

                    ISessionListener sessionListener = null;
                    switch (sessionType)
                    {
                    case SessionTypes.Shard:
                        if (_listeners.ContainsKey(SessionTypes.Shard))
                        {
                            sessionListener = _listeners[SessionTypes.Shard];
                        }
                        break;

                    case SessionTypes.Client:
                        if (_listeners.ContainsKey(SessionTypes.Client))
                        {
                            sessionListener = _listeners[SessionTypes.Client];
                        }
                        break;

                    case SessionTypes.Management:
                        if (_listeners.ContainsKey(SessionTypes.Management))
                        {
                            sessionListener = _listeners[SessionTypes.Management];
                        }
                        break;

                    case SessionTypes.Monitoring:
                        if (_listeners.ContainsKey(SessionTypes.Monitoring))
                        {
                            sessionListener = _listeners[SessionTypes.Monitoring];
                        }
                        break;
                    }

                    if (sessionListener != null)
                    {
                        IPEndPoint localEndPoint  = (IPEndPoint)connectedSocket.LocalEndPoint;
                        IPEndPoint remoteEndPoint = (IPEndPoint)connectedSocket.RemoteEndPoint;

                        IConnection connection = new TcpConnection(connectedSocket, sessionType);

                        sessionListener.OnSessionEstablished(new Session(sessionType, connection, localEndPoint.Port, remoteEndPoint.Port, remoteEndPoint.Address));
                    }
                    else //As no session listener found
                    {
                        connectedSocket.Close();
                    }
                }
            }
            catch (Exception e)
            {
                if (connectedSocket != null)
                {
                    connectedSocket.Close();
                }
                if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled)
                {
                    LoggerManager.Instance.ShardLogger.Error("ShardServer.OnConnectionEstablished()", e);
                }
            }
        }