Ejemplo n.º 1
0
 /// <summary>
 /// 写数据
 /// </summary>
 /// <param name="stream"></param>
 public override void Write(StreamBuffer stream)
 {
     base.Write(stream);
     stream.WriteData(this.m_sTaskType);
     stream.WriteData(this.m_uiVersion);
     stream.WriteData(this.m_ulVerifyCode);
 }
Ejemplo n.º 2
0
        private SESSION_STATUS m_cStatus; //会话状态

        #endregion Fields

        #region Constructors

        public SeverSession(Socket socket, DispatchFactoryBase dispatchFactory)
        {
            this.m_cDispatchFactory = dispatchFactory;
            this.m_cDispatch = this.m_cDispatchFactory.Create( this );
            this.m_cSocket = socket;

            this.m_cReceiveBuffer = new StreamBuffer();
            this.m_cSendBuffer = new StreamBuffer();

            this.m_cSendQueue = new NetQueue<PacketBase>(64 * 256);
            this.m_cSendQueue.Clear();
        }
Ejemplo n.º 3
0
        private string m_strAddress; //连接地址

        #endregion Fields

        #region Constructors

        public ClientSession( DispatchFactoryBase dispatchFactory )
        {
            this.m_cStatus = SESSION_STATUS.NO_CONNECT;
            this.m_cDispatchFactory = dispatchFactory;

            this.m_cReceiveBuffer = new StreamBuffer();
            this.m_cSendBuffer = new StreamBuffer();

            this.m_cSendQueue = new NetQueue<PacketBase>(64 * 256);
            this.m_cSendQueue.Clear();

            this.m_bStartReConnect = false;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 处理包
 /// </summary>
 private void ProcessPacket()
 {
     try
     {
         for (; true; )
         {
             PacketBase head = Packing.GetPacketHead(this.m_cReceiveBuffer.m_lstBuffer, this.m_cReceiveBuffer.ReadIndex, this.m_cReceiveBuffer.WriteIndex);
             if (head != null)
             {
                 byte[] buffer = this.m_cReceiveBuffer.Read(head.m_usPacketSize);
                 buffer = Packing.PackingDecode(buffer, head.m_usPacketSize);
                 if (buffer != null)
                 {
                     //MemoryStream ms = new MemoryStream(buffer);
                     //BinaryFormatter bf = new BinaryFormatter();
                     //PacketBase pb = (PacketBase)bf.Deserialize(ms);
                     PacketBase pb = PacketFactoryManager.GetInstance().CreatePacket(head.GetPacketID());
                     StreamBuffer sb = new StreamBuffer();
                     sb.Init(buffer, buffer.Length);
                     pb.Load(sb);
                     this.m_cDispatch.AckPacket(pb);
                 }
                 else
                 {
                     break;
                 }
             }
             else
             {
                 break;
             }
         }
     }
     catch (Exception e)
     {
         WriteFiles.WritFile.Log(LogerType.ERROR, e.ToString());
         DisConnect();
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// 发送
        /// </summary>
        public void Send()
        {
            //
            try
            {
                PacketBase pb;
                if (this.m_cSendQueue.Dequeue(out pb))
                {
                    if (this.m_cSocket == null)
                    {
                        throw new Exception("The socket is null.");
                    }
                    //MemoryStream ms = new MemoryStream();
                    //BinaryFormatter bf = new BinaryFormatter();
                    //byte[] buffer = null;
                    //bf.Serialize(ms, pb);
                    //buffer = ms.ToArray();
                    byte[] buffer = null;
                    StreamBuffer sb = new StreamBuffer();
                    pb.Write(sb);
                    buffer = sb.m_lstBuffer;
                    if (buffer == null)
                    {
                        throw new Exception("The the send buffer is null.");
                    }
                    buffer = Packing.PackingEncode(buffer, pb.GetSize());

                    SocketAsyncEventArgs e = new SocketAsyncEventArgs();
                    e.Completed += this.SendCompleteCallBack;
                    e.SetBuffer(buffer, 0, Packing.GetPacketHeadSize() + pb.GetSize());
                    bool done = this.m_cSocket.SendAsync(e);
                    if (!done)
                    {
                        SendProcess(e);
                    }
                    //this.m_cSocket.BeginSend(buffer, 0, Packing.GetPacketHeadSize() + pb.GetSize(),
                    //    SocketFlags.DontRoute, new AsyncCallback(SendCallBack), this.m_cSocket);
                }
            }
            catch (Exception ee)
            {
                WriteFiles.WritFile.Log(LogerType.ERROR, ee.ToString());
                DisConnect();
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// 获取数据比特流
 /// </summary>
 /// <returns></returns>
 public virtual void Write(StreamBuffer stream)
 {
     stream.WriteData(this.m_usPacketSize);
     stream.WriteData(this.m_usPacketId);
 }
Ejemplo n.º 7
0
 /// <summary>
 /// 设置由比特流而得到的数据
 /// </summary>
 /// <param name="data"></param>
 public virtual void Load(StreamBuffer stream)
 {
     this.m_usPacketId = stream.ReadUInt16();
     this.m_usPacketSize = stream.ReadUInt16();
 }
Ejemplo n.º 8
0
        /// <summary>
        /// 发送列表包中的数据
        /// </summary>
        public void Send()
        {
            try
            {
                PacketBase pb;
                if (this.m_cSendQueue.Dequeue(out pb))
                {
                    if (this.m_cSocket == null)
                    {
                        throw new Exception("The socket is null.");
                    }
                    //MemoryStream ms = new MemoryStream();
                    //BinaryFormatter bf = new BinaryFormatter();
                    //byte[] buffer = null;
                    //bf.Serialize(ms, pb);
                    //buffer = ms.ToArray();
                    byte[] buffer = null;
                    StreamBuffer sb = new StreamBuffer();
                    pb.m_usPacketSize = pb.GetSize();
                    pb.Write(sb);
                    buffer = sb.GetBuffer();
                    if (buffer == null)
                    {
                        throw new Exception("The the send buffer is null.");
                    }

                    //加密
                    //buffer = Packing.PackingEncode(buffer, pb.GetSize() );

                    this.m_cSocket.BeginSend(buffer, 0, buffer.Length,
                        SocketFlags.DontRoute, new AsyncCallback(SendCallBack), this.m_cSocket);
                }
            }
            catch (Exception e)
            {
                WriteFiles.WritFile.Log(LogerType.ERROR, e.StackTrace);
                DisConnect();
                if (this.m_iReConnectSecond >= 0)   //设置重连
                {
                    this.m_bStartReConnect = true;
                    this.m_lStartReConnectTime = DateTime.Now.Ticks;
                }
            }
        }