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