File: Socket.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • 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 (64 lines) | stat: -rw-r--r-- 1,468 bytes parent folder | download
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
#include "stdafx.h"
#include "Socket.h"
#include "Core/Exception.h"

namespace storm {


	Socket::Socket(os::Handle handle, os::Thread attachedTo) : handle(handle), attachedTo(attachedTo) {
		assert(attachedTo != os::Thread::invalid, L"We need a thread!");
	}

	Socket::Socket(const Socket &o) : Object(o), attachedTo(os::Thread::invalid) {
		throw new (this) NotSupported(S("Copying a socket"));
	}

	Socket::~Socket() {
		if (handle)
			closeSocket(handle, attachedTo);
	}

	void Socket::close() {
		if (handle) {
			os::Handle toClose = handle;
			handle = os::Handle();
			closeSocket(toClose, attachedTo);
		}
	}

	Nat Socket::inputBufferSize() const {
		Nat out = 0;
		getSocketOpt(handle, SOL_SOCKET, SO_RCVBUF, &out, sizeof(out));
		return out;
	}

	void Socket::inputBufferSize(Nat size) {
		setSocketOpt(handle, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
	}

	Nat Socket::outputBufferSize() const {
		Nat out = 0;
		getSocketOpt(handle, SOL_SOCKET, SO_SNDBUF, &out, sizeof(out));
		return out;
	}

	void Socket::outputBufferSize(Nat size) {
		setSocketOpt(handle, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size));
	}

	void Socket::toS(StrBuf *to) const {
		*to << S("Socket: ");
		if (handle) {
			sockaddr_storage data;
			memset(&data, 0, sizeof(data));
			if (getSocketName(handle, (sockaddr *)&data, sizeof(data))) {
				*to << toStorm(engine(), (sockaddr *)&data);
			} else {
				*to << S("<unknown>");
			}
		} else {
			*to << S("<closed>");
		}
	}

}