SoDaRadio-12.2.0-cut_dependencies:6c82803
Loading...
Searching...
No Matches
UDSockets.hxx
Go to the documentation of this file.
1/*
2 Copyright (c) 2012, 2025, 2026 Matthew H. Reilly (kb1vc)
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
7 met:
8
9 Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in
13 the documentation and/or other materials provided with the
14 distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28#pragma once
29
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <stdio.h>
33#include <netinet/in.h>
34#include <netdb.h>
35#include <sys/un.h>
36#include <unistd.h>
37
38#include <memory>
39#include <string>
40#include <iostream>
41
42namespace SoDa {
43 namespace UD { // Unix Domain sockets.
44
45 class ServerSocket;
46 typedef std::shared_ptr<SoDa::UD::ServerSocket> ServerSocketPtr;
47
48 class ClientSocket;
49 typedef std::shared_ptr<SoDa::UD::ClientSocket> ClientSocketPtr;
50
51 class NetSocket {
52 public:
54 timeout.tv_sec = 0;
55 timeout.tv_usec = 5;
56 }
57
58
59 int put(const void * ptr, unsigned int size, bool len_prefix = true);
60
61 int get(void * ptr, unsigned int size, bool len_prefix = true);
62
64 struct sockaddr_un server_address, client_address;
65
66 struct timeval timeout;
67 private:
68 int loopWrite(int fd, const void * ptr, unsigned int nbytes);
69 };
70
71 class ServerSocket : public NetSocket {
72 public:
73 ServerSocket(const std::string & path);
75 close(conn_socket);
76 close(server_socket);
77 unlink(mailbox_pathname.c_str());
78 }
79
80 static ServerSocketPtr make(const std::string & path) {
81 return std::shared_ptr<ServerSocket>(new ServerSocket(path));
82 }
83
84 bool isReady(size_t required_len = 32);
85
86 int get(void *ptr, unsigned int size, bool len_prefix = true) {
87 int rv = NetSocket::get(ptr, size, len_prefix);
88 if(rv < 0) ready = false;
89 return rv;
90 }
91
92 int put(const void *ptr, unsigned int size, bool len_prefix = true) {
93 if(!ready && !isReady()) {
94 return 0;
95 }
96 int rv = NetSocket::put(ptr, size, len_prefix);
97 if(rv < 0) ready = false;
98 return rv;
99 }
100
101 void setDebug(bool v) {
102 debug = v;
103 }
104
105 private:
106 bool debug;
107 bool ready;
108 std::string mailbox_pathname;
109 };
110
111 class ClientSocket : public NetSocket {
112 public:
113 ClientSocket(const std::string & path, int startup_timeout_count = 1);
115 close(conn_socket);
116 }
117
118 static ClientSocketPtr make(const std::string & path,
119 int startup_timeout_count = 1) {
120 return std::shared_ptr<ClientSocket>(new ClientSocket(path, startup_timeout_count));
121 }
122
123 private:
124 struct hostent * server;
125 std::string mailbox_pathname;
126 };
127 }
128}
129
struct hostent * server
std::string mailbox_pathname
static ClientSocketPtr make(const std::string &path, int startup_timeout_count=1)
ClientSocket(const std::string &path, int startup_timeout_count=1)
int put(const void *ptr, unsigned int size, bool len_prefix=true)
int loopWrite(int fd, const void *ptr, unsigned int nbytes)
struct timeval timeout
Definition UDSockets.hxx:66
int get(void *ptr, unsigned int size, bool len_prefix=true)
struct sockaddr_un server_address client_address
Definition UDSockets.hxx:64
ServerSocket(const std::string &path)
bool isReady(size_t required_len=32)
std::string mailbox_pathname
static ServerSocketPtr make(const std::string &path)
Definition UDSockets.hxx:80
int put(const void *ptr, unsigned int size, bool len_prefix=true)
Definition UDSockets.hxx:92
int get(void *ptr, unsigned int size, bool len_prefix=true)
Definition UDSockets.hxx:86
std::shared_ptr< SoDa::UD::ServerSocket > ServerSocketPtr
Definition UDSockets.hxx:46
std::shared_ptr< SoDa::UD::ClientSocket > ClientSocketPtr
Definition UDSockets.hxx:49