eap.freeciv
Class PacketStructure

java.lang.Object
  extended byeap.freeciv.PacketStructure

public class PacketStructure
extends Object

Describes the way a packet is encoded in a data stream. A structure consistes of an ordered list of Field objects. Generally you get a structure from a PacketBroker, which in turn reads a configuration file. Note that the structure does not store any of the packet data. However, the structure knows how to read the data from a stream and store it in a Packet. By separating storage and structure, we can create one structure object for each packet type, and reuse it for every packet of that type. This avoids having to re-read the configuration for every packet.


Constructor Summary
PacketStructure()
          Create a new structure with no fields.
 
Method Summary
 void addField(Field field)
          Append a field to the end of this structure.
 void read(Packet packet, PacketSource source)
          Read the packet from the given source and store its values in the given packet.
 int size(Packet packet)
          Calculate the size of the body of a given packet in bytes.
 void write(Packet packet, OutputStream out)
          Write the body of the given packet to the given output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PacketStructure

public PacketStructure()
Create a new structure with no fields.

Method Detail

addField

public void addField(Field field)
Append a field to the end of this structure.


read

public void read(Packet packet,
                 PacketSource source)
          throws IOException
Read the packet from the given source and store its values in the given packet.

Throws:
IOException

write

public void write(Packet packet,
                  OutputStream out)
           throws IOException
Write the body of the given packet to the given output stream. Note this does not write the packet header. Packet.write(OutputStream) writes the packet header and then calls this method.

Throws:
IOException

size

public int size(Packet packet)
Calculate the size of the body of a given packet in bytes. Note that we need both the data and the structure to determine the size of a packet, because some fields are optional and some have variable size. Note that the packet header has three bytes, so you need to add three to this number to get the packet size encoded in the header.