OpenTTD Source  1.11.0-beta2
PacketWriter Struct Reference

Writing a savegame directly to a number of packets. More...

Inheritance diagram for PacketWriter:
SaveFilter

Public Member Functions

 PacketWriter (ServerNetworkGameSocketHandler *cs)
 Create the packet writer. More...
 
 ~PacketWriter ()
 Make sure everything is cleaned up.
 
void Destroy ()
 Begin the destruction of this packet writer. More...
 
bool HasPackets ()
 Checks whether there are packets. More...
 
PacketPopPacket ()
 Pop a single created packet from the queue with packets.
 
void AppendQueue ()
 Append the current packet to the queue.
 
void PrependQueue ()
 Prepend the current packet to the queue.
 
void Write (byte *buf, size_t size) override
 Write a given number of bytes into the savegame. More...
 
void Finish () override
 Prepare everything to finish writing the savegame.
 
- Public Member Functions inherited from SaveFilter
 SaveFilter (SaveFilter *chain)
 Initialise this filter. More...
 
virtual ~SaveFilter ()
 Make sure the writers are properly closed.
 

Data Fields

ServerNetworkGameSocketHandlercs
 Socket we are associated with.
 
Packetcurrent
 The packet we're currently writing to.
 
size_t total_size
 Total size of the compressed savegame.
 
Packetpackets
 Packet queue of the savegame; send these "slowly" to the client.
 
std::mutex mutex
 Mutex for making threaded saving safe.
 
std::condition_variable exit_sig
 Signal for threaded destruction of this packet writer.
 
- Data Fields inherited from SaveFilter
SaveFilterchain
 Chained to the (savegame) filters.
 

Detailed Description

Writing a savegame directly to a number of packets.

Definition at line 56 of file network_server.cpp.

Constructor & Destructor Documentation

◆ PacketWriter()

PacketWriter::PacketWriter ( ServerNetworkGameSocketHandler cs)
inline

Create the packet writer.

Parameters
csThe socket handler we're making the packets for.

Definition at line 68 of file network_server.cpp.

Member Function Documentation

◆ Destroy()

void PacketWriter::Destroy ( )
inline

Begin the destruction of this packet writer.

It can happen in two ways: in the first case the client disconnected while saving the map. In this case the saving has not finished and killed this PacketWriter. In that case we simply set cs to nullptr, triggering the appending to fail due to the connection problem and eventually triggering the destructor. In the second case the destructor is already called, and it is waiting for our signal which we will send. Only then the packets will be removed by the destructor.

Definition at line 100 of file network_server.cpp.

References lock.

Referenced by ServerNetworkGameSocketHandler::~ServerNetworkGameSocketHandler().

◆ HasPackets()

bool PacketWriter::HasPackets ( )
inline

Checks whether there are packets.

It's not 100% threading safe, but this is only asked for when checking whether there still is something to send. Then another call will be made to actually get the Packet, which will be the only one popping packets and thus eventually setting this on false.

Definition at line 123 of file network_server.cpp.

◆ Write()

void PacketWriter::Write ( byte *  buf,
size_t  len 
)
inlineoverridevirtual

Write a given number of bytes into the savegame.

Parameters
bufThe bytes to write.
lenThe number of bytes to write.

Implements SaveFilter.

Definition at line 166 of file network_server.cpp.

References Packet::buffer, lock, PACKET_SERVER_MAP_DATA, SEND_MTU, Packet::size, and SlError().


The documentation for this struct was generated from the following file: