]> git.localhorst.tv Git - blank.git/blob - src/server/ChunkTransmitter.hpp
let the server wait a little more efficiently
[blank.git] / src / server / ChunkTransmitter.hpp
1 #ifndef BLANK_SERVER_CHUNKTRANSMITTER_HPP_
2 #define BLANK_SERVER_CHUNKTRANSMITTER_HPP_
3
4 #include <cstdint>
5 #include <memory>
6 #include <vector>
7
8
9 namespace blank {
10
11 class Chunk;
12
13 namespace server {
14
15 class ClientConnection;
16
17 class ChunkTransmitter {
18
19 public:
20         explicit ChunkTransmitter(ClientConnection &);
21         ~ChunkTransmitter();
22
23         /// Returns true if not transmitting or waiting on acks, so
24         /// the next chunk may be queued without schmutzing up anything.
25         bool Idle() const noexcept;
26
27         /// Returns true if a transmission is still going on,
28         /// meaning there's at least one packet that needs to
29         /// be sent.
30         bool Transmitting() const noexcept;
31         /// Send the next packet of the current chunk (if any).
32         void Transmit();
33
34         /// Returns true if there's one or more packets which
35         /// still have to be ack'd by the remote.
36         bool Waiting() const noexcept;
37         /// Mark packet with given sequence number as ack'd.
38         /// If all packets for the current chunk have been ack'd
39         /// the transmission is considered complete.
40         void Ack(std::uint16_t);
41         /// Mark packet with given sequence number as lost.
42         /// Its part of the chunk data should be resent.
43         void Nack(std::uint16_t);
44
45         /// Cancel the current transmission.
46         void Abort();
47         /// Start transmitting given chunk.
48         /// If there's a chunk already in transmission it will be
49         /// cancelled.
50         void Send(Chunk &);
51
52 private:
53         void SendBegin();
54         void SendData(std::size_t);
55         void Release();
56
57 private:
58         ClientConnection &conn;
59         Chunk *current;
60         std::size_t buffer_size;
61         std::unique_ptr<std::uint8_t[]> buffer;
62         std::size_t buffer_len;
63         std::size_t packet_len;
64         std::size_t cursor;
65         std::size_t num_packets;
66         int begin_packet;
67         std::vector<int> data_packets;
68         int confirm_wait;
69         std::uint32_t trans_id;
70         bool compressed;
71
72 };
73
74 }
75 }
76
77 #endif