File: NetStream.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (223 lines) | stat: -rw-r--r-- 4,861 bytes parent folder | download | duplicates (2)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "stdafx.h"
#include "NetStream.h"

#if defined(WINDOWS)
#include <mstcpip.h> // for tcp_keepalive
#endif

namespace storm {

	void Keepalive::toS(StrBuf *to) const {
		if (!enabled) {
			*to << S("<keepalive off>");
		} else if (idle.v <= 0 || interval.v <= 0) {
			*to << S("<keepalive on, default times>");
		} else {
			*to << S("<keepalive on, idle: ") << idle << S(", interval: ") << interval << S(">");
		}
	}

	NetStream::NetStream(os::Handle handle, os::Thread attachedTo, Address *peer)
		: Socket(handle, attachedTo), closed(0), peer(peer) {

		i = new (this) NetIStream(this, attachedTo);
		o = new (this) NetOStream(this, attachedTo);
	}

	void NetStream::deepCopy(CloneEnv *env) {
		cloned(i, env);
		cloned(o, env);
	}

	NetIStream *NetStream::input() const {
		return i;
	}

	NetOStream *NetStream::output() const {
		return o;
	}

	Bool NetStream::nodelay() const {
		int v = 0;
		getSocketOpt(handle, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
		return v != 0;
	}

	void NetStream::nodelay(Bool v) {
		int val = v ? 1 : 0;
		setSocketOpt(handle, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
	}

	Keepalive NetStream::keepalive() const {
		return alive;
	}

#if defined(WINDOWS)

	void NetStream::keepalive(Keepalive d) {
		alive = d;

		Bool ok = false;

		if (!alive.enabled) {
			// Just turn it off with a setsockopt.
			int enabled = 1;
			ok = setSocketOpt(handle, SOL_SOCKET, SO_KEEPALIVE, &enabled, sizeof(enabled));
		} else if (alive.enabled && !alive.defaultTimes()) {
			// Just turn it on with a setsockopt.
			int enabled = 0;
			ok = setSocketOpt(handle, SOL_SOCKET, SO_KEEPALIVE, &enabled, sizeof(enabled));
		} else {
			// Set times also:
			tcp_keepalive info = {
				1,
				ULONG(alive.idle.inMs()),
				ULONG(alive.interval.inMs()),
			};
			DWORD out = 0;
			int res = WSAIoctl((SOCKET)handle.v(), SIO_KEEPALIVE_VALS,
							&info, sizeof(info), // input
							NULL, NULL, &out, // no output
							NULL, NULL); // not OVERLAPPED
			ok = res == 0;
		}

		if (!ok)
			throw new (this) NetError(S("Failed to set keepalive information."));
	}

#elif defined(LINUX)

	void NetStream::keepalive(Keepalive d) {
		alive = d;

		int enabled = alive.enabled ? 1 : 0;
		if (!setSocketOpt(handle, SOL_SOCKET, SO_KEEPALIVE, &enabled, sizeof(enabled)))
			throw new (this) NetError(S("Failed to set SO_KEEPALIVE."));

		if (alive.enabled && !alive.defaultTimes()) {
			int idletime = alive.idle.inS();
			int idleinterval = alive.interval.inS();

			if (!setSocketOpt(handle, SOL_TCP, TCP_KEEPIDLE, &idletime, sizeof(idletime)))
				throw new (this) NetError(S("Failed to set TCP_KEEPIDLE."));
			if (!setSocketOpt(handle, SOL_TCP, TCP_KEEPINTVL, &idleinterval, sizeof(idleinterval)))
				throw new (this) NetError(S("Failed to set TCP_KEEPINTVL."));
		}
	}

#else
#error "TODO: Implement keepalive for your OS."
#endif

	void NetStream::close() {
		i->close();
		o->close();
	}

	void NetStream::closeEnd(Nat which) {
		Nat old, w;
		do {
			old = atomicRead(closed);
			w = old | which;
		} while (atomicCAS(closed, old, w) != old);

		if (w == (closeRead | closeWrite) && handle) {
			closeSocket(handle, attachedTo);
			handle = os::Handle();
		}
	}

	void NetStream::toS(StrBuf *to) const {
		Socket::toS(to);
		if (handle)
			*to << S(" <-> ") << peer;
	}

	/**
	 * Connect.
	 */

	MAYBE(NetStream *) connect(Address *to) {
		initSockets();

		sockaddr_storage data;
		sockaddr *sa = (sockaddr *)&data;
		to->fill(sa);

		os::Handle h = createTcpSocket(sa->sa_family);
		os::Thread current = os::Thread::current();
		current.attach(h);

		try {
			// Connect!
			bool ok = connectSocket(h, current, sa, sizeof(data));
			if (!ok)
				return null;

			return new (to) NetStream(h, current, to);
		} catch (...) {
			closeSocket(h, current);
			throw;
		}
	}

	MAYBE(NetStream *) connect(Str *host, Nat port) {
		Array<Address *> *found = lookupAddress(host);

		for (Nat i = 0; i < found->count(); i++) {
			Address *addr = found->at(i);
			if (!addr->port())
				addr = addr->withPort(port);

			if (NetStream *s = connect(addr))
				return s;
		}

		return null;
	}


	/**
	 * IStream.
	 */

	NetIStream::NetIStream(NetStream *owner, const os::Thread &t)
		: HandleTimeoutIStream(owner->handle, t), owner(owner) {
		skipClose();
		skipPos();
	}

	NetIStream::~NetIStream() {
		// The socket will close the handle.
		handle = os::Handle();
	}

	void NetIStream::close() {
		handle = os::Handle();
		owner->closeEnd(NetStream::closeRead);
	}


	/**
	 * OStream.
	 */

	NetOStream::NetOStream(NetStream *owner, const os::Thread &t)
		: HandleTimeoutOStream(owner->handle, t), owner(owner) {
		skipClose();
		skipPos();
	}

	NetOStream::~NetOStream() {
		// The socket will close the handle.
		handle = os::Handle();
	}

	void NetOStream::close() {
		handle = os::Handle();
		owner->closeEnd(NetStream::closeWrite);
	}


}