//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); }
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); }
public AsyncUserToken(int asyncReceiveBufferSize) { _connectSocket = null; _receiveEventArgs = new SocketAsyncEventArgs(); _receiveEventArgs.UserToken = this; _asyncReceiveBuffer = new byte[asyncReceiveBufferSize]; _receiveEventArgs.SetBuffer(_asyncReceiveBuffer, 0, _asyncReceiveBuffer.Length); _receiveBuffer = new DynamicBufferManager(10); _sendEventArgs = new SocketAsyncEventArgs(); _sendEventArgs.UserToken = this; _sendBuffer = new AsyncSendBufferManager(10); }
private bool SendCompleted() { _sendAsync = false; AsyncSendBufferManager asyncSendBufferManager = _userToken.SendBuffer; asyncSendBufferManager.ClearFirstPacket(); int offset = 0; int count = 0; if (asyncSendBufferManager.GetFristPacket(ref offset, ref count)) { return(SendAsyncEvent(_userToken.ConnectSocket, _userToken.SendEventArgs, asyncSendBufferManager.DynamicBufferManager.Buffer, offset, count)); } else { return(SendCallback()); } }