Пример #1
0
        /// <summary>
        /// 初始新实例
        /// </summary>
        /// <param name="logManager"></param>
        /// <param name="port"></param>
        /// <exception cref="ArgumentNullException">logManager</exception>
        public ServerListen(LogManager logManager, int port)
        {
            //config
            if (port == 0)
            {
                this.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
            }
            else
            {
                this.Port = port;
            }
            this.Name = ConfigHelper.AppName;

            //
            if (string.IsNullOrEmpty(this.Name))
            {
                throw new ApplicationException("Not Config AppName");
            }

            //
            if (logManager == null)
            {
                throw new ArgumentNullException("logManager");
            }

            //
            this.isDispose = false;
            this.StartTime = DateTime.Now;
            //init
            logManager.Message.WriteLine("Name:{0}", this.Name);
            logManager.Message.WriteLine("Port:{0}", this.Port);
            logManager.Message.WriteLine("Time:{0}", this.StartTime);
            this.logManager = logManager;
            this.connectionUnfinishedLogWriter = logManager.GetWriter("ConnectionUnfinished");
            //
            this.serverLogger = new ServerLogger(logManager);
            ServerCache.Init(logManager);

            //init context
            ServerCommand.Init(logManager);
            ServerConfig.Init(logManager, ServerCommand.Servers);

            //socket
            this.Socket = new Socket(AddressFamily.InterNetwork,
                                     SocketType.Stream,
                                     ProtocolType.Tcp);
            this.Socket.Bind(new IPEndPoint(IPAddress.Any, this.Port));

            //为了性能保证,服务暂不做连接数限制
            this.Socket.Listen(LISTEN_BACKLOG);

            this.Socket.BeginAccept(this.AcceptCallback, this.Socket);
        }
Пример #2
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="listen"></param>
        /// <param name="logger"></param>
        /// <param name="clientInfo"></param>
        internal ServerSession(Socket socket, ServerLogger logger, ServerListen listen, CsInfo clientInfo)
        {
            this.listen = listen;
            this.socket = socket;
            //
            this.serverLogger = logger;
            this.begin        = DateTime.Now;
            //
            var endPoint = (IPEndPoint)socket.RemoteEndPoint;

            this.ClientIp   = endPoint.Address.ToString();
            this.ClientPort = endPoint.Port;
            //
            this.Id = this.ClientIp + ":" + this.ClientPort;
            //
            this.ClientInfo = clientInfo;
            //
            this.serverLogger.NewSession(this);
        }