ngscopeclient v0.1.1
Socket.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* ANTIKERNEL *
4* *
5* Copyright (c) 2012-2025 Andrew D. Zonenberg *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
34#ifndef Socket_h
35#define Socket_h
36
37#include <string>
38#include <stdint.h>
39
40//Pull in some OS specific stuff
41#ifdef _WIN32
42
43#include <ws2tcpip.h>
44#define ZSOCKLEN int
45#define ZSOCKET SOCKET
46
47#else
48
49#include <arpa/inet.h>
50#include <netdb.h>
51#include <netinet/in.h>
52#include <netinet/tcp.h>
53#include <sys/socket.h>
54#include <sys/ioctl.h>
55#include <unistd.h>
56#define ZSOCKLEN socklen_t
57#define ZSOCKET int
58
59#endif
60
64class Socket
65{
66public:
67 Socket(int af, int type, int protocol);
68
69 //Create a Socket object from an existing socket
70 Socket(ZSOCKET sock, int af = PF_INET);
71
72 //Destructor
73 virtual ~Socket(void);
74
75 //Connect to a host (automatic DNS resolution)
76 bool Connect(const std::string& host, uint16_t port);
77
78 //Bind to a port (any available interface)
79 bool Bind(unsigned short port);
80
81 //Put us in listening mode
82 bool Listen();
83
84 //Accept a new connection
85 Socket Accept(sockaddr_in* addr, ZSOCKLEN len);
86 Socket Accept(sockaddr_in6* addr, ZSOCKLEN len);
87 Socket Accept();
88
89 //Disconnect us from the socket object
90 ZSOCKET Detach();
91
92 // Flush the incoming socket
93 void FlushRxBuffer(void);
94
95 //Send / receive rawdata
96 bool SendLooped(const unsigned char* buf, int count);
97 bool RecvLooped(unsigned char* buf, int len);
98 //size_t RecvFrom(void* buf, size_t len, sockaddr_in& addr, int flags = 0);
99 //size_t SendTo(void* buf, size_t len, sockaddr_in& addr, int flags = 0);
100
101 //Send/receive a string
102 bool RecvPascalString(std::string& str);
103 bool SendPascalString(const std::string& str);
104
105 //Set TCP_NODELAY on our socket
106 bool DisableNagle();
107
108 //Set TCP_QUICKACK on our socket
109 bool DisableDelayedACK();
110
111 //Set SO_REUSEADDR on our socket
112 bool SetReuseaddr(bool on = true);
113
114 //Set RX/TX timeouts
115 bool SetRxTimeout(unsigned int microSeconds);
116 bool SetTxTimeout(unsigned int microSeconds);
117
118 //Set buffer sizes
119 bool SetTxBuffer(int bufsize);
120 bool SetRxBuffer(int bufsize);
121
122 //Peek if data is ready
123 size_t GetRxBytesAvailable() const;
124
129 operator const ZSOCKET&() const { return m_socket; }
130
131 bool IsValid() const
132 {
133#ifdef _WIN32
134 return (m_socket != INVALID_SOCKET);
135#else
136 return (m_socket >= 0);
137#endif
138 }
139
140 Socket& operator=(ZSOCKET rhs);
141
142 void Close();
143
144protected:
145 void Open();
146
150 int m_af;
151
154
157
158 double m_rxtimeout;
159
160 double m_txtimeout;
161
165 ZSOCKET m_socket;
166};
167
168#endif
Class representing a network socket.
Definition: Socket.h:65
int m_protocol
Protocol of the socket.
Definition: Socket.h:156
bool Connect(const std::string &host, uint16_t port)
Establishes a TCP connection to a remote host.
Definition: Socket.cpp:132
bool SendPascalString(const std::string &str)
Sends a string to a socket.
Definition: Socket.cpp:554
bool DisableDelayedACK()
Disable delayed-ACK so that we send ACKs immediately upon packet receipt.
Definition: Socket.cpp:610
void FlushRxBuffer(void)
Flush RX buffer.
Definition: Socket.cpp:348
Socket(int af, int type, int protocol)
Creates a socket.
Definition: Socket.cpp:54
int m_af
Address family of this socket (typically AF_INET or AF_INET6)
Definition: Socket.h:150
virtual ~Socket(void)
Closes a socket.
Definition: Socket.cpp:97
size_t GetRxBytesAvailable() const
Return the number of unread bytes in the RX buffer.
Definition: Socket.cpp:685
ZSOCKET Detach()
Detaches the socket from this object.
Definition: Socket.cpp:535
bool RecvLooped(unsigned char *buf, int len)
Recives data from a UDP socket.
Definition: Socket.cpp:293
bool Bind(unsigned short port)
Binds the socket to an address.
Definition: Socket.cpp:391
Socket Accept()
Accepts a connection on the socket.
Definition: Socket.cpp:475
bool Listen()
Puts the socket in listening mode.
Definition: Socket.cpp:432
bool RecvPascalString(std::string &str)
Reads a Pascal-style string from a socket.
Definition: Socket.cpp:576
bool SetReuseaddr(bool on=true)
Set SO_REUSEADDR on our socket, allowing binding to it again without waiting for timeout if our task ...
Definition: Socket.cpp:629
int m_type
Type of the socket.
Definition: Socket.h:153
ZSOCKET m_socket
The socket handle.
Definition: Socket.h:165
bool DisableNagle()
Disable the Nagle algorithm on the socket so that messages get sent right away.
Definition: Socket.cpp:596
bool SendLooped(const unsigned char *buf, int count)
Sends data over the socket.
Definition: Socket.cpp:206