SoDaRadio-12.2.0-cut_dependencies:6c82803
Loading...
Searching...
No Matches
IPSockets.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
29#pragma once
30//#include "SoDaBase.hxx"
31//#include "Command.hxx"
32
33#include <sys/types.h>
34#include <sys/socket.h>
35#include <stdio.h>
36#include <netinet/in.h>
37#include <netdb.h>
38#include <fcntl.h>
39#include <unistd.h>
40#include <SoDa/Format.hxx>
41
42#include <stdexcept>
43
44namespace SoDa {
45 namespace IP {
46
47 class ReadTimeoutExc : public std::runtime_error {
48 public:
49 ReadTimeoutExc(std::string sockname) :
50 std::runtime_error(SoDa::Format("Read operation on %0 socket timed out").addS(sockname).str()) {
51 }
52 };
53
54 enum TransportType { UDP, TCP };
55
56 class NetSocket {
57 public:
59 timeout.tv_sec = 0;
60 timeout.tv_usec = 5;
61 }
62
63
64 int put(const void * ptr, unsigned int size);
65
66 int get(void * ptr, unsigned int size);
67
68 int putRaw(const void * ptr, unsigned int size);
69
78 int getRaw(const void * ptr, unsigned int size, unsigned int usec_timeout = 0);
79
84 int x = fcntl(conn_socket, F_GETFL, 0);
85 fcntl(conn_socket, F_SETFL, (x | O_NONBLOCK));
86 non_blocking_mode = true;
87 }
88
89 void setBlocking() {
90 int x = fcntl(conn_socket, F_GETFL, 0);
91 fcntl(conn_socket, F_SETFL, (x & ~O_NONBLOCK));
92 non_blocking_mode = false;
93 }
94
96 struct sockaddr_in server_address, client_address;
98
99 struct timeval timeout;
100 private:
101 int loopWrite(int fd, const void * ptr, unsigned int nbytes);
102 };
103
104 class ServerSocket : public NetSocket {
105 public:
108 bool isReady();
109
110 int get(void *ptr, unsigned int size) {
111 int rv = NetSocket::get(ptr, size);
112 if(rv < 0) ready = false;
113 return rv;
114 }
115 int put(const void *ptr, unsigned int size) {
116 if(!ready) return 0;
117 int rv = NetSocket::put(ptr, size);
118 if(rv < 0) ready = false;
119 return rv;
120 }
121 protected:
122 bool ready;
123 };
124
125 class ClientSocket : public NetSocket {
126 public:
127 ClientSocket(const char * hostname, int portnum, TransportType transport = TCP);
129 private:
130 struct hostent * server;
131 };
132 }
133}
134
ClientSocket(const char *hostname, int portnum, TransportType transport=TCP)
struct hostent * server
int get(void *ptr, unsigned int size)
int put(const void *ptr, unsigned int size)
int loopWrite(int fd, const void *ptr, unsigned int nbytes)
int getRaw(const void *ptr, unsigned int size, unsigned int usec_timeout=0)
get a raw string of <size> bytes...
int putRaw(const void *ptr, unsigned int size)
struct sockaddr_in server_address client_address
Definition IPSockets.hxx:96
struct timeval timeout
Definition IPSockets.hxx:99
ReadTimeoutExc(std::string sockname)
Definition IPSockets.hxx:49
int put(const void *ptr, unsigned int size)
ServerSocket(int portnum, TransportType transport=TCP)
int get(void *ptr, unsigned int size)