Line data Source code
1 : // UDP Client Server -- send/receive UDP packets
2 : // Copyright (c) 2013-2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // This program is free software; you can redistribute it and/or modify
5 : // it under the terms of the GNU General Public License as published by
6 : // the Free Software Foundation; either version 2 of the License, or
7 : // (at your option) any later version.
8 : //
9 : // This program is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : // GNU General Public License for more details.
13 : //
14 : // You should have received a copy of the GNU General Public License
15 : // along with this program; if not, write to the Free Software
16 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 : #pragma once
18 :
19 : // snapdev lib
20 : //
21 : #include <snapdev/raii_generic_deleter.h>
22 :
23 : // C++ lib
24 : //
25 : #include <stdexcept>
26 :
27 : // C lib
28 : //
29 : #include <netdb.h>
30 : #include <sys/types.h>
31 : #include <sys/socket.h>
32 : #include <unistd.h>
33 :
34 :
35 :
36 : namespace udp_client_server
37 : {
38 :
39 0 : class udp_client_server_runtime_error : public std::runtime_error
40 : {
41 : public:
42 0 : udp_client_server_runtime_error(const std::string & errmsg) : std::runtime_error(errmsg) {}
43 : };
44 :
45 0 : class udp_client_server_parameter_error : public std::logic_error
46 : {
47 : public:
48 0 : udp_client_server_parameter_error(const std::string & errmsg) : std::logic_error(errmsg) {}
49 : };
50 :
51 :
52 0 : class udp_base
53 : {
54 : public:
55 0 : typedef std::unique_ptr<struct addrinfo, snap::raii_pointer_deleter<struct addrinfo, decltype(&::freeaddrinfo), &::freeaddrinfo>> raii_addrinfo_t;
56 :
57 : int get_socket() const;
58 : int get_mtu_size() const;
59 : int get_mss_size() const;
60 : int get_port() const;
61 : std::string get_addr() const;
62 :
63 : protected:
64 : udp_base(std::string const & addr, int port, int family);
65 :
66 : // TODO: convert the port + addr into a libaddr addr object?
67 : // (we use the f_addrinfo as is in the sendto() and bind() calls, though)
68 : //
69 : snap::raii_fd_t f_socket = snap::raii_fd_t();
70 : int f_port = -1;
71 : mutable int f_mtu_size = 0;
72 : std::string f_addr = std::string();
73 : raii_addrinfo_t f_addrinfo = raii_addrinfo_t();
74 : };
75 :
76 :
77 : class udp_client
78 : : public udp_base
79 : {
80 : public:
81 : typedef std::shared_ptr<udp_client> pointer_t;
82 :
83 : udp_client(std::string const & addr, int port, int family = AF_UNSPEC);
84 : ~udp_client();
85 :
86 : int send(char const * msg, size_t size);
87 :
88 : private:
89 : };
90 :
91 :
92 : class udp_server
93 : : public udp_base
94 : {
95 : public:
96 : typedef std::shared_ptr<udp_server> pointer_t;
97 :
98 : udp_server(std::string const & addr, int port, int family = AF_UNSPEC, std::string const * multicast_addr = nullptr);
99 : ~udp_server();
100 :
101 : int recv(char * msg, size_t max_size);
102 : int timed_recv( char * msg, size_t const max_size, int const max_wait_ms );
103 : std::string timed_recv( int const bufsize, int const max_wait_ms );
104 :
105 : private:
106 : };
107 :
108 : } // namespace udp_client_server
109 : // vim: ts=4 sw=4 et
|