Wizard
Software Engineering Project - Wizard
Loading...
Searching...
No Matches
stream_socket.h
Go to the documentation of this file.
1
13// --------------------------------------------------------------------------
14// This file is part of the "sockpp" C++ socket library.
15//
16// Copyright (c) 2014-2017 Frank Pagliughi
17// All rights reserved.
18//
19// Redistribution and use in source and binary forms, with or without
20// modification, are permitted provided that the following conditions are
21// met:
22//
23// 1. Redistributions of source code must retain the above copyright notice,
24// this list of conditions and the following disclaimer.
25//
26// 2. Redistributions in binary form must reproduce the above copyright
27// notice, this list of conditions and the following disclaimer in the
28// documentation and/or other materials provided with the distribution.
29//
30// 3. Neither the name of the copyright holder nor the names of its
31// contributors may be used to endorse or promote products derived from this
32// software without specific prior written permission.
33//
34// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
35// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
36// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
38// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
41// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45// --------------------------------------------------------------------------
46
47#ifndef __sockpp_stream_socket_h
48#define __sockpp_stream_socket_h
49
50#include "sockpp/socket.h"
51#include <vector>
52
53namespace sockpp {
54
56
62class stream_socket : public socket
63{
65 using base = socket;
66
67protected:
69 friend class acceptor;
70
75 static socket_t create_handle(int domain) {
76 return (socket_t) ::socket(domain, COMM_TYPE, 0);
77 }
78
79public:
81 static constexpr int COMM_TYPE = SOCK_STREAM;
96 stream_socket(stream_socket&& sock) : base(std::move(sock)) {}
97
98
115 static stream_socket create(int domain, int protocol=0);
116
123 base::operator=(std::move(rhs));
124 return *this;
125 }
138 auto h = base::clone().release();
139 return stream_socket(h);
140 }
147 virtual ssize_t read(void *buf, size_t n);
157 virtual ssize_t read_n(void *buf, size_t n);
163 ssize_t read(const std::vector<iovec>& ranges);
171 virtual bool read_timeout(const std::chrono::microseconds& to);
179 template<class Rep, class Period>
180 bool read_timeout(const std::chrono::duration<Rep,Period>& to) {
181 return read_timeout(std::chrono::duration_cast<std::chrono::microseconds>(to));
182 }
189 virtual ssize_t write(const void *buf, size_t n);
197 virtual ssize_t write_n(const void *buf, size_t n);
205 virtual ssize_t write(const std::string& s) {
206 return write_n(s.data(), s.size());
207 }
213 virtual ssize_t write(const std::vector<iovec> &ranges);
221 virtual bool write_timeout(const std::chrono::microseconds& to);
229 template<class Rep, class Period>
230 bool write_timeout(const std::chrono::duration<Rep,Period>& to) {
231 return write_timeout(std::chrono::duration_cast<std::chrono::microseconds>(to));
232 }
233};
234
236
244template <typename ADDR>
246{
248 using base = stream_socket;
249
250public:
252 static constexpr sa_family_t ADDRESS_FAMILY = ADDR::ADDRESS_FAMILY;
254 using addr_t = ADDR;
271 : base(std::move(sock)) {}
277 : base(std::move(sock)) {}
284 base::operator=(std::move(rhs));
285 return *this;
286 }
292 stream_socket_tmpl create(int protocol=0) {
293 return stream_socket_tmpl(std::move(base::create(ADDRESS_FAMILY, protocol)));
294 }
307 static std::tuple<stream_socket_tmpl, stream_socket_tmpl> pair(int protocol=0) {
308 auto pr = base::pair(ADDRESS_FAMILY, COMM_TYPE, protocol);
309 return std::make_tuple<stream_socket_tmpl, stream_socket_tmpl>(
310 stream_socket_tmpl{std::get<0>(pr).release()},
311 stream_socket_tmpl{std::get<1>(pr).release()});
312 }
318 addr_t address() const { return addr_t(socket::address()); }
325};
326
328// end namespace sockpp
329}
330
331#endif // __sockpp_socket_h
332
Definition acceptor.h:64
Definition socket.h:85
socket_t release()
Definition socket.h:311
socket clone() const
Definition socket.cpp:129
static std::tuple< socket, socket > pair(int domain, int type, int protocol=0)
Definition socket.cpp:146
socket_t handle() const
Definition socket.h:259
socket()
Definition socket.h:183
sock_address_any address() const
Definition socket.cpp:195
sock_address_any peer_address() const
Definition socket.cpp:210
Definition stream_socket.h:246
ADDR addr_t
Definition stream_socket.h:254
stream_socket_tmpl(socket_t handle)
Definition stream_socket.h:264
addr_t address() const
Definition stream_socket.h:318
static std::tuple< stream_socket_tmpl, stream_socket_tmpl > pair(int protocol=0)
Definition stream_socket.h:307
stream_socket_tmpl create(int protocol=0)
Definition stream_socket.h:292
stream_socket_tmpl()
Definition stream_socket.h:258
static constexpr sa_family_t ADDRESS_FAMILY
Definition stream_socket.h:252
stream_socket_tmpl(stream_socket &&sock)
Definition stream_socket.h:270
stream_socket_tmpl(stream_socket_tmpl &&sock)
Definition stream_socket.h:276
addr_t peer_address() const
Definition stream_socket.h:324
stream_socket_tmpl & operator=(stream_socket_tmpl &&rhs)
Definition stream_socket.h:283
Definition stream_socket.h:63
bool write_timeout(const std::chrono::duration< Rep, Period > &to)
Definition stream_socket.h:230
static stream_socket create(int domain, int protocol=0)
Definition stream_socket.cpp:50
stream_socket & operator=(stream_socket &&rhs)
Definition stream_socket.h:122
bool read_timeout(const std::chrono::duration< Rep, Period > &to)
Definition stream_socket.h:180
virtual bool write_timeout(const std::chrono::microseconds &to)
virtual ssize_t write_n(const void *buf, size_t n)
Definition stream_socket.cpp:154
stream_socket(stream_socket &&sock)
Definition stream_socket.h:96
virtual bool read_timeout(const std::chrono::microseconds &to)
stream_socket(socket_t handle)
Definition stream_socket.h:91
virtual ssize_t write(const void *buf, size_t n)
Definition stream_socket.cpp:140
static socket_t create_handle(int domain)
Definition stream_socket.h:75
virtual ssize_t read_n(void *buf, size_t n)
Definition stream_socket.cpp:77
virtual ssize_t write(const std::string &s)
Definition stream_socket.h:205
stream_socket clone() const
Definition stream_socket.h:137
static constexpr int COMM_TYPE
Definition stream_socket.h:81
stream_socket()
Definition stream_socket.h:85
virtual ssize_t read(void *buf, size_t n)
Definition stream_socket.cpp:62
int socket_t
The OS socket handle.
Definition socket.h:60