public void SendData(PacketBase packet) { packet.Write(); byte[] data = packet.GetWriteData(); Packet p = new Packet(); p.Create(data); Peer.Send(0, ref p); }
public static void SendMessage(Client recipient, PacketType packetType, object data) { if (recipient == null) { return; } #if DEBUG // // If we're debugging, make sure that the data type matches that required by the packetType, or else throw an exception. // var type = DataPacket.PacketTypeMap[packetType]; if (type != null) { var testObject = Convert.ChangeType(data, type); } else { if (data != null) { throw new ArgumentException("Data from this packet type was expected to be null, but wasn't."); } } #endif // ToDo: Don't hardcode Packet Version 1 here. Instead, add a packet version property to Client, and switch on that. var packetBase = new PacketBase { PacketVersion = PacketVersion.Version1 }; var packetV1 = new PacketV1 { PacketType = packetType, Data = data, Sender = ServerName }; packetV1.Recipients.Add(recipient.UserInfo.EmailAddress); packetBase.Data = packetV1; dynamic baseBuffer; using (var stream = new MemoryStream()) { var binaryWriter = new BinaryWriter(stream); packetBase.Write(binaryWriter); baseBuffer = stream.ToArray(); } var messageSize = baseBuffer.Length; byte[] bytes = BitConverter.GetBytes(messageSize); if (BitConverter.IsLittleEndian) { Array.Reverse(bytes); } var offset = 0; var messageBuffer = new byte[sizeof(uint) + messageSize]; bytes.CopyTo(messageBuffer, offset); offset += sizeof(uint); baseBuffer.CopyTo(messageBuffer, offset); recipient.Send(messageBuffer); }
private async Task SendMessage(PacketType packetType, IEnumerable <Friend> recipientList, object data) { var mainPageViewModel = MainPageViewModel.GetInstance(); if ((ServerProxy.TcpSocket == null) || mainPageViewModel.ConnectionStatus == false) { return; } #if DEBUG // // If we're debugging, make sure that the data type matches that required by the packetType, or else throw an exception. // var type = DataPacket.PacketTypeMap[packetType]; if (type != null) { var testObject = Convert.ChangeType(data, type); } else { if (data != null) { throw new ArgumentException("Data from this packet type was expected to be null, but wasn't."); } } #endif using (var dataWriter = new DataWriter(ServerProxy.TcpSocket.OutputStream)) { byte[] msgSizeBuffer; dynamic baseBuffer = null; if (PacketType.c_HeartBeat == packetType) { // Message Size is specified by the size of an int. msgSizeBuffer = new byte[] { 0, 0, 0, 0 }; } else { var packetBase = new PacketBase { PacketVersion = PacketVersion.Version1 }; var packetV1 = new PacketV1 { PacketType = packetType, Data = data, Sender = Settings.EmailAddress }; foreach (var b in recipientList) { packetV1.Recipients.Add(b.EmailAddress); } packetBase.Data = packetV1; using (var stream = new MemoryStream()) { var binaryWriter = new BinaryWriter(stream); packetBase.Write(binaryWriter); baseBuffer = stream.ToArray(); } var messageSize = baseBuffer.Length; msgSizeBuffer = BitConverter.GetBytes(messageSize); if (!BitConverter.IsLittleEndian) { Array.Reverse(msgSizeBuffer); } } // // 1. Write the message size // 2. Write the object buffer (unless it's a heartbeat) // dataWriter.WriteBytes(msgSizeBuffer); if (baseBuffer != null) { dataWriter.WriteBytes(baseBuffer); } await dataWriter.StoreAsync(); dataWriter.DetachStream(); } }
/// <summary> /// Serializes the data stored in the stat class into the packet data /// </summary> public void Serialize(PacketBase packet) { //Write all our data out packet.Write(zonestat1); packet.Write(zonestat2); packet.Write(zonestat3); packet.Write(zonestat4); packet.Write(zonestat5); packet.Write(zonestat6); packet.Write(zonestat7); packet.Write(zonestat8); packet.Write(zonestat9); packet.Write(zonestat10); packet.Write(zonestat11); packet.Write(zonestat12); packet.Write(kills); packet.Write(deaths); packet.Write(killPoints); packet.Write(deathPoints); packet.Write(assistPoints); packet.Write(bonusPoints); packet.Write(vehicleKills); packet.Write(vehicleDeaths); packet.Write(playSeconds); packet.Write(cash); packet.Write((Int16)inventory.Count); foreach (PlayerStats.InventoryStat istat in inventory) { packet.Write(istat.itemid); packet.Write(istat.quantity); } packet.Write(experience); packet.Write(experienceTotal); packet.Write((Int16)skills.Count); foreach (PlayerStats.SkillStat sstat in skills) { packet.Write(sstat.skillid); packet.Write(sstat.quantity); } }