File: tcp_socket.cpp

package info (click to toggle)
btanks 0.9.8083-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 43,616 kB
  • sloc: cpp: 46,425; ansic: 12,005; xml: 4,262; python: 313; sh: 13; makefile: 13
file content (183 lines) | stat: -rw-r--r-- 4,694 bytes parent folder | download | duplicates (5)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* M-runtime for c++
 * Copyright (C) 2005-2008 Vladimir Menshakov
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "tcp_socket.h"
#include <sys/types.h>
#include <string.h>


#ifdef _WINDOWS
#	include "Winsock2.h"
#else
#	include <sys/socket.h>
#	include <netinet/in.h>
#	include <netinet/tcp.h>
#	include <netinet/ip.h> /* superset of previous */
#	include <arpa/inet.h>
#	include <netdb.h>
#endif              

#ifdef _WINDOWS
#	ifndef socklen_t 
#		define socklen_t int
#	endif

#	ifndef in_addr_t
#		define in_addr_t unsigned long
#	endif
#endif


#include "net_exception.h"              

using namespace mrt;

TCPSocket::TCPSocket() {
	create(PF_INET, SOCK_STREAM, 0);

}

void TCPSocket::listen(const std::string &bindaddr, const unsigned port, const bool reuse) {
	int on = 1;
	if (reuse)
		setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));

	struct sockaddr_in addr;
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	if (!bindaddr.empty()) {
		addr.sin_addr.s_addr = inet_addr(bindaddr.c_str());
	}
	
	if (bind(_sock, (const sockaddr *)&addr, sizeof(addr)) == -1)
		throw_net(("bind"));
	
	if (::listen(_sock, 10) == -1)
		throw_net(("listen"));
}

void TCPSocket::connect(const addr &addr, const bool no_delay) {
	if (no_delay)
		noDelay();

	struct sockaddr_in sin_addr;
	memset(&sin_addr, 0, sizeof(sin_addr));
	sin_addr.sin_family = AF_INET;
	sin_addr.sin_port = htons(addr.port);
	sin_addr.sin_addr.s_addr = addr.ip;

	LOG_DEBUG(("connect %s:%d", inet_ntoa(sin_addr.sin_addr), addr.port));
	if (::connect(_sock, (const struct sockaddr*)&sin_addr, sizeof(sin_addr))	 == -1)
		throw_net(("connect"));

	_addr = addr;
}


void TCPSocket::connect(const std::string &host, const int port, const bool no_delay) {
	if (no_delay)
		noDelay();

	struct sockaddr_in addr;
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	addr.sin_addr.s_addr = inet_addr(host.c_str());

	//add gethostbyname here.
	if (addr.sin_addr.s_addr == (in_addr_t)-1) {
		//try to resolve host
		hostent *he = gethostbyname(host.c_str());
		if (he == NULL)
			throw_ex(("host '%s' was not found", host.c_str()));
		addr.sin_addr = *(struct in_addr*)(he->h_addr_list[0]);
	}
	
	LOG_DEBUG(("connect %s:%d", inet_ntoa(addr.sin_addr), port));
	if (::connect(_sock, (const struct sockaddr*)&addr, sizeof(addr))	 == -1)
		throw_net(("connect"));

	_addr.ip = addr.sin_addr.s_addr;
	_addr.port = port;
}

const int TCPSocket::send(const void *data, const int len) const {
#ifdef _WINDOWS
	return ::send(_sock, (const char *)data, len, 0);
#else
	return ::send(_sock, data, len, 0);
#endif
}
//void send(const mrt::Chunk &data) const;
const int TCPSocket::recv(void *data, const int len) const {
#ifdef _WINDOWS
	return ::recv(_sock, (char *)data, len, 0);
#else
	return ::recv(_sock, data, len, 0);
#endif
}

void TCPSocket::accept(TCPSocket &client) {
	struct sockaddr_in addr;
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	socklen_t len = sizeof(addr);
	int s = ::accept(_sock, (struct sockaddr *)&addr, &len);
	if (s == -1)
		throw_net(("accept"));
	client.close();
	client._sock = s;

	client._addr.ip = addr.sin_addr.s_addr;
	client._addr.port = ntohs(addr.sin_port); //real port
}

#ifdef _WINDOWS
#ifndef IP_TOS
#	define IP_TOS              3
#endif

#ifndef IPTOS_LOWDELAY
#	define IPTOS_LOWDELAY		0x10
#	define IPTOS_THROUGHPUT		0x08
#	define IPTOS_RELIABILITY	0x04
#endif
#endif

void TCPSocket::noDelay(const bool flag) {
TRY {
	if (_sock == -1)
		throw_ex(("noDelay on unitialized socket"));

	int value = flag?1:0;
	int r = setsockopt(_sock, IPPROTO_TCP, TCP_NODELAY, (char *)&value, sizeof(value));
	if (r < 0) 
		throw_net(("setsockopt(TCP_NODELAY)"));

#ifndef _WINDOWS
	if (flag) {	
		value = IPTOS_LOWDELAY;
		r = setsockopt(_sock, IPPROTO_IP, IP_TOS, (char *)&value, sizeof(value));
		if (r < 0) 
			throw_net(("setsockopt(TOS_LOWDELAY)"));
	}
#endif
} CATCH("noDelay", {});
}