Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JTCPSocket.hh
Go to the documentation of this file.
1#ifndef __JNET__JTCPSOCKET__
2#define __JNET__JTCPSOCKET__
3
4#include "JLang/JException.hh"
5
6#include "JSystem/JNetwork.hh"
7
8#include "JNet/JSocket.hh"
9#include "JNet/JHostname.hh"
10
11#define IPROTO_TCP 6
12
13#if !defined(TCP_KEEPIDLE) && defined(TCP_KEEPALIVE)
14#define TCP_KEEPIDLE TCP_KEEPALIVE
15#endif
16
17/**
18 * \author mdejong
19 */
20namespace JNET {}
21namespace JPP { using namespace JNET; }
22
23namespace JNET {
24
25 /**
26 * TCP socket.
27 */
28 class JTCPSocket :
29 public JSocket
30 {
31 public:
32 /**
33 * Default constructor.
34 */
36 JSocket(AF_INET, SOCK_STREAM)
37 {}
38
39
40 /**
41 * Constructor.
42 *
43 * \param server file descriptor of TCP server socket
44 */
45 JTCPSocket(const int server)
46 {
47 accept(server);
48 }
49
50
51 /**
52 * Set non-blocking of I/O.
53 *
54 * \param on true to enable non-blocking; false to disable
55 */
56 void setNonBlocking(const bool on)
57 {
58 const int flags = fcntl(getFileDescriptor(), F_GETFL, -1);
59 const int mask = FNDELAY;
60
61 if (flags == -1) {
62 THROW(JSocketException, "Get socket option failed " << errno);
63 }
64
65 if (((flags & mask) != mask && on) ||
66 ((flags & mask) != 0 && !on) ) {
67
68 if (fcntl(getFileDescriptor(), F_SETFL, flags ^ mask) < 0) {
69 THROW(JSocketException, "Set socket option failed " << errno);
70 }
71 }
72 }
73
74
75 /**
76 * Get non-blocking of I/O.
77 *
78 * \return true if enabled non-blocking; else false
79 */
80 bool getNonBlocking() const
81 {
82 const int flags = fcntl(getFileDescriptor(), F_GETFL, -1);
83 const int mask = FNDELAY;
84
85 if (flags == -1) {
86 THROW(JSocketException, "Get socket option failed " << errno);
87 }
88
89 return ((flags & mask) != 0);
90 }
91
92
93 /**
94 * Set the TCP idle time.
95 *
96 * \param t_s time [s]
97 */
98 void setKeepIdle(const int t_s)
99 {
100 setOption(IPROTO_TCP, TCP_KEEPIDLE, t_s);
101 }
102
103
104 /**
105 * Set the TCP idle count.
106 *
107 * \param count count
108 */
109 void setKeepCnt(const int count)
110 {
111 setOption(IPROTO_TCP, TCP_KEEPCNT, count);
112 }
113
114
115 /**
116 * Set the TCP interval time.
117 *
118 * \param t_s time [s]
119 */
120 void setKeepIntvl(const int t_s)
121 {
122 setOption(IPROTO_TCP, TCP_KEEPINTVL, t_s);
123 }
124
125
126 /**
127 * Set TCP no-delay.
128 *
129 * \param on true to set TCP no-delay; false to disable
130 */
131 void setTcpNoDelay(const bool on)
132 {
133 setOption(IPPROTO_TCP, TCP_NODELAY, int(on ? 1 : 0));
134 }
135
136
137 /**
138 * Get TCP no-delay.
139 *
140 * \return true if TCP no-delay; else false
141 */
142 bool getTcpNoDelay() const
143 {
144 return (getOption<int>(IPPROTO_TCP, TCP_NODELAY) == 1);
145 }
146
147
148 /**
149 * Accept connection from a server.
150 *
151 * \param server file descriptor of TCP server socket
152 */
153 void accept(const int server)
154 {
155 socklen_t size = sizeof(sockaddr_in);
156
157 fileDescriptor = ::accept(server, getSockaddr(), &size);
158 }
159
160
161 /**
162 * Connect to port on local host.
163 *
164 * \param port port number
165 */
166 void connect(const int port)
167 {
168 connect(INADDR_ANY, port);
169 }
170
171
172 /**
173 * Connect to port on specified host.
174 *
175 * \param hostname host name
176 */
177 void connect(const JHostname& hostname)
178 {
179 connect(hostname.hostname, hostname.port);
180 }
181
182
183 /**
184 * Connect to port on specified host.
185 *
186 * \param hostname host name
187 * \param port port number
188 */
189 void connect(const std::string& hostname, const int port)
190 {
191 connect(JSYSTEM::getIPnumber(hostname), port);
192 }
193
194
195 /**
196 * Connect to port on specified host.
197 *
198 * \param ip_number IP number
199 * \param port port number
200 */
201 void connect(const int ip_number, const int port)
202 {
203 setIPnumber(ip_number);
204 setPort(port);
205
206 if (::connect(getFileDescriptor(), getSockaddr(), sizeof(sockaddr_in)) < 0) {
207 THROW(JSocketException, "Socket connection failed " << JSYSTEM::getIPaddress(ip_number) << ":" << port << " / " << getFileDescriptor() << " " << errno);
208 }
209 }
210 };
211}
212
213#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Hostname and IP address functions.
Base class for interprocess communication.
#define IPROTO_TCP
Definition JTCPSocket.hh:11
int getFileDescriptor() const
Get file descriptor.
Exception for socket.
const sockaddr * getSockaddr() const
Get sockaddr.
void setIPnumber()
Set any IP number.
void setPort(const int port)
Set port number.
Socket class.
Definition JSocket.hh:41
void setOption(const int level, const int option, const T value)
Set socket option.
Definition JSocket.hh:274
TCP socket.
Definition JTCPSocket.hh:30
void connect(const JHostname &hostname)
Connect to port on specified host.
void setKeepIdle(const int t_s)
Set the TCP idle time.
Definition JTCPSocket.hh:98
JTCPSocket(const int server)
Constructor.
Definition JTCPSocket.hh:45
void setKeepCnt(const int count)
Set the TCP idle count.
bool getNonBlocking() const
Get non-blocking of I/O.
Definition JTCPSocket.hh:80
void connect(const int ip_number, const int port)
Connect to port on specified host.
void connect(const int port)
Connect to port on local host.
void connect(const std::string &hostname, const int port)
Connect to port on specified host.
void accept(const int server)
Accept connection from a server.
void setKeepIntvl(const int t_s)
Set the TCP interval time.
void setNonBlocking(const bool on)
Set non-blocking of I/O.
Definition JTCPSocket.hh:56
bool getTcpNoDelay() const
Get TCP no-delay.
JTCPSocket()
Default constructor.
Definition JTCPSocket.hh:35
void setTcpNoDelay(const bool on)
Set TCP no-delay.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
int getIPnumber()
Get IP number.
Definition JNetwork.hh:142
std::string getIPaddress()
Get IP address (decimal-dot notation).
Definition JNetwork.hh:178
Auxiliary data structure for hostname and port number.
Definition JHostname.hh:35
std::string hostname
Definition JHostname.hh:171