summaryrefslogtreecommitdiffhomepage
path: root/include/ocl/net/unique_socket.hpp
blob: ed1d5496a44fcb75606afad5dfec145c61660413 (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * File: net/unique_socket.hpp
 * Purpose: RAII socket concept in modern C++
 * Author: Amlal El Mahrouss (amlal@nekernel.org)
 * Copyright 2025, Amlal El Mahrouss, licensed under the Boost Software License.
 */

#pragma once

#include <core/config.hpp>

#include <stdexcept>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string>
#include <cstddef>
#include <cstring>

#ifdef _WIN32
#error !!! "Windows is not supported yet for <socket>" !!!
#endif // _WIN32

namespace ocl::net
{
	class unique_socket final
	{
	public:
		using socket_type	 = int;
		using error_type	 = bool;
		using condition_type = bool;
		using socket_state	 = int;

	private:
		socket_type	   socket_{};
		condition_type is_server_{false};
		error_type	   bad_{false};

	public:
		unique_socket() = default;

		~unique_socket()
		{
			this->destroy();
		}

		unique_socket& operator=(const unique_socket&) = delete;
		unique_socket(const unique_socket&)			   = delete;

		unique_socket& operator=(unique_socket&& other) noexcept
		{
			if (this != &other)
			{
				destroy();
				socket_		  = other.socket_;
				is_server_	  = other.is_server_;
				bad_		  = other.bad_;
				other.socket_ = 0;
				other.bad_	  = true;
			}

			return *this;
		}

		unique_socket(unique_socket&& other) noexcept
			: socket_(other.socket_), is_server_(other.is_server_), bad_(other.bad_)
		{
			other.socket_ = 0;
			other.bad_	  = true;
		}

		static constexpr auto local_address_ip4 = "127.0.0.1";
		static constexpr auto backlog_count		= 5U;

		const error_type& bad()
		{
			return bad_;
		}

		unique_socket::socket_state state() noexcept
		{
			socket_state error = 0;
			socklen_t	 len   = sizeof(error);
			getsockopt(socket_, SOL_SOCKET, SO_ERROR, &error, &len);

			return error;
		}

		unique_socket accept() noexcept
		{
			socket_type cl_{-1};

			if (this->is_server_)
				cl_ = ::accept(socket_, nullptr, nullptr);

			unique_socket ret_sock;
			ret_sock.socket_ = cl_;

			return std::move(ret_sock);
		}

		unique_socket read_server_buffer(char* out, std::size_t len)
		{
			if (!out || !len)
				return {};

			if (!is_server_)
			{
				return {};
			}

			auto ret_sock = accept();

			if (ret_sock.socket_ == -1)
				throw std::invalid_argument("no connection to accept.");

			auto ret	  = ::recv(ret_sock.socket_, static_cast<void*>(out), len, 0);
			ret_sock.bad_ = ret < 0L;

			return ret_sock;
		}

		void read_client_buffer(char* out, std::size_t len)
		{
			if (!out || !len)
				return;

			if (is_server_)
				return;

			auto ret	  = ::recv(this->socket_, static_cast<void*>(out), len, 0);
			this->bad_ = ret < 0L;
		}

		void write_from_buffer(const char* out, std::size_t len)
		{
			if (!out)
				return;

			if (!len)
				return;

			auto ret = ::send(socket_, out, len, 0);

			bad_ = ret < 0L;
		}

		template <uint16_t port>
		static unique_socket make_socket(const std::string& address, const bool is_server)
		{
			unique_socket sock;

			if (!sock.construct<AF_INET, SOCK_STREAM, port>(address.c_str(), is_server))
				throw std::invalid_argument("invalid socket argument");

			return sock;
		}

	private:
		template <uint16_t af, uint16_t kind, uint16_t port>
		bool construct(const char* addr = unique_socket::local_address_ip4, const bool& is_server = false) noexcept
		{
			static_assert(af != 0, "Address family is zero");
			static_assert(kind != 0, "Kind is zero");

			socket_	   = ::socket(af, kind, 0);
			is_server_ = is_server;

			if (socket_ == -1)
				return false;

			struct sockaddr_in addr_;
			std::memset(&addr_, 0, sizeof(struct sockaddr_in));

			addr_.sin_addr.s_addr = ::inet_addr(addr);
			addr_.sin_port		  = htons(port);
			addr_.sin_family	  = af;

			if (!is_server)
			{
				const auto ret = ::connect(socket_, reinterpret_cast<struct sockaddr*>(&addr_), sizeof(addr_));
				return ret == 0L;
			}

			int ret = ::bind(socket_, (struct sockaddr*)&addr_, sizeof(addr_));

			bad_ = ret == -1;

			::listen(socket_, unique_socket::backlog_count);

			return bad_ == false;
		}

		bool destroy() noexcept
		{
			if (!socket_)
				return false;

			::shutdown(socket_, SHUT_RDWR);
			::close(socket_);

			socket_ = 0L;

			return true;
		}
	};
} // namespace ocl::net