示例#1
0
        /// <summary>
        /// new session
        /// </summary>
        /// <param name="socket"></param>
        private void NewSession(Socket socket)
        {
            var headBuffer           = SocketHelper.Receive(socket, 4);
            var clientInfoDataLength = BitConverter.ToInt32(headBuffer, 0);

            if (clientInfoDataLength >= 0 && clientInfoDataLength < 65535)
            {
                //旧有版本使用的BitConvert.GetBytes序列化因此 根据旧版本长度不可能超过1000字节的依据,使用 >  [0,0,0,0] = 0 && < [255,255,0,0] = 65535

                CsInfo clientInfo = null;
                //body
                using (var m = SocketHelper.Receive(socket, clientInfoDataLength, CsSocket.BODY_BUFFER_SIZE))
                {
                    try
                    {
                        clientInfo = CsSerializer.Deserialize(typeof(CsInfo), m) as CsInfo;
                    }
                    catch (Exception)
                    {
                        socket.Close();
                        return;
                    }
                }
                //
                if (clientInfo != null)
                {
                    var session = new ServerSession(socket, this.serverLogger, this, clientInfo);
                    lock (this.sessionCountLock)
                    {
                        session.Disposed += this.SessionDisposedCallback;
                        this.sessionCount++;
                    }

                    session.ReceiveCommand();
                }
            }
            //client >= 1.4
            else
            {
                var session = new ServerSession2(socket, this.logManager, headBuffer);
                //
                lock (this.sessionCountLock)
                {
                    session.Disposed += this.Session2DisposedCallback;
                    this.sessionCount++;
                }
                session.ReceiveCommand();
            }
        }
示例#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);
        }