µ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 Liviu Ionescu.
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28#ifndef CMSIS_PLUS_POSIX_IO_NET_STACK_H_
29#define CMSIS_PLUS_POSIX_IO_NET_STACK_H_
30
31// ----------------------------------------------------------------------------
32
33#if defined(__cplusplus)
34
35// ----------------------------------------------------------------------------
36
37#if defined(OS_USE_OS_APP_CONFIG_H)
38#include <cmsis-plus/os-app-config.h>
39#endif
40
43
45
46#include <cstddef>
47#include <cassert>
48
49// ----------------------------------------------------------------------------
50
51#pragma GCC diagnostic push
52
53#if defined(__clang__)
54#pragma clang diagnostic ignored "-Wc++98-compat"
55#endif
56
57// ----------------------------------------------------------------------------
58
59namespace os
60{
61 namespace posix
62 {
63 // ------------------------------------------------------------------------
64
65 class io;
66 class socket;
67 class net_interface;
68
69 class net_stack_impl;
70
76 // ------------------------------------------------------------------------
77 // ----- Non-io, global file system functions -----
78 class socket*
79 socket (int domain, int type, int protocol);
80
85 // ------------------------------------------------------------------------
92 {
93 // ----------------------------------------------------------------------
94
100 public:
101
102 net_stack (net_stack_impl& impl, const char* name);
103
108 // The rule of five.
109 net_stack (const net_stack&) = delete;
110 net_stack (net_stack&&) = delete;
111 net_stack&
112 operator= (const net_stack&) = delete;
113 net_stack&
114 operator= (net_stack&&) = delete;
115
120 virtual
121 ~net_stack ();
122
127 // ----------------------------------------------------------------------
133 public:
134
135 virtual class socket*
136 socket (int domain, int type, int protocol);
137
138 const char*
139 name (void) const;
140
141 void
142 add_deferred_socket (class socket* sock);
143
145 utils::double_list_links, &socket::deferred_links_>;
146
149
150 // ----------------------------------------------------------------------
151
152 template<typename T>
153 T*
154 allocate_socket (void);
155
156 template<typename T, typename L>
157 T*
158 allocate_socket (L& locker);
159
160 // --------------------------------------------------------------------
161 // Support functions.
162
164 interface (void) const;
165
167 impl (void) const;
168
173 // ----------------------------------------------------------------------
174 protected:
175
180 const char* name_ = nullptr;
181
182 net_stack_impl& impl_;
183
184 deferred_sockets_list_t deferred_sockets_list_;
185
190 // ----------------------------------------------------------------------
191 public:
192
197 // Intrusive node used to link this net stack to the
198 // net manager list.
199 // Must be public. The constructor clears the pointers.
200 utils::double_list_links net_manager_links_;
201
206 // ----------------------------------------------------------------------
207 protected:
208
213 // Statics.
214 using net_list = utils::intrusive_list<net_stack,
215 utils::double_list_links, &net_stack::net_manager_links_>;
216 static net_list net_list__;
217
222 };
223
224 // ========================================================================
225
227 {
228 // ----------------------------------------------------------------------
229
235 public:
236
238
243 // The rule of five.
244 net_stack_impl (const net_stack_impl&) = delete;
245 net_stack_impl (net_stack_impl&&) = delete;
247 operator= (const net_stack_impl&) = delete;
249 operator= (net_stack_impl&&) = delete;
250
255 virtual
257
262 // ----------------------------------------------------------------------
268 public:
269
270 virtual class socket*
271 do_socket (int domain, int type, int protocol) = 0;
272
273 // ----------------------------------------------------------------------
274 // Support functions.
275
277 interface (void) const;
278
283 // ----------------------------------------------------------------------
284 protected:
285
290 net_interface& interface_;
291
295 };
296
297 // ========================================================================
298
299 template<typename T>
301 {
302 // --------------------------------------------------------------------
303
304 public:
305
306 using value_type = T;
307
308 // --------------------------------------------------------------------
309
315 public:
316
317 template<typename ... Args>
318 net_stack_implementable (const char* name, net_interface& interface,
319 Args&&... args);
320
325 // The rule of five.
329 operator= (const net_stack_implementable&) = delete;
331 operator= (net_stack_implementable&&) = delete;
332
337 virtual
339
344 // --------------------------------------------------------------------
350 public:
351
352 // Support functions.
353
355 impl (void) const;
356
361 // --------------------------------------------------------------------
362 protected:
363
368 value_type impl_instance_;
369
373 };
374
375 // ========================================================================
376
377 template<typename T, typename L>
379 {
380 // --------------------------------------------------------------------
381
382 public:
383
384 using value_type = T;
385 using lockable_type = L;
386
387 // --------------------------------------------------------------------
388
394 public:
395
396 template<typename ... Args>
397 net_stack_lockable (const char* name, net_interface& interface,
398 lockable_type& locker, Args&&... args);
399
404 // The rule of five.
405 net_stack_lockable (const net_stack_lockable&) = delete;
408 operator= (const net_stack_lockable&) = delete;
410 operator= (net_stack_lockable&&) = delete;
411
416 virtual
418
423 // --------------------------------------------------------------------
429 public:
430
431 // TBD
432
433 // --------------------------------------------------------------------
434 // Support functions.
435
437 impl (void) const;
438
443 // --------------------------------------------------------------------
444 protected:
445
450 value_type impl_instance_;
451
455 };
456
457 // ==========================================================================
458 } /* namespace posix */
459} /* namespace os */
460
461// ===== Inline & template implementations ====================================
462
463namespace os
464{
465 namespace posix
466 {
467 // ------------------------------------------------------------------------
468
469 inline const char*
470 net_stack::name (void) const
471 {
472 return name_;
473 }
474
475 inline net_stack_impl&
476 net_stack::impl (void) const
477 {
478 return static_cast<net_stack_impl&> (impl_);
479 }
480
481 inline void
483 {
484 deferred_sockets_list_.link (*sock);
485 }
486
489 {
490 return deferred_sockets_list_;
491 }
492
493 template<typename T>
494 T*
496 {
497 using socket_type = T;
498
499 socket_type* sock;
500
501 if (deferred_sockets_list_.empty ())
502 {
503 sock = new socket_type (*this);
504 }
505 else
506 {
507 sock =
508 static_cast<socket_type*> (deferred_sockets_list_.unlink_head ());
509
510 // Call the constructor before reusing the object,
511 sock->~socket_type ();
512
513 // Placement new, run only the constructor.
514 new (sock) socket_type (*this);
515
516 // Deallocate all remaining elements in the list.
517 while (!deferred_sockets_list_.empty ())
518 {
519 socket_type* s =
520 static_cast<socket_type*> (deferred_sockets_list_.unlink_head ());
521
522 // Call the destructor and the deallocator.
523 delete s;
524 }
525 }
526 return sock;
527 }
528
529 template<typename T, typename L>
530 T*
532 {
533 using socket_type = T;
534
535 socket_type* sock;
536
537 if (deferred_sockets_list_.empty ())
538 {
539 sock = new socket_type (*this, locker);
540 }
541 else
542 {
543 sock =
544 static_cast<socket_type*> (deferred_sockets_list_.unlink_head ());
545
546 // Call the constructor before reusing the object,
547 sock->~socket_type ();
548
549 // Placement new, run only the constructor.
550 new (sock) socket_type (*this, locker);
551
552 // Deallocate all remaining elements in the list.
553 while (!deferred_sockets_list_.empty ())
554 {
555 socket_type* s =
556 static_cast<socket_type*> (deferred_sockets_list_.unlink_head ());
557
558 // Call the destructor and the deallocator.
559 delete s;
560 }
561 }
562 return sock;
563 }
564
565 // ========================================================================
566
567 inline net_interface&
569 {
570 return interface_;
571 }
572
573 // ========================================================================
574
575 template<typename T>
576 template<typename ... Args>
578 const char* name, net_interface& interface, Args&&... args) :
580 { impl_instance_, name }, //
581 impl_instance_
582 { interface, std::forward<Args>(args)... }
583 {
584#if defined(OS_TRACE_POSIX_IO_NET_STACK)
585 trace::printf ("net_stack_implementable::%s(\"%s\")=@%p\n", __func__,
586 name_, this);
587#endif
588 }
589
590 template<typename T>
592 {
593#if defined(OS_TRACE_POSIX_IO_NET_STACK)
594 trace::printf ("net_stack_implementable::%s() @%p %s\n", __func__, this,
595 name_);
596#endif
597 }
598
599 template<typename T>
602 {
603 return static_cast<value_type&> (impl_);
604 }
605
606 // ========================================================================
607
608 template<typename T, typename L>
609 template<typename ... Args>
611 net_interface& interface,
612 lockable_type& locker,
613 Args&&... args) :
615 { impl_instance_, name }, //
616 impl_instance_
617 { interface, locker, std::forward<Args>(args)... }
618 {
619#if defined(OS_TRACE_POSIX_IO_NET_STACK)
620 trace::printf ("net_stack_lockable::%s()=%p\n", __func__, this);
621#endif
622 }
623
624 template<typename T, typename L>
626 {
627#if defined(OS_TRACE_POSIX_IO_NET_STACK)
628 trace::printf ("net_stack_lockable::%s() @%p\n", __func__, this);
629#endif
630 }
631
632 // ------------------------------------------------------------------------
633
634 template<typename T, typename L>
637 {
638 return static_cast<value_type&> (impl_);
639 }
640
641 // ==========================================================================
642 } /* namespace posix */
643} /* namespace os */
644
645#pragma GCC diagnostic pop
646
647// ----------------------------------------------------------------------------
648
649#endif /* __cplusplus */
650
651// ----------------------------------------------------------------------------
652
653#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:568
net_stack_implementable(const char *name, net_interface &interface, Args &&... args)
Definition net-stack.h:577
value_type & impl(void) const
Definition net-stack.h:601
net_stack_lockable(const char *name, net_interface &interface, lockable_type &locker, Args &&... args)
Definition net-stack.h:610
value_type & impl(void) const
Definition net-stack.h:636
Network stack class.
Definition net-stack.h:92
net_stack_impl & impl(void) const
Definition net-stack.h:476
T * allocate_socket(void)
Definition net-stack.h:495
net_interface & interface(void) const
void add_deferred_socket(class socket *sock)
Definition net-stack.h:482
const char * name(void) const
Definition net-stack.h:470
deferred_sockets_list_t & deferred_sockets_list(void)
Definition net-stack.h:488
Network socket.
List of intrusive nodes.
Definition lists.h:721
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:74
int socket(int domain, int type, int protocol)
System namespace.