Skip to main content

OutgoingPacketManager

A class in Root.Township, Alta.Networking.Internal.

Batches outgoing messages for one connection channel into packets and sends them. Reliable channels keep up to 64 unsent packets and disconnect the player when that fills up. Each Connection has one per channel.

Constructors​

MemberWhat it is
OutgoingPacketManager(Connection connection, ConnectionChannel channel)Creates an OutgoingPacketManager from connection and channel.

Fields​

14 private and internal fields
MemberWhat it is
private readonly ConnectionChannel ChannelThe connection channel this manager sends on.
private readonly Connection ConnectionThe connection this manager sends to.
private Packet CurrentPacketThe packet messages are currently being written into.
private readonly Queue<Packet> FailedPacketsQueueReliable channels only: packets that could not be sent yet and wait for a retry.
private readonly bool isReliableWhether the channel is reliable.
private bool IsWarningOnNextPacketLossWhether the next queue overflow should log an error and force a disconnect. Re-armed once the failed queue drops below 32.
private LinkedListNode<StreamWriter> lastSceneSerializeOn unreliable channels, the queued scene serialize message (type 28), replaced when a newer one is queued.
private float LastSendTimeReal time of the last full send, checked against the channel's maximum send interval in Update.
private static Logger loggerNLog logger for this class.
private const int MaximumFreePacketsLargest number of spare packets kept in the pool (512).
private static int maximumPacketSizeLargest PacketSize seen so far; the shared writers are rebuilt when it grows.
private const int MaximumPendingPacketsPerManagerFailed packets a reliable manager can hold (64) before it disconnects the connection.
private readonly LinkedList<StreamWriter> queueMessages held by SendWithQueued until SendAllQueued is called.
private static readonly Dictionary<int, StreamWriter> writerMapShared stream writers, one per connection identifier, reused for serializing messages.

Properties​

MemberWhat it is
static int PacketSize { get; set; }Packet size in bytes for all managers. Set by the socket to the transport packet size minus 100.
1 private and internal property
MemberWhat it is
private static ConcurrentBag<Packet> FreePackets { get; }Shared pool of spare packets, reused by PrepareCurrentPacket and filled by ReleasePacket.

Methods​

MemberWhat it is
void Dispose()Returns queued and current packets to the shared packet pool.
bool Send(MessageType type, SerializeConnectionMethod handler)Adds a message to the current packet, sending that packet first if it is full. Refuses messages bigger than PacketSize.
bool Send(StreamWriter writer, int byteCount)Adds a message to the current packet, sending that packet first if it is full. Refuses messages bigger than PacketSize.
virtual bool SendAll(bool isSkippingCurrent = false)Sends any failed reliable packets, then the current packet unless told to skip it.
void SendAllQueued()Sends every message held back by SendWithQueued.
bool SendWithQueued(StreamWriter writer, int byteCount)Holds a copy of the message until SendAllQueued, used while the connection queues sends. On unreliable channels only the latest scene serialize message is kept.
bool SendWithQueued(MessageType type, SerializeConnectionMethod handler)Holds a copy of the message until SendAllQueued, used while the connection queues sends. On unreliable channels only the latest scene serialize message is kept.
static StreamWriter SerializeToWriter(Connection connection, MessageType type, SerializeConnectionMethod handler, out int byteCount)Writes the message type and the handler's body into a shared writer and returns it with its byte count.
static StreamWriter StartSerialize(Connection connection, MessageType type)Gets the shared writer for the connection, clears it and writes the 5-bit message type.
static int StopSerialize(StreamWriter writer, Connection connection, MessageType type)Aligns and flushes the writer and returns the byte count.
void Update()Sends the current packet once the channel's maximum send interval has passed; otherwise only retries failed reliable packets.
protected virtual void ReleasePacket(Packet packet)Returns a packet to the shared pool (up to 512 kept).
protected virtual bool SendPacket(Packet packet)Sends one packet on the channel. Returns false if the transport refused it.
7 private and internal methods
MemberWhat it is
private bool AttemptSendAnyRemainingFailedPackets()Reliable channels: resends queued packets that failed earlier, in order, stopping at the first that fails again.
private static Packet CreatePacket()Makes a new empty packet of PacketSize.
private void PrepareCurrentPacket()Takes a packet from the pool (or makes one) and sets it as the current packet.
private void ReliableOverflow()Disconnects the connection with the reason "Reliable overflow". Called a moment after the failed queue fills up.
private bool WriteToCurrent(StreamWriter writer, int byteCount)Writes a message into the current packet, and sends straight away if the channel has no send interval.
private bool WriteToReliable(StreamWriter writer, int byteCount)Called when the current packet is full on a reliable channel: sends it or queues it as failed, then starts a new one. Disconnects the client when 64 packets are queued.
private bool WriteToUnreliable(StreamWriter writer, int byteCount)Called when the current packet is full on an unreliable channel: sends it and writes the message to the next packet.

Used in​

Members of other types that take, return or hold a OutgoingPacketManager: