AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
socket.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2012 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-network-export.hpp"
23
24#include <chrono>
25#include <cstddef>
26#include <string>
27
28#include <sys/socket.h>
29
30#include "network/address.hpp"
31#include "network/ioException.hpp"
32
33
34namespace anch::network {
35
39 enum class Direction {
40
42 RECEPTION = SHUT_RD,
43
45 TRANSMISSION = SHUT_WR,
46
48 BOTH = SHUT_RDWR,
49
50 };
51
63 class ANCH_NETWORK_EXPORT Socket {
64
65 // Attributes +
66 private:
68 int _fd;
69 // Attributes -
70
71 // Constructors +
72 public:
76 Socket() noexcept;
77
83 explicit Socket(int fd) noexcept;
84
93 Socket(int family, anch::network::SocketType type);
94
98 Socket(const Socket&) = delete;
99
105 Socket(Socket&& other) noexcept;
106 // Constructors -
107
108 // Destructor +
109 public:
113 virtual ~Socket() noexcept;
114 // Destructor -
115
116 // Operators +
117 public:
121 Socket& operator=(const Socket&) = delete;
122
130 Socket& operator=(Socket&& other) noexcept;
131 // Operators -
132
133 // Methods +
134 public:
146 static Socket connect(const std::string& host, uint16_t port, anch::network::SocketType type);
147
160 static Socket bind(const std::string& host, uint16_t port, anch::network::SocketType type);
161
169 void listen(int backlog = SOMAXCONN);
170
181
192 std::size_t send(const char* data, std::size_t size);
193
202 void sendAll(const char* data, std::size_t size);
203
214 std::size_t receive(char* data, std::size_t size);
215
227 std::size_t sendTo(const char* data, std::size_t size, const anch::network::Address& to);
228
240 std::size_t receiveFrom(char* data, std::size_t size, anch::network::Address& from);
241
250
254 void close() noexcept;
255
263 void setReceiveTimeout(std::chrono::milliseconds timeout);
264
272 void setSendTimeout(std::chrono::milliseconds timeout);
273 // Methods -
274
275 // Accessors +
276 public:
285
294
300 int getDescriptor() const noexcept;
301
307 bool isOpen() const noexcept;
308 // Accessors -
309
310 };
311
312} // anch::network
Socket address (IPv4 or IPv6 address and port).
Definition address.hpp:54
void setSendTimeout(std::chrono::milliseconds timeout)
static Socket connect(const std::string &host, uint16_t port, anch::network::SocketType type)
void setReceiveTimeout(std::chrono::milliseconds timeout)
std::size_t receiveFrom(char *data, std::size_t size, anch::network::Address &from)
anch::network::Address getLocalAddress() const
void close() noexcept
void shutdown(anch::network::Direction how=anch::network::Direction::BOTH)
void sendAll(const char *data, std::size_t size)
static Socket bind(const std::string &host, uint16_t port, anch::network::SocketType type)
std::size_t send(const char *data, std::size_t size)
int getDescriptor() const noexcept
void listen(int backlog=SOMAXCONN)
anch::network::Address getPeerAddress() const
std::size_t receive(char *data, std::size_t size)
Socket accept(anch::network::Address *peer=NULL)
std::size_t sendTo(const char *data, std::size_t size, const anch::network::Address &to)
bool isOpen() const noexcept
Network namespace.
Definition address.hpp:30
Direction
Definition socket.hpp:39
@ RECEPTION
Definition socket.hpp:42
@ TRANSMISSION
Definition socket.hpp:45
@ BOTH
Definition socket.hpp:48
SocketType
Definition address.hpp:35
AnCH framework base namespace.
Definition app.hpp:30