Esempio n. 1
0
 public bool Send(MemoryStream stream)
 {
     if (stream.Length == 0)
     {
         return(true);
     }
     else
     {
         AsyncSendBufferManager asyncSendBufferManager = _userToken.SendBuffer;
         asyncSendBufferManager.StartPacket();
         asyncSendBufferManager.DynamicBufferManager.WriteBuffer(stream.GetBuffer(), 0, (int)stream.Length);
         asyncSendBufferManager.EndPacket();
         bool result = true;
         if (!_sendAsync)
         {
             int packetOffset = 0;
             int packetCount  = 0;
             if (asyncSendBufferManager.GetFristPacket(ref packetOffset, ref packetCount))
             {
                 _sendAsync = true;
                 result     = SendAsyncEvent(_userToken.ConnectSocket, _userToken.SendEventArgs, asyncSendBufferManager.DynamicBufferManager.Buffer, packetOffset, packetCount);
                 return(result);
             }
         }
     }
     return(true);
 }
Esempio n. 2
0
        //public virtual bool Send<T>(T msg) where T : global::ProtoBuf.IExtensible
        //{
        //    MemoryStream body = new MemoryStream();
        //  MessagePacker.ProtobufHelper.Serialize(body, msg);

        //    MemoryStream head = new MemoryStream(sizeof(ushort) + sizeof(uint));
        //    ushort len = (ushort)body.Length;
        //    head.Write(BitConverter.GetBytes(len),0,2);
        //    head.Write(BitConverter.GetBytes(Id<T>.Value),0,4);
        //    return Send(head, body);
        //}

        public bool Send(MemoryStream head, MemoryStream body)
        {
            head.Seek(0, SeekOrigin.Begin);
            body.Seek(0, SeekOrigin.Begin);
            if (body.Length == 0)
            {
                return(Send(head));
            }
            else
            {
                AsyncSendBufferManager asyncSendBufferManager = _userToken.SendBuffer;
                asyncSendBufferManager.StartPacket();
                asyncSendBufferManager.DynamicBufferManager.WriteBuffer(head.GetBuffer(), 0, (int)head.Length);
                asyncSendBufferManager.DynamicBufferManager.WriteBuffer(body.GetBuffer(), 0, (int)body.Length);
                asyncSendBufferManager.EndPacket();
                bool result = true;
                if (!_sendAsync)
                {
                    int packetOffset = 0;
                    int packetCount  = 0;
                    if (asyncSendBufferManager.GetFristPacket(ref packetOffset, ref packetCount))
                    {
                        _sendAsync = true;
                        result     = SendAsyncEvent(_userToken.ConnectSocket, _userToken.SendEventArgs, asyncSendBufferManager.DynamicBufferManager.Buffer, packetOffset, packetCount);
                        return(result);
                    }
                }
            }
            return(true);
        }