Beispiel #1
0
        /// <summary>
        /// 初始化新实例
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="logManager"></param>
        /// <param name="headBuffer"></param>
        public ServerSession2(Socket socket, LogManager logManager, byte[] headBuffer)
        {
            this.logManager = logManager;
            this.logMessage = logManager.Message;
            this.socket     = socket;
            //
            //reserve1(byte) + reserve2(byte) + reserve3(byte) + Protocol Version(byte)
            this.protocolVersion = headBuffer[3];
            //
            this.begin = DateTime.Now;
            //
            var endPoint = (IPEndPoint)socket.RemoteEndPoint;

            this.clientIp   = endPoint.Address.ToString();
            this.clientPort = endPoint.Port;
            //
            this.id = this.ClientIp + ":" + this.ClientPort;
            //
            if (logMessage.Enable)
            {
                logMessage.WriteTimeLine("{0}: New Session, Protocol Version:{1}",
                                         this.id, protocolVersion);
            }
            //
            this.receiveCommandState = new ServerSocketState(socket, 1);
        }
Beispiel #2
0
        /// <summary>
        /// 读取命令
        /// </summary>
        public void ReceiveCommand()
        {
            var state = new ServerSocketState(this.socket, CsSocket.LENGTH_BUFFER_SIZE);

            try
            {
                socket.BeginReceive(state.Buffer, state.Offset, state.BufferSize, SocketFlags.None, this.ReceiveCommandCallback, state);
            }
            catch (ObjectDisposedException)
            {
                return;
            }
            catch (SocketException)
            {
                this.Dispose();
            }
            catch (Exception exception)
            {
                this.serverLogger.SessionException(this, exception);

                this.Dispose();
            }
        }