private void SendMessage(TcpConnection connection, Message message) { MemoryStream stream = new MemoryStream(); switch(MessageType) { case MessageType.Simple: byte[] data; ConvertHelper.Int32ToNewBytes((int)message.MessageTypeId, out data); stream.Write(data, 0, 4); if (message.MessageData != null) stream.Write(message.MessageData, 0, message.MessageData.Length); break; case MessageType.Protobuf: ProtoBuf.Serializer.Serialize<Message>(stream, message); break; } TcpDatagram datagram = new TcpDatagram((int)stream.Length); datagram.Data = stream; SendDatagram(connection, datagram); }
private void SendDatagram(TcpConnection connection, TcpDatagram datagram) { if (connection != null) { lock (connection) //Синхронизируем (если вдруг доступ из многих потоков, одного клиента) { OnBeginSending(new BeginSendingEventArgs(connection)); byte[] data = new byte[datagram.Data.Length + 4]; ConvertHelper.Int32ToBytes(datagram.Size, ref data); datagram.Data.Position = 0; datagram.Data.Read(data, 4, (int)datagram.Data.Length); Send(connection, data); OnEndSending(new EndSendingEventArgs(connection)); } } }