mn_basic_socket.hpp
Go to the documentation of this file.
1 /*
2 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
3 *Copyright (c) 2018 Amber-Sophia Schroeck
4 *
5 *The Mini Thread Library is free software; you can redistribute it and/or modify
6 *it under the terms of the GNU Lesser General Public License as published by
7 *the Free Software Foundation, version 3, or (at your option) any later version.
8 
9 *The Mini Thread Library is distributed in the hope that it will be useful, but
10 *WITHOUT ANY WARRANTY; without even the implied warranty of
11 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 *General Public License for more details.
13 *
14 *You should have received a copy of the GNU Lesser General Public
15 *License along with the Mini Thread Library; if not, see
16 *<https://www.gnu.org/licenses/>.
17 */
18 #ifndef _MINILIB_BASIC_SOCKET_HPP_
19 #define _MINILIB_BASIC_SOCKET_HPP_
20 
21 #include "../mn_config.hpp"
22 #include "../mn_autolock.hpp"
23 
24 #include <sdkconfig.h>
25 #include <lwip/api.h>
26 #include <lwip/sockets.h>
27 #include <lwip/udp.h>
28 
29 #include "mn_basic_endpoint.hpp"
30 
31 #ifndef UDPLITE_SEND_CSCOV
32 #define UDPLITE_SEND_CSCOV 0x01
33 #endif // UDPLITE_SEND_CSCOV
34 
35 #ifndef UDPLITE_RECV_CSCOV
36 #define UDPLITE_RECV_CSCOV 0x02
37 #endif // UDPLITE_RECV_CSCOV
38 
39 #define MNTHREAD_NET_INVALID_SOCKET -1
40 
41 namespace mn {
42  namespace net {
43 
55  public:
56  using handle_type = int;
57 
62  explicit basic_ip_socket(const handle_type& hndl) noexcept;
69  basic_ip_socket(const address_family& fam, const socket_type& type, const protocol_type& protocol) noexcept;
73  basic_ip_socket(const basic_ip_socket& other) noexcept;
77  virtual ~basic_ip_socket() noexcept;
78 
85  virtual bool open();
86 
87 
91  virtual void close() { lwip_close(m_iHandle); }
96  virtual int available();
97 
103  virtual void reset(const handle_type& hnd = MNTHREAD_NET_INVALID_SOCKET, bool bClosed = false);
104 
111  virtual bool initialized();
112 
120  int get_last_error(bool poolLast = true);
125  int get_handle() { return m_iHandle; }
130  virtual basic_ip_socket* get_copy() = 0;
131 
132 
134 
135 
136  virtual void swap(basic_ip_socket& rhs) noexcept {
137  mn::swap(m_iHandle, rhs.m_iHandle);
138  mn::swap(m_eFam, rhs.m_eFam);
139  mn::swap(m_eType, rhs.m_eType);
140  mn::swap(m_eProtocol, rhs.m_eProtocol);
141  mn::swap(m_bBlocked, rhs.m_bBlocked);
142  }
143 
148  void shutdown(const socket_shutdown_type& cmd);
149 
160  bool poll(const unsigned long& timeout, int mode);
165  void set_reuse_address(bool flag);
170  void set_rause_port(bool flag);
176  void set_linger(bool on, int seconds);
181  void set_no_delay(bool flag);
186  void set_keep_alive(bool flag);
191  void set_oob_inline(bool flag);
196  void set_blocking(bool flag);
201  void set_nocheak(bool value);
206  void set_send_buffer_size(int value);
211  void set_recive_buffer_size(int value);
212 
217  void set_send_timeout(int value);
222  void set_recive_timeout(int value);
223 
224 
229  bool get_reuse_address();
234  bool get_rause_port();
240  void get_linger(bool& on, int& seconds);
241 
246  bool get_no_delay();
251  bool get_keep_alive();
256  bool get_oob_inline();
261  bool get_nocheak();
262 
267  bool get_blocking() { return m_bBlocked; }
272  int get_send_buffer_size();
278 
283  int get_send_timeout();
288  int get_recive_timeout();
289  public:
297  int set_options(const socket_option_level& opt, const socket_option_name& name, int value);
298 
306  int set_options(const socket_option_level& opt, const socket_option_name& name, unsigned int value);
307 
315  int set_options(const socket_option_level& opt, const socket_option_name& name, basic_ip4_address value);
316 
324  int set_options(const socket_option_level& opt, const socket_option_name& name, basic_ip6_address value);
325 
333  int set_options(const socket_option_level& opt, const socket_option_name& name, bool value);
342  int set_options(const socket_option_level& opt, const socket_option_name& name,
343  void* value, uint32_t size);
344 
352  int get_option_int(const socket_option_level& opt, const socket_option_name& name);
360  bool get_option_bool(const socket_option_level& opt, const socket_option_name& name);
370  int get_option_raw(const socket_option_level& opt, const socket_option_name& name, void* value,
371  uint32_t size);
372  public:
376  int ioctl(const ioctl_request_type& request, int& arg);
380  int ioctl(const ioctl_request_type& request, void* arg);
381 
385  int set_fcntl(int arg);
386 
390  int get_fcntl();
391 
392  protected:
402  virtual bool open(const address_family& fam, const socket_type& type,
403  const protocol_type& protocol);
404 
405 
406  protected:
412 
425 
430  };
431 
432  }
433 }
434 
435 
436 
437 #endif // _MINILIB_BASIC_SOCKET_HPP_
This class represents an internet (IP) version 6 host address.
Definition: mn_basic_ip4_address.hpp:38
This class represents an internet (IP) version 6 host address.
Definition: mn_basic_ip6_address.hpp:49
Wrapper class around lwip implementation of a socket.
Definition: mn_basic_socket.hpp:54
virtual int available()
Returns the number of bytes available that can be read without causing the socket to block.
Definition: mn_basic_socket.cpp:62
int ioctl(const ioctl_request_type &request, int &arg)
A wrapper for the ioctl system call.
Definition: mn_basic_socket.cpp:413
int get_last_error(bool poolLast=true)
Get the saved copy of the last lwip error code.
Definition: mn_basic_socket.cpp:119
int get_fcntl()
A wrapper for the fcntl system call.
Definition: mn_basic_socket.cpp:429
void set_recive_timeout(int value)
Set the option socket_option_name::recive_timeout.
Definition: mn_basic_socket.cpp:314
virtual bool initialized()
If the socket initialized.
Definition: mn_basic_socket.cpp:113
bool get_rause_port()
Get the value of the option socket_option_name::reuse_port.
Definition: mn_basic_socket.cpp:328
virtual void swap(basic_ip_socket &rhs) noexcept
Definition: mn_basic_socket.hpp:136
int get_recive_timeout()
Set the option socket_option_name::recive_timeout.
Definition: mn_basic_socket.cpp:397
int get_option_int(const socket_option_level &opt, const socket_option_name &name)
Get a interger value of a given option.
Definition: mn_basic_socket.cpp:154
int get_option_raw(const socket_option_level &opt, const socket_option_name &name, void *value, uint32_t size)
Get a buffer value of a given option.
Definition: mn_basic_socket.cpp:174
address_family m_eFam
A saved / cached copy of the address family type of this socket.
Definition: mn_basic_socket.hpp:416
int get_send_timeout()
Set the option socket_option_name::send_timeout.
Definition: mn_basic_socket.cpp:390
bool get_nocheak()
Get the value of the option socket_option_name::no_check.
Definition: mn_basic_socket.cpp:370
void set_send_timeout(int value)
Set the option socket_option_name::send_timeout.
Definition: mn_basic_socket.cpp:307
void set_rause_port(bool flag)
Set the option socket_option_name::reuse_port.
Definition: mn_basic_socket.cpp:241
basic_ip_socket(const handle_type &hndl) noexcept
construtor from a raw handle
Definition: mn_basic_socket.cpp:27
bool get_no_delay()
Get the value of the option socket_option_name::no_delay.
Definition: mn_basic_socket.cpp:351
void set_keep_alive(bool flag)
Set the option socket_option_name::keepalive.
Definition: mn_basic_socket.cpp:265
bool get_blocking()
Is the socket blocking enabled.
Definition: mn_basic_socket.hpp:267
bool get_keep_alive()
Get the value of the option socket_option_name::keepalive.
Definition: mn_basic_socket.cpp:357
int get_recive_buffer_size()
Get the size of the recive buffer.
Definition: mn_basic_socket.cpp:383
virtual void reset(const handle_type &hnd=MNTHREAD_NET_INVALID_SOCKET, bool bClosed=false)
Reset the socket.
Definition: mn_basic_socket.cpp:73
bool get_oob_inline()
Get the value of the option socket_option_name::oob_inline.
Definition: mn_basic_socket.cpp:363
socket_type m_eType
A saved / cached copy of the socket type of this socket.
Definition: mn_basic_socket.hpp:420
int set_options(const socket_option_level &opt, const socket_option_name &name, int value)
Sets the socket option specified by level and option to the given integer value.
Definition: mn_basic_socket.cpp:126
int handle_type
Definition: mn_basic_socket.hpp:56
virtual ~basic_ip_socket() noexcept
deconstrutor, close the socket
Definition: mn_basic_socket.cpp:45
handle_type m_iHandle
Reference to the underlying socket handle for this socket.
Definition: mn_basic_socket.hpp:411
void shutdown(const socket_shutdown_type &cmd)
send shutdown
Definition: mn_basic_socket.cpp:437
void set_no_delay(bool flag)
Set the option socket_option_name::tcp_nodelay.
Definition: mn_basic_socket.cpp:259
virtual bool open()
Open the socket - only used when the socket not initialized.
Definition: mn_basic_socket.cpp:82
bool get_reuse_address()
Get the value of the option socket_option_name::reuse_addr.
Definition: mn_basic_socket.cpp:321
bool get_option_bool(const socket_option_level &opt, const socket_option_name &name)
Get a booles value of a given option.
Definition: mn_basic_socket.cpp:167
virtual void close()
Definition: mn_basic_socket.hpp:91
void set_send_buffer_size(int value)
Set the size of the send buffer.
Definition: mn_basic_socket.cpp:293
bool m_bBlocked
A saved / cached copy of a blocked flag.
Definition: mn_basic_socket.hpp:429
int get_send_buffer_size()
Get the size of the send buffer.
Definition: mn_basic_socket.cpp:376
bool poll(const unsigned long &timeout, int mode)
poll the socket
Definition: mn_basic_socket.cpp:443
void set_nocheak(bool value)
Set the option socket_option_name::no_check.
Definition: mn_basic_socket.cpp:286
void set_blocking(bool flag)
Set the socket for bloking.
Definition: mn_basic_socket.cpp:277
void set_reuse_address(bool flag)
Set the option socket_option_name::reuse_addr.
Definition: mn_basic_socket.cpp:234
int get_handle()
Get the raw socket handle.
Definition: mn_basic_socket.hpp:125
void get_linger(bool &on, int &seconds)
Get the value of the option socket_option_name::linger.
Definition: mn_basic_socket.cpp:338
basic_ip_socket & operator=(const basic_ip_socket &other)
Definition: mn_basic_socket.cpp:222
protocol_type m_eProtocol
A saved / cached copy of the protocal type of this socket.
Definition: mn_basic_socket.hpp:424
virtual basic_ip_socket * get_copy()=0
Get a copy of the socket wrapper (abstarct)
void set_oob_inline(bool flag)
Set the option socket_option_name::oob_inline.
Definition: mn_basic_socket.cpp:271
void set_linger(bool on, int seconds)
Set the option socket_option_name::linger.
Definition: mn_basic_socket.cpp:249
int set_fcntl(int arg)
A wrapper for the fcntl system call.
Definition: mn_basic_socket.cpp:421
void set_recive_buffer_size(int value)
Set the size of the recive buffer.
Definition: mn_basic_socket.cpp:300
#define MNTHREAD_NET_INVALID_SOCKET
Definition: mn_basic_socket.hpp:39
struct mn::memory::detail::ptr_difference type
socket_shutdown_type
Commands for lwip_shutdown.
Definition: mn_net_types.hpp:157
protocol_type
Defines socket protoco types for creating.
Definition: mn_net_types.hpp:60
socket_type
Defines socket socket types for creating.
Definition: mn_net_types.hpp:99
address_family
Defines socket address types for creating.
Definition: mn_net_types.hpp:41
socket_option_name
Items for socket option name for the mn_socket::set_options and mn_socket::get_options.
Definition: mn_net_types.hpp:209
socket_option_level
Items for socket option levels for the mn_socket::set_options and mn_socket::get_options.
Definition: mn_net_types.hpp:175
ioctl_request_type
Commands for ioctlsocket(), taken from the BSD file fcntl.h. lwip_ioctl only supports FIONREAD and FI...
Definition: mn_net_types.hpp:429
Definition: mn_allocator_typetraits.hpp:25
void swap(TAssignable &a, TAssignable &b)
Definition: mn_algorithm.hpp:312