µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
net-stack.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2015-2023 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit/.
11 */
12
13#ifndef CMSIS_PLUS_POSIX_IO_NET_STACK_H_
14#define CMSIS_PLUS_POSIX_IO_NET_STACK_H_
15
16// ----------------------------------------------------------------------------
17
18#if defined(__cplusplus)
19
20// ----------------------------------------------------------------------------
21
22#if defined(OS_USE_OS_APP_CONFIG_H)
23#include <cmsis-plus/os-app-config.h>
24#endif
25
28
30
31#include <cstddef>
32#include <cassert>
33
34// ----------------------------------------------------------------------------
35
36#pragma GCC diagnostic push
37#if defined(__clang__)
38#pragma clang diagnostic ignored "-Wc++98-compat"
39#endif
40
41// ----------------------------------------------------------------------------
42
43namespace os
44{
45 namespace posix
46 {
47 // ------------------------------------------------------------------------
48
49 class io;
50 class socket;
51 class net_interface;
52
53 class net_stack_impl;
54
60 // ------------------------------------------------------------------------
61 // ----- Non-io, global file system functions -----
62 /* class */ socket*
63 socket (int domain, int type, int protocol);
64
69 // ------------------------------------------------------------------------
75#pragma GCC diagnostic push
76#if defined(__clang__)
77#elif defined(__GNUC__)
78#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
79#pragma GCC diagnostic ignored "-Wsuggest-final-types"
80#endif
82 {
83 // ----------------------------------------------------------------------
84
90 public:
91
92 net_stack (net_stack_impl& impl, const char* name);
93
98 // The rule of five.
99 net_stack (const net_stack&) = delete;
100 net_stack (net_stack&&) = delete;
101 net_stack&
102 operator= (const net_stack&) = delete;
103 net_stack&
104 operator= (net_stack&&) = delete;
105
110 virtual
111 ~net_stack ();
112
117 // ----------------------------------------------------------------------
123 public:
124
125 virtual class socket*
126 socket (int domain, int type, int protocol);
127
128 const char*
129 name (void) const;
130
131 void
132 add_deferred_socket (class socket* sock);
133
135 utils::double_list_links, &socket::deferred_links_>;
136
139
140 // ----------------------------------------------------------------------
141
142 template<typename T>
143 T*
144 allocate_socket (void);
145
146 template<typename T, typename L>
147 T*
148 allocate_socket (L& locker);
149
150 // --------------------------------------------------------------------
151 // Support functions.
152
154 interface (void) const;
155
157 impl (void) const;
158
163 // ----------------------------------------------------------------------
164 protected:
165
170 const char* name_ = nullptr;
171
172 net_stack_impl& impl_;
173
174 deferred_sockets_list_t deferred_sockets_list_;
175
180 // ----------------------------------------------------------------------
181 public:
182
187 // Intrusive node used to link this net stack to the
188 // net manager list.
189 // Must be public. The constructor clears the pointers.
190 utils::double_list_links net_manager_links_;
191
196 // ----------------------------------------------------------------------
197 protected:
198
203 // Statics.
204 using net_list = utils::intrusive_list<net_stack,
205 utils::double_list_links, &net_stack::net_manager_links_>;
206 static net_list net_list__;
207
212 };
213#pragma GCC diagnostic pop
214
215 // ========================================================================
216
218 {
219 // ----------------------------------------------------------------------
220
226 public:
227
229
234 // The rule of five.
235 net_stack_impl (const net_stack_impl&) = delete;
236 net_stack_impl (net_stack_impl&&) = delete;
238 operator= (const net_stack_impl&) = delete;
240 operator= (net_stack_impl&&) = delete;
241
246 virtual
248
253 // ----------------------------------------------------------------------
259 public:
260
261 virtual class socket*
262 do_socket (int domain, int type, int protocol) = 0;
263
264 // ----------------------------------------------------------------------
265 // Support functions.
266
268 interface (void) const;
269
274 // ----------------------------------------------------------------------
275 protected:
276
281 net_interface& interface_;
282
286 };
287
288 // ========================================================================
289
290 template<typename T>
292 {
293 // --------------------------------------------------------------------
294
295 public:
296
297 using value_type = T;
298
299 // --------------------------------------------------------------------
300
306 public:
307
308 template<typename ... Args>
309 net_stack_implementable (const char* name, net_interface& interface,
310 Args&&... args);
311
316 // The rule of five.
320 operator= (const net_stack_implementable&) = delete;
322 operator= (net_stack_implementable&&) = delete;
323
328 virtual
330
335 // --------------------------------------------------------------------
341 public:
342
343 // Support functions.
344
346 impl (void) const;
347
352 // --------------------------------------------------------------------
353 protected:
354
359 value_type impl_instance_;
360
364 };
365
366 // ========================================================================
367
368 template<typename T, typename L>
370 {
371 // --------------------------------------------------------------------
372
373 public:
374
375 using value_type = T;
376 using lockable_type = L;
377
378 // --------------------------------------------------------------------
379
385 public:
386
387 template<typename ... Args>
388 net_stack_lockable (const char* name, net_interface& interface,
389 lockable_type& locker, Args&&... args);
390
395 // The rule of five.
396 net_stack_lockable (const net_stack_lockable&) = delete;
399 operator= (const net_stack_lockable&) = delete;
401 operator= (net_stack_lockable&&) = delete;
402
407 virtual
409
414 // --------------------------------------------------------------------
420 public:
421
422 // TBD
423
424 // --------------------------------------------------------------------
425 // Support functions.
426
428 impl (void) const;
429
434 // --------------------------------------------------------------------
435 protected:
436
441 value_type impl_instance_;
442
446 };
447
448 // ==========================================================================
449 } /* namespace posix */
450} /* namespace os */
451
452// ===== Inline & template implementations ====================================
453
454namespace os
455{
456 namespace posix
457 {
458 // ------------------------------------------------------------------------
459
460 inline const char*
461 net_stack::name (void) const
462 {
463 return name_;
464 }
465
466 inline net_stack_impl&
467 net_stack::impl (void) const
468 {
469#pragma GCC diagnostic push
470#if defined(__clang__)
471#elif defined(__GNUC__)
472#pragma GCC diagnostic ignored "-Wuseless-cast"
473#pragma GCC diagnostic ignored "-Wnull-dereference"
474#endif
475 return static_cast<net_stack_impl&> (impl_);
476#pragma GCC diagnostic push
477 }
478
479 inline void
481 {
482 deferred_sockets_list_.link (*sock);
483 }
484
487 {
488 return deferred_sockets_list_;
489 }
490
491 template<typename T>
492 T*
494 {
495 using socket_type = T;
496
497 socket_type* sock;
498
499 if (deferred_sockets_list_.empty ())
500 {
501 sock = new socket_type (*this);
502 }
503 else
504 {
505 sock =
506 static_cast<socket_type*> (deferred_sockets_list_.unlink_head ());
507
508 // Call the constructor before reusing the object,
509 sock->~socket_type ();
510
511 // Placement new, run only the constructor.
512 new (sock) socket_type (*this);
513
514 // Deallocate all remaining elements in the list.
515 while (!deferred_sockets_list_.empty ())
516 {
517 socket_type* s =
518 static_cast<socket_type*> (deferred_sockets_list_.unlink_head ());
519
520 // Call the destructor and the deallocator.
521 delete s;
522 }
523 }
524 return sock;
525 }
526
527 template<typename T, typename L>
528 T*
530 {
531 using socket_type = T;
532
533 socket_type* sock;
534
535 if (deferred_sockets_list_.empty ())
536 {
537 sock = new socket_type (*this, locker);
538 }
539 else
540 {
541 sock =
542 static_cast<socket_type*> (deferred_sockets_list_.unlink_head ());
543
544 // Call the constructor before reusing the object,
545 sock->~socket_type ();
546
547 // Placement new, run only the constructor.
548 new (sock) socket_type (*this, locker);
549
550 // Deallocate all remaining elements in the list.
551 while (!deferred_sockets_list_.empty ())
552 {
553 socket_type* s =
554 static_cast<socket_type*> (deferred_sockets_list_.unlink_head ());
555
556 // Call the destructor and the deallocator.
557 delete s;
558 }
559 }
560 return sock;
561 }
562
563 // ========================================================================
564
565 inline net_interface&
567 {
568 return interface_;
569 }
570
571 // ========================================================================
572
573 template<typename T>
574 template<typename ... Args>
576 const char* name, net_interface& interface, Args&&... args) :
578 { impl_instance_, name }, //
579 impl_instance_
580 { interface, std::forward<Args>(args)... }
581 {
582#if defined(OS_TRACE_POSIX_IO_NET_STACK)
583 trace::printf ("net_stack_implementable::%s(\"%s\")=@%p\n", __func__,
584 name_, this);
585#endif
586 }
587
588 template<typename T>
590 {
591#if defined(OS_TRACE_POSIX_IO_NET_STACK)
592 trace::printf ("net_stack_implementable::%s() @%p %s\n", __func__, this,
593 name_);
594#endif
595 }
596
597 template<typename T>
600 {
601 return static_cast<value_type&> (impl_);
602 }
603
604 // ========================================================================
605
606 template<typename T, typename L>
607 template<typename ... Args>
609 net_interface& interface,
610 lockable_type& locker,
611 Args&&... args) :
613 { impl_instance_, name }, //
614 impl_instance_
615 { interface, locker, std::forward<Args>(args)... }
616 {
617#if defined(OS_TRACE_POSIX_IO_NET_STACK)
618 trace::printf ("net_stack_lockable::%s()=%p\n", __func__, this);
619#endif
620 }
621
622 template<typename T, typename L>
624 {
625#if defined(OS_TRACE_POSIX_IO_NET_STACK)
626 trace::printf ("net_stack_lockable::%s() @%p\n", __func__, this);
627#endif
628 }
629
630 // ------------------------------------------------------------------------
631
632 template<typename T, typename L>
635 {
636 return static_cast<value_type&> (impl_);
637 }
638
639 // ==========================================================================
640 } /* namespace posix */
641} /* namespace os */
642
643#pragma GCC diagnostic pop
644
645// ----------------------------------------------------------------------------
646
647#endif /* __cplusplus */
648
649// ----------------------------------------------------------------------------
650
651#endif /* CMSIS_PLUS_POSIX_IO_NET_STACK_H_ */
Network interface class.
virtual class socket * do_socket(int domain, int type, int protocol)=0
net_interface & interface(void) const
Definition net-stack.h:566
net_stack_implementable(const char *name, net_interface &interface, Args &&... args)
Definition net-stack.h:575
value_type & impl(void) const
Definition net-stack.h:599
net_stack_lockable(const char *name, net_interface &interface, lockable_type &locker, Args &&... args)
Definition net-stack.h:608
value_type & impl(void) const
Definition net-stack.h:634
Network stack class.
Definition net-stack.h:82
net_stack_impl & impl(void) const
Definition net-stack.h:467
T * allocate_socket(void)
Definition net-stack.h:493
net_interface & interface(void) const
void add_deferred_socket(class socket *sock)
Definition net-stack.h:480
const char * name(void) const
Definition net-stack.h:461
deferred_sockets_list_t & deferred_sockets_list(void)
Definition net-stack.h:486
Network socket.
List of intrusive nodes.
Definition lists.h:705
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:60
int socket(int domain, int type, int protocol)
System namespace.