/// <summary> /// Transmits the specified packet to the remote end point. /// </summary> /// <param name="packet">The packet to transmit to the remot end point.</param> /// <returns>True when transmitting the specified packet was a success.</returns> public bool Send(IMeisterPacket packet) { if (packet == null) return false; //Create a new binary writer to let the packet serialize to.. BinaryWriter body_writer = new BinaryWriter(new MemoryStream()); //Serialize the packet.. if (!packet.Serialize(body_writer)) return false; //Get the size of the packet body long body_size_large = body_writer.BaseStream.Length; if (body_size_large > UInt16.MaxValue) return false; UInt16 body_size = (UInt16)body_size_large; //Create a writer for the header BinaryWriter header_writer = new BinaryWriter(new MemoryStream()); //Initialize a new packet header, with the body size and packet identifier MeisterPacketHeader header = new MeisterPacketHeader(body_size, 0, packet.GetIdentifier()); header.Serialize(header_writer); //Determine the header size UInt16 header_size = MeisterPacketHeader.HeaderSize; //Reset the positions of both the writer for the header and body header_writer.BaseStream.Position = 0; body_writer.BaseStream.Position = 0; //Copy the contents of both the packet and header into our buffer byte[] send_buffer = new byte[header.Size]; header_writer.BaseStream.Read(send_buffer, 0, (int)header_size); body_writer.BaseStream.Read(send_buffer, (int)header_size, (int)body_size); //Start transmission to the remote end point this.BaseSocket.BeginSend(send_buffer, 0, send_buffer.Length, SocketFlags.None, OnSend, null); return true; }
/// <summary> /// Event handler for when a packet is received. /// </summary> private static void PacketReceived(MeisterSocket socket, IMeisterPacket packet) { if (packet.GetIdentifier() != 0) return; TestPacket pack = (TestPacket)packet; Logger.Info("Received a packet from one of my minions... " + pack.Neger.ToString()); }