-
Notifications
You must be signed in to change notification settings - Fork 8
/
binaryio.h
61 lines (49 loc) · 1.02 KB
/
binaryio.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
#ifndef BINARYIO_H
#define BINARYIO_H
#include <errno.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <zlib.h>
#include <thread>
ssize_t recvall(int s, void *buf, size_t size, int flags);
ssize_t sendall(int s, void *buf, size_t size, int flags);
class BinaryReader
{
private:
int socket_fd;
public:
BinaryReader(int socket_fd) : socket_fd(socket_fd) {}
template <typename T>
T Read();
};
class BinaryWriter
{
private:
int socket_fd;
public:
BinaryWriter(int socket_fd) : socket_fd(socket_fd) {}
template <typename T>
void Write(T value);
};
template <typename T>
T BinaryReader::Read()
{
T value;
recvall(socket_fd, &value, sizeof(T), MSG_WAITALL);
return value;
}
template <typename T>
void BinaryWriter::Write(T value)
{
sendall(socket_fd, &value, sizeof(T), MSG_WAITALL);
}
#endif