Library of Assembled Shared Sources
socket.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44#include "lass_common.h"
45#include "socket.h"
46#include "../num/num_cast.h"
47
48#if LASS_HAVE_SYS_SOCKET_H && !defined(LASS_IO_SOCKET_HAVE_LIBC)
49# define LASS_IO_SOCKET_HAVE_LIBC
50#endif
51
52#if defined(LASS_IO_SOCKET_HAVE_WINSOCK)
54#elif defined(LASS_IO_SOCKET_HAVE_LIBC)
55# include "impl/socket_libc.inl"
56#else
57# error "[LASS BUILD MSG] Socket not supported for this platform"
58#endif
59
60namespace lass
61{
62namespace io
63{
64
65Socket::Socket()
66{
67 pimpl_ = new impl::SocketImpl;
68}
69
70
71
72Socket::~Socket()
73{
74 LASS_ASSERT(pimpl_);
75 delete pimpl_;
76 pimpl_ = 0;
77}
78
79
80
81void Socket::open()
82{
83 LASS_ASSERT(pimpl_);
84 pimpl_->openSocket();
85}
86
87
88
89void Socket::close()
90{
91 LASS_ASSERT(pimpl_);
92 pimpl_->closeSocket();
93}
94
95
96
97void Socket::bind(TPort port)
98{
99 bind("", port);
100}
101
102
103
104void Socket::bind(const std::string& address, TPort port)
105{
106 LASS_ASSERT(pimpl_);
107 pimpl_->bind(address, port);
108}
109
110
111
112/** Bind to the first available port in range [@a begin, @a end).
113* complexity: O(n)
114*/
115Socket::TPort Socket::bindInRange(TPort begin, TPort end)
116{
117 return bindInRange("", begin, end);
118}
119
120
121
122/** Bind to the first available port in range [@a begin, @a end).
123* complexity: O(n)
124*/
125Socket::TPort Socket::bindInRange(const std::string& address, TPort begin, TPort end)
126{
127 LASS_ASSERT(pimpl_);
128 for (TPort port = begin; port < end; ++port)
129 {
130 try
131 {
132 pimpl_->bind(address, port);
133 return port;
134 }
135 catch (const SocketError&)
136 {
137 }
138 }
139 LASS_THROW_EX(SocketError, "Failed to bind socket to any port in range [" << begin << ", " << end << ").");
140}
141
142
143
144void Socket::listen()
145{
146 LASS_ASSERT(pimpl_);
147 pimpl_->listen();
148}
149
150
151
152void Socket::accept(Socket& connection) const
153{
154 LASS_ASSERT(pimpl_);
155 LASS_ASSERT(connection.pimpl_);
156 pimpl_->accept(connection.pimpl_);
157}
158
159
160
161void Socket::connect(const std::string& address, TPort port)
162{
163 LASS_ASSERT(pimpl_);
164 pimpl_->connect(address, port);
165}
166
167
168
169std::string Socket::address() const
170{
171 LASS_ASSERT(pimpl_);
172 return pimpl_->address();
173}
174
175
176
177Socket::TPort Socket::port() const
178{
179 LASS_ASSERT(pimpl_);
180 return pimpl_->port();
181}
182
183
184
185int Socket::send(const void* begin, int length) const
186{
187 LASS_ASSERT(pimpl_);
188 return pimpl_->send(begin, length);
189}
190
191
192
193const void* Socket::send(const void* begin, const void* end) const
194{
195 LASS_ASSERT(pimpl_);
196 const char* first = static_cast<const char*>(begin);
197 const char* last = static_cast<const char*>(end);
198 const int length = num::numCast<int>(last - first);
199 const int sent = pimpl_->send(first, length);
200 LASS_ASSERT(sent >= 0);
201 return first + sent;
202}
203
204
205
206int Socket::receive(void* begin, int length) const
207{
208 LASS_ASSERT(pimpl_);
209 return pimpl_->receive(begin, length);
210}
211
212
213
214void* Socket::receive(void* begin, void* end) const
215{
216 LASS_ASSERT(pimpl_);
217 char* first = static_cast<char*>(begin);
218 char* last = static_cast<char*>(end);
219 const int length = num::numCast<int>(last - first);
220 const int sent = pimpl_->receive(first, length);
221 LASS_ASSERT(sent >= 0);
222 return first + sent;
223}
224
225
226
227int Socket::sizeSendBuffer() const
228{
229 LASS_ASSERT(pimpl_);
230 return pimpl_->sizeSendBuffer();
231}
232
233
234
235void Socket::swap(Socket& other)
236{
237 std::swap(pimpl_, other.pimpl_);
238}
239
240
241
242}
243}
244
245// EOF
TCP/IP socket.
Definition socket.h:76
TPort bindInRange(TPort begin, TPort end)
Bind to the first available port in range [begin, end).
Definition socket.cpp:115
streams, binary streams, vrmlstreams, ...
Library for Assembled Shared Sources.
Definition config.h:53