/
dpNetwork.h
70 lines (60 loc) · 1.73 KB
/
dpNetwork.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
62
63
64
65
66
67
68
69
70
// created by i-saint
// distributed under Creative Commons Attribution (CC BY) license.
// https://github.com/i-saint/DynamicPatcher
#ifndef dpNetwork_h
#define dpNetwork_h
#include "dpFeatures.h"
#include "DynamicPatcher.h"
#ifdef dpWithNetwork
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif // WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <ws2tcpip.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#else // _WIN32
#endif // _WIN32
#include <cstdint>
class dpTCPSocket;
class dpCommunicator;
void dpInitializeNetwork();
void dpFinalizeNetwork();
bool dpRunTCPServer(uint16_t port, const std::function<bool (dpTCPSocket &client)> &handler);
class dpTCPSocket
{
public:
dpTCPSocket();
dpTCPSocket(SOCKET s, bool needs_shutdown=true);
dpTCPSocket(const char *host, uint16_t port);
~dpTCPSocket();
SOCKET getHandle() const;
bool open(const char *host, uint16_t port);
void close();
int read(void *buf, uint32_t buf_size);
int write(const void *data, uint32_t data_size);
private:
SOCKET m_socket;
bool m_needs_shutdown;
private:
// forbidden
dpTCPSocket(const dpTCPSocket &);
dpTCPSocket& operator=(const dpTCPSocket &);
};
class dpProtocolSocket
{
public:
dpProtocolSocket();
dpProtocolSocket(SOCKET s, bool needs_shutdown=true);
dpProtocolSocket(const char *host, uint16_t port);
~dpProtocolSocket();
bool open(const char *host, uint16_t port);
void close();
bool read(std::string &o_str);
bool write(const void *data, uint32_t data_size);
private:
dpTCPSocket m_socket;
};
#endif // dpWithNetwork
#endif // dpNetwork_h