/// <summary> /// Serializes a NetMessage to the reliable stream. /// If there is no current stream, one is prepared. /// If the current stream cannot fit the message, it is sent and a new stream is prepared. /// </summary> internal void SerializeReliableMessage(NetMessage message) { if (sendWindow.Count >= 512) { return; } if (sendStream == null) { InitializeStream(); } if (!NetSerializer.TryWriteMessage(sendStream, message)) { if (retryingSerialization) { NetLog.Warning("SerializeReliableMessage failed."); retryingSerialization = false; return; } retryingSerialization = true; FlushStream(); SerializeReliableMessage(message); } if (retryingSerialization) { retryingSerialization = false; } }
/// <summary> Deserializes incoming reliable stream into NetMessages, forwards them to the NetSocket, releases the stream, /// increments the remote sequence, and retries the out-of-order buffer when needed. </summary> private void DeliverStream(NetStream strm) { // Deserialize stream into individual messages and pass them to the socket: while (NetSerializer.CanReadMessage(strm)) { var message = NetSerializer.ReadNetMessage(strm); if (message == null) { NetLog.Error("Failed to parse reliable message from: " + Connection.Endpoint + " Pos: " + strm.Pos + " Size: " + strm.Size); break; } Connection.Socket.ReceiveMessage(message, Connection); } // Return stream to pool and update receive buffer distances: strm.Release(); LastAcceptedRemoteSequence++; if (recvBuffer.Count > 0) { DecrementReceiveBuffer(); } }