ngscopeclient 0.1-dev+51fbda87c
Socket.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* ANTIKERNEL v0.1 *
4* *
5* Copyright (c) 2012-2023 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 <sys/socket.h>
53#include <unistd.h>
54#define ZSOCKLEN socklen_t
55#define ZSOCKET int
56
57#endif
58
62class Socket
63{
64public:
65 Socket(int af, int type, int protocol);
66
67 //Create a Socket object from an existing socket
68 Socket(ZSOCKET sock, int af = PF_INET);
69
70 //Destructor
71 virtual ~Socket(void);
72
73 //Connect to a host (automatic DNS resolution)
74 bool Connect(const std::string& host, uint16_t port);
75
76 //Bind to a port (any available interface)
77 bool Bind(unsigned short port);
78
79 //Put us in listening mode
80 bool Listen();
81
82 //Accept a new connection
83 Socket Accept(sockaddr_in* addr, ZSOCKLEN len);
84 Socket Accept(sockaddr_in6* addr, ZSOCKLEN len);
85 Socket Accept();
86
87 //Disconnect us from the socket object
88 ZSOCKET Detach();
89
90 // Flush the incoming socket
91 void FlushRxBuffer(void);
92
93 //Send / receive rawdata
94 bool SendLooped(const unsigned char* buf, int count);
95 bool RecvLooped(unsigned char* buf, int len);
96 //size_t RecvFrom(void* buf, size_t len, sockaddr_in& addr, int flags = 0);
97 //size_t SendTo(void* buf, size_t len, sockaddr_in& addr, int flags = 0);
98
99 //Send/receive a string
100 bool RecvPascalString(std::string& str);
101 bool SendPascalString(const std::string& str);
102
103 //Set TCP_NODELAY on our socket
104 bool DisableNagle();
105
106 //Set TCP_QUICKACK on our socket
107 bool DisableDelayedACK();
108
109 //Set SO_REUSEADDR on our socket
110 bool SetReuseaddr(bool on = true);
111
112 //Set RX/TX timeouts
113 bool SetRxTimeout(unsigned int microSeconds);
114 bool SetTxTimeout(unsigned int microSeconds);
115
116 //Set buffer sizes
117 bool SetTxBuffer(int bufsize);
118 bool SetRxBuffer(int bufsize);
119
124 operator const ZSOCKET&() const { return m_socket; }
125
126 bool IsValid() const
127 {
128#ifdef _WIN32
129 return (m_socket != INVALID_SOCKET);
130#else
131 return (m_socket >= 0);
132#endif
133 }
134
135 Socket& operator=(ZSOCKET rhs);
136
137 void Close();
138
139protected:
140 void Open();
141
145 int m_af;
146
149
152
153 double m_rxtimeout;
154
155 double m_txtimeout;
156
160 ZSOCKET m_socket;
161};
162
163#endif
Class representing a network socket.
Definition: Socket.h:63
int m_protocol
Protocol of the socket.
Definition: Socket.h:151
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:145
virtual ~Socket(void)
Closes a socket.
Definition: Socket.cpp:97
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:148
ZSOCKET m_socket
The socket handle.
Definition: Socket.h:160
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