File: Socket.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,316 kB
  • sloc: cpp: 543,954; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (101 lines) | stat: -rw-r--r-- 2,426 bytes parent folder | download | duplicates (4)
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "Socket.h"

#include "lib/streflop/streflop_cond.h"

#include "System/Log/ILog.h"
#include "System/StringUtil.h"


namespace netcode
{

asio::io_service netservice;

bool CheckErrorCode(asio::error_code& err)
{
	// connection reset can happen when host did not start up
	// before the client wants to connect
	if (!err || err.value() == asio::error::connection_reset ||
		err.value() == asio::error::try_again) { // this should only ever happen with async sockets, but testing indicates it happens anyway...
		return false;
	} else {
		LOG_L(L_WARNING, "Network error %i: %s", err.value(),
				err.message().c_str());
		return true;
	}
}

asio::ip::udp::endpoint ResolveAddr(const std::string& host, int port, asio::error_code* err)
{
	assert(err);
	using namespace asio;
	ip::address tempAddr = WrapIP(host, err);
	if (!*err)
		return ip::udp::endpoint(tempAddr, port);

	auto errBuf = *err; // WrapResolve() might clear err
	asio::io_service io_service;
	ip::udp::resolver resolver(io_service);
	ip::udp::resolver::query query(host, IntToString(port));
	auto iter = WrapResolve(resolver, query, err);
	ip::udp::resolver::iterator end;
	if (!*err && iter != end) {
		return *iter;
	}

	if (!*err) *err = errBuf;
	return ip::udp::endpoint(tempAddr, 0);
}


asio::ip::address WrapIP(const std::string& ip,
		asio::error_code* err)
{
	asio::ip::address addr;

	if (err == NULL) {
		addr = asio::ip::address::from_string(ip);
	} else {
		addr = asio::ip::address::from_string(ip, *err);
	}

	// (date of note: 08/05/10)
	// something in from_string() is invalidating the FPU flags
	// tested on win2k and linux (not happening there)
	streflop::streflop_init<streflop::Simple>();
	return addr;
}

asio::ip::udp::resolver::iterator WrapResolve(
		asio::ip::udp::resolver& resolver,
		asio::ip::udp::resolver::query& query,
		asio::error_code* err)
{
	asio::ip::udp::resolver::iterator resolveIt;

	if (err == NULL) {
		resolveIt = resolver.resolve(query);
	} else {
		resolveIt = resolver.resolve(query, *err);
	}

	// (date of note: 08/22/10)
	// something in resolve() is invalidating the FPU flags
	streflop::streflop_init<streflop::Simple>();
	return resolveIt;
}


asio::ip::address GetAnyAddress(const bool IPv6)
{
	if (IPv6) {
		return asio::ip::address_v6::any();
	}
	return asio::ip::address_v4::any();
}


} // namespace netcode