]> Dogcows Code - chaz/yoink/blob - src/stlplus/portability/tcp_sockets.cpp
testing new non-autotools build system
[chaz/yoink] / src / stlplus / portability / tcp_sockets.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2
3 // Author: Andy Rushton
4 // Copyright: (c) Southampton University 1999-2004
5 // (c) Andy Rushton 2004-2009
6 // License: BSD License, see ../docs/license.html
7
8 ////////////////////////////////////////////////////////////////////////////////
9
10 #include "tcp_sockets.hpp"
11
12 ////////////////////////////////////////////////////////////////////////////////
13
14 namespace stlplus
15 {
16
17 //////////////////////////////////////////////////////////////////////////////
18 // TCP Connection
19
20
21 TCP_connection::TCP_connection(const IP_socket& socket) : IP_socket(socket)
22 {
23 }
24
25 TCP_connection::TCP_connection(void) : IP_socket(TCP)
26 {
27 }
28
29 unsigned short TCP_connection::port(void) const
30 {
31 return remote_port();
32 }
33
34 ////////////////////////////////////////////////////////////////////////////////
35 // Server
36
37 TCP_server::TCP_server(void) : IP_socket(TCP)
38 {
39 }
40
41 TCP_server::TCP_server(unsigned short port, unsigned short queue) : IP_socket(TCP)
42 {
43 initialise(port,queue);
44 }
45
46 bool TCP_server::initialise(unsigned short port, unsigned short queue)
47 {
48 if (!IP_socket::bind_any(port)) return false;
49 return IP_socket::listen(queue);
50 }
51
52 TCP_connection TCP_server::accept(void)
53 {
54 return TCP_connection(IP_socket::accept());
55 }
56
57 bool TCP_server::connection_ready(unsigned timeout)
58 {
59 return accept_ready(timeout);
60 }
61
62 TCP_connection TCP_server::connection(void)
63 {
64 return accept();
65 }
66
67 //////////////////////////////////////////////////////////////////////////////
68 // Client
69
70 TCP_client::TCP_client(void) : IP_socket(TCP)
71 {
72 }
73
74 TCP_client::TCP_client(const std::string& address, unsigned short port, unsigned int timeout) : IP_socket(TCP)
75 {
76 initialise(address,port,timeout);
77 }
78
79 TCP_client::TCP_client(unsigned long address, unsigned short port, unsigned int timeout) : IP_socket(TCP)
80 {
81 initialise(address,port,timeout);
82 }
83
84 bool TCP_client::initialise(unsigned long remote_address, unsigned short remote_port, unsigned int timeout)
85 {
86 if (!IP_socket::connect(remote_address, remote_port))
87 {
88 close();
89 return false;
90 }
91 if (timeout && !IP_socket::connected(timeout))
92 {
93 close();
94 return false;
95 }
96 return true;
97 }
98
99 bool TCP_client::initialise(const std::string& address, unsigned short remote_port, unsigned int timeout)
100 {
101 // lookup the address and convert it into an IP number
102 unsigned long remote_address = IP_socket::ip_lookup(address);
103 if (!remote_address) return false;
104 return initialise(remote_address, remote_port, timeout);
105 }
106
107 unsigned short TCP_client::port(void) const
108 {
109 return remote_port();
110 }
111
112 unsigned long TCP_client::address(void) const
113 {
114 return remote_address();
115 }
116
117 ////////////////////////////////////////////////////////////////////////////////
118
119 } // end namespace stlplus
This page took 0.034591 seconds and 4 git commands to generate.