public virtual void Serialize(NetworkBinaryWriter writer) { writer.Seek(HEADER_LENGTH, SeekOrigin.Current); var bodyStartPosition = writer.GetBaseStream().Position; SerializeBody(writer); var bodyLength = checked((ushort)(writer.GetBaseStream().Position - bodyStartPosition)); writer.Seek(-(HEADER_LENGTH + bodyLength), SeekOrigin.Current); SerializeHeader(writer, bodyLength); writer.Seek(bodyLength, SeekOrigin.Current); }
/// <summary> /// Writes a serialisable object to streamed data, /// appending to previously written streamed data. /// </summary> /// <param name="serializable">The object to serialise.</param> /// <param name="mode">What to serialise of the serialisable object.</param> /// <returns>The number of serialised bytes.</returns> public int Write(INetworkSerializable serializable, SerializationModeFlags mode) { switch (DataMode) { case DataModeType.Uninitialized: _writer = NetworkBinaryWriter.Create(new MemoryStream()); break; case DataModeType.SettingDataByWrite: break; default: throw new InvalidOperationException("Cannot Write() streamed data in mode " + DataMode); } DataMode = DataModeType.SettingDataByWrite; long oldPos = _writer.Seek(0, SeekOrigin.Current); serializable.Serialize(_writer, mode); long newPos = _writer.Seek(0, SeekOrigin.Current); return (int)(newPos - oldPos); }