-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathconnection.h
61 lines (48 loc) · 1.87 KB
/
connection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#pragma once
#include <cstdint>
#include <vector>
#include <memory>
#include <stdexcept>
// Not thread safe
class QubicConnection
{
public:
QubicConnection(const char* nodeIp, int nodePort);
~QubicConnection();
// Establish connection to mNodePort on node mNodeIp.
// May throw std::logic_error.
void resolveConnection();
// Receive at most sz bytes and write them to buffer. Return the actual number of received bytes.
// Should only return less than sz bytes on timeout, closed connection, or error.
int receiveData(uint8_t* buffer, int sz);
// Receive sz bytes and write them to buffer. Throws std::logic_error if sz bytes cannot be read.
int receiveAllDataOrThrowException(uint8_t* buffer, int sz);
// Send sz bytes contained in buffer.
int sendData(uint8_t* buffer, int sz);
//void receiveDataAll(std::vector<uint8_t>& buffer);
void getHandshakeData(std::vector<uint8_t>& buffer);
// Receive data of type T that is preceeded by a header.
// May throw std::logic_error.
template <typename T> T receivePacketWithHeaderAs();
// Receive data of type T without a header.
// May throw std::logic_error.
template <typename T> T receivePacketAs();
// Receive vector data of Ts where each T is preceeded by a header.
template <typename T> std::vector<T> getLatestVectorPacketAs();
private:
char mNodeIp[32];
int mNodePort;
int mSocket;
uint8_t mBuffer[0xFFFFFF];
std::vector<uint8_t> mHandshakeData; // storing handshake data after open a connection
};
typedef std::shared_ptr<QubicConnection> QCPtr;
static QCPtr make_qc(const char* nodeIp, int nodePort)
{
return std::make_shared<QubicConnection>(nodeIp, nodePort);
}
class EndResponseReceived : public std::runtime_error
{
public:
explicit EndResponseReceived(const char* message = "Received end response message") : std::runtime_error(message) {}
};