forked from goatpig/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketObject.h
More file actions
132 lines (102 loc) · 3.24 KB
/
SocketObject.h
File metadata and controls
132 lines (102 loc) · 3.24 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2016, goatpig. //
// Distributed under the MIT license //
// See LICENSE-MIT or https://opensource.org/licenses/MIT //
// //
////////////////////////////////////////////////////////////////////////////////
#ifndef _SOCKETOBJ_H
#define _SOCKETOBJ_H
#include <sys/types.h>
#include <string>
#include <sstream>
#include <stdint.h>
#include <functional>
#include <memory>
#ifndef _WIN32
#include <poll.h>
#endif
#include "ThreadSafeClasses.h"
#include "bdmenums.h"
#include "log.h"
#include "SocketIncludes.h"
using namespace std;
///////////////////////////////////////////////////////////////////////////////
class BinarySocket
{
friend class FCGI_Server;
public:
typedef function<bool(vector<uint8_t>, exception_ptr)> ReadCallback;
typedef function<bool(const vector<uint8_t>&)> SequentialReadCallback;
protected:
const size_t maxread_ = 4*1024*1024;
struct sockaddr serv_addr_;
const string addr_;
const string port_;
private:
void readFromSocketThread(SOCKET, ReadCallback);
protected:
void writeToSocket(SOCKET, void*, size_t);
void readFromSocket(SOCKET, ReadCallback);
void setBlocking(SOCKET, bool);
void writeAndRead(SOCKET, uint8_t*, size_t,
SequentialReadCallback);
public:
static void closeSocket(SOCKET&);
SOCKET openSocket(bool blocking);
BinarySocket(const string& addr, const string& port);
bool testConnection(void);
virtual string writeAndRead(const string&, SOCKET sock = SOCK_MAX)
{
throw SocketError("not implemented, use the protected method instead");
}
virtual SocketType type(void) const { return SocketBinary; }
};
///////////////////////////////////////////////////////////////////////////////
class DedicatedBinarySocket : public BinarySocket
{
private:
SOCKET sockfd_ = SOCK_MAX;
public:
DedicatedBinarySocket(const string& addr, const string& port) :
BinarySocket(addr, port)
{}
~DedicatedBinarySocket(void) { BinarySocket::closeSocket(sockfd_); }
void closeSocket()
{
BinarySocket::closeSocket(sockfd_);
}
void writeToSocket(void* data, size_t len)
{
BinarySocket::writeToSocket(sockfd_, data, len);
}
void readFromSocket(BinarySocket::ReadCallback callback)
{
BinarySocket::readFromSocket(sockfd_, callback);
}
bool openSocket(bool blocking)
{
sockfd_ = BinarySocket::openSocket(blocking);
return isValid();
}
int getSocketName(struct sockaddr& sa)
{
#ifdef _WIN32
int namelen = sizeof(sa);
#else
unsigned int namelen = sizeof(sa);
#endif
return getsockname(sockfd_, &sa, &namelen);
}
int getPeerName(struct sockaddr& sa)
{
#ifdef _WIN32
int namelen = sizeof(sa);
#else
unsigned int namelen = sizeof(sa);
#endif
return getpeername(sockfd_, &sa, &namelen);
}
bool isValid(void) const { return sockfd_ != SOCK_MAX; }
};
#endif