-
Notifications
You must be signed in to change notification settings - Fork 27
/
Socket.h
163 lines (128 loc) · 5.85 KB
/
Socket.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/***********************************************************************************************************************
* *
* ANTIKERNEL v0.1 *
* *
* Copyright (c) 2012-2023 Andrew D. Zonenberg *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
* following disclaimer. *
* *
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
* following disclaimer in the documentation and/or other materials provided with the distribution. *
* *
* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
* derived from this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
***********************************************************************************************************************/
/**
@file Socket.h
@brief Declaration of Socket class
*/
#ifndef Socket_h
#define Socket_h
#include <string>
#include <stdint.h>
//Pull in some OS specific stuff
#ifdef _WIN32
#include <ws2tcpip.h>
#define ZSOCKLEN int
#define ZSOCKET SOCKET
#else
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#define ZSOCKLEN socklen_t
#define ZSOCKET int
#endif
/**
@brief Class representing a network socket.
*/
class Socket
{
public:
Socket(int af, int type, int protocol);
//Create a Socket object from an existing socket
Socket(ZSOCKET sock, int af = PF_INET);
//Destructor
virtual ~Socket(void);
//Connect to a host (automatic DNS resolution)
bool Connect(const std::string& host, uint16_t port);
//Bind to a port (any available interface)
bool Bind(unsigned short port);
//Put us in listening mode
bool Listen();
//Accept a new connection
Socket Accept(sockaddr_in* addr, ZSOCKLEN len);
Socket Accept(sockaddr_in6* addr, ZSOCKLEN len);
Socket Accept();
//Disconnect us from the socket object
ZSOCKET Detach();
// Flush the incoming socket
void FlushRxBuffer(void);
//Send / receive rawdata
bool SendLooped(const unsigned char* buf, int count);
bool RecvLooped(unsigned char* buf, int len);
//size_t RecvFrom(void* buf, size_t len, sockaddr_in& addr, int flags = 0);
//size_t SendTo(void* buf, size_t len, sockaddr_in& addr, int flags = 0);
//Send/receive a string
bool RecvPascalString(std::string& str);
bool SendPascalString(const std::string& str);
//Set TCP_NODELAY on our socket
bool DisableNagle();
//Set TCP_QUICKACK on our socket
bool DisableDelayedACK();
//Set SO_REUSEADDR on our socket
bool SetReuseaddr(bool on = true);
//Set RX/TX timeouts
bool SetRxTimeout(unsigned int microSeconds);
bool SetTxTimeout(unsigned int microSeconds);
//Set buffer sizes
bool SetTxBuffer(int bufsize);
bool SetRxBuffer(int bufsize);
/**
@brief Convert us to the native OS socket type
@return A reference to our socket handle
*/
operator const ZSOCKET&() const { return m_socket; }
bool IsValid() const
{
#ifdef _WIN32
return (m_socket != INVALID_SOCKET);
#else
return (m_socket >= 0);
#endif
}
Socket& operator=(ZSOCKET rhs);
void Close();
protected:
void Open();
/**
@brief Address family of this socket (typically AF_INET or AF_INET6)
*/
int m_af;
/// @brief Type of the socket
int m_type;
/// @brief Protocol of the socket
int m_protocol;
double m_rxtimeout;
double m_txtimeout;
/**
@brief The socket handle
*/
ZSOCKET m_socket;
};
#endif