Пример #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="type"></param>
        /// <param name="deserialize"></param>
        /// <returns></returns>
        private object Receive(System.Net.Sockets.Socket socket, Type type, bool deserialize)
        {
            //length
            var length = BitConverter.ToInt32(SocketHelper.Receive(socket, CsSocket.LENGTH_BUFFER_SIZE), 0);

            //body
            using (var m = SocketHelper.Receive(socket, length, CsSocket.BODY_BUFFER_SIZE))
            {
                if (deserialize)
                {
                    try
                    {
                        return(CsSerializer.Deserialize(type, m));
                    }
                    catch (Exception exception)
                    {
                        this.serverLogger.SessionException(this, exception);
                        return(ReceiveParameterError.ERROR);
                    }
                }
                return(null);
            }
        }