Beamlib 3.1.1
This is the Beam C++ class library.
Loading...
Searching...
No Matches
BSocket.h
Go to the documentation of this file.
1/*******************************************************************************
2 * BSocket.h BSocket Access Classes
3 * T.Barnaby, BEAM Ltd, 1/4/05
4 * Copyright (c) 2022 All Right Reserved, Beam Ltd, https://www.beam.ltd.uk
5 * For license see LICENSE.txt at the root of the beamlib source tree.
6 *******************************************************************************
7 */
8#ifndef BSOCKET_H
9#define BSOCKET_H 1
10
11#include <BString.h>
12#include <BError.h>
13#include <BTypes.h>
14#include <stdint.h>
15#include <sys/types.h>
16
17#if TARGET_win32 || TARGET_win64
18#include <winsock2.h>
19#else
20#ifdef __linux__
21#include <sys/prctl.h>
22#else
23#include <netinet/in.h>
24#define SOL_IP 0
25#define SO_PRIORITY 12
26#define MSG_NOSIGNAL 0
27#endif
28#endif
29
32public:
33 typedef struct sockaddr SockAddr;
34
39
41
42 const SockAddr* raw() const;
43 int len() const;
44
45 BString getString() const;
46
47 // Operator functions
49 operator const SockAddr*() const { return raw(); }
50 int operator==(const BSocketAddress& add) const;
51 int operator!=(const BSocketAddress& add) const;
52private:
53 int olen;
54 SockAddr* oaddress;
55};
56
59public:
60 typedef struct sockaddr_in SockAddrIP;
61
62 BError set(BString hostName, uint32_t port);
63 BError set(uint32_t address, uint32_t port);
64 BError set(BString hostName, BString service, BString type);
65
66 void setPort(uint32_t port);
67 uint32_t address();
68 uint32_t port();
69
71
72 // Some useful generic system functions
73 static BString getHostName();
77};
78
80class BSocket {
81public:
82 enum NType { STREAM, DGRAM };
84
85 BSocket();
86 BSocket(int fd);
87 BSocket(NType type);
88 BSocket(int domain, int type, int protocol);
89 ~BSocket();
90
91 BError init(int domain, int type, int protocol);
92 BError init(NType type);
93 void setFd(int fd);
94 int getFd();
95
96 // Main functions
97 BError bind(const BSocketAddress& add);
98 BError connect(const BSocketAddress& add);
99 BError shutdown(int how);
100 BError close();
101 BError listen(int backlog = 5);
102 BError accept(int& fd);
104
105 // Main data transfer functions
106 BError send(const void* buf, BSize nbytes, BSize& nbytesSent, int flags = 0);
107 BError sendTo(const BSocketAddress& address, const void* buf, BSize nbytes, BSize& nbytesSent, int flags = 0);
108 BError sendChunks(const BDataChunk* chunks, BSize nChunks, BSize& nbytesSent, int flags = 0);
109 BError recv(void* buf, BSize maxbytes, BSize& nbytesRecv, int flags = 0);
110 BError recvFrom(BSocketAddress& address, void* buf, BSize maxbytes, BSize& nbytesRecv, int flags = 0);
111 BError recvWithTimeout(void* buf, BSize maxbytes, BSize& nbytesRecv, int timeout, int flags = 0);
112 BError recvFromWithTimeout(BSocketAddress& address, void* buf, BSize maxbytes, BSize& nbytesRecv, int timeout, int flags = 0);
113
115
116 // Configuration functions
117 BError setSockOpt(int level, int optname, void* optval, unsigned int optlen);
118 BError getSockOpt(int level, int optname, void* optval, unsigned int* optlen);
119 BError setReuseAddress(int on);
120 BError setBroadCast(int on);
121 BError setPriority(Priority priority);
122
123 BError getMTU(uint32_t& mtu);
125
126private:
127 int osocket;
128};
129#endif
BUInt32 address
Definition: BFirmware.h:7
size_t BSize
Definition: BTypes.h:36
BUInt32 BUInt
Definition: BTypes.h:33
A chunk of data allowing writes of multiple chunks of segmented data.
Definition: BTypes.h:94
Error return class. This class is used to return the error status from a function....
Definition: BError.h:31
Template based list class.
Definition: BList.h:31
IPV4 aware socket address.
Definition: BSocket.h:58
uint32_t port()
Returns socket port.
Definition: BSocket.cpp:205
static BList< BString > getIpAddressList()
Get a list of all the IP addresses of this host under hostname.
Definition: BSocket.cpp:252
struct sockaddr_in SockAddrIP
Definition: BSocket.h:60
uint32_t address()
Returns socket ip address.
Definition: BSocket.cpp:196
BString getString()
Return string version of address <ip>:<port>
Definition: BSocket.cpp:214
BError set(BString hostName, uint32_t port)
Definition: BSocket.cpp:134
static BList< uint32_t > getIpAddresses()
Get a list of all the IP addresses of this host.
Definition: BSocket.cpp:239
void setPort(uint32_t port)
Definition: BSocket.cpp:185
static BList< BString > getIpAddressListAll()
Get a list of all the IP addresses of this host looking at physical interfaces.
Definition: BSocket.cpp:269
static BString getHostName()
Get this hosts network name.
Definition: BSocket.cpp:231
Socket Address.
Definition: BSocket.h:31
struct sockaddr SockAddr
Definition: BSocket.h:33
BSocketAddress & operator=(const BSocketAddress &add)
Definition: BSocket.cpp:113
int operator==(const BSocketAddress &add) const
Definition: BSocket.cpp:124
BString getString() const
Return string version of address <ip>:<port>
Definition: BSocket.cpp:97
BError set(SockAddr *address, int len)
Definition: BSocket.cpp:76
int len() const
Definition: BSocket.cpp:93
BSocketAddress()
Definition: BSocket.cpp:53
~BSocketAddress()
Definition: BSocket.cpp:70
int operator!=(const BSocketAddress &add) const
Definition: BSocket.cpp:128
const SockAddr * raw() const
Definition: BSocket.cpp:89
A network communications socket.
Definition: BSocket.h:80
BError send(const void *buf, BSize nbytes, BSize &nbytesSent, int flags=0)
Definition: BSocket.cpp:449
BError accept(int &fd)
Definition: BSocket.cpp:420
BError getSockOpt(int level, int optname, void *optval, unsigned int *optlen)
Definition: BSocket.cpp:629
BSocket()
Definition: BSocket.cpp:308
BError init(int domain, int type, int protocol)
Definition: BSocket.cpp:343
BUInt recvAvailable()
Definition: BSocket.cpp:608
BError setReuseAddress(int on)
Definition: BSocket.cpp:642
NType
Definition: BSocket.h:82
@ STREAM
Definition: BSocket.h:82
@ DGRAM
Definition: BSocket.h:82
BError recvWithTimeout(void *buf, BSize maxbytes, BSize &nbytesRecv, int timeout, int flags=0)
Definition: BSocket.cpp:535
BError setSockOpt(int level, int optname, void *optval, unsigned int optlen)
Definition: BSocket.cpp:621
BError shutdown(int how)
Definition: BSocket.cpp:393
BError recv(void *buf, BSize maxbytes, BSize &nbytesRecv, int flags=0)
Definition: BSocket.cpp:520
BError connect(const BSocketAddress &add)
Definition: BSocket.cpp:384
BError sendChunks(const BDataChunk *chunks, BSize nChunks, BSize &nbytesSent, int flags=0)
Definition: BSocket.cpp:492
void setFd(int fd)
Definition: BSocket.cpp:367
BError sendTo(const BSocketAddress &address, const void *buf, BSize nbytes, BSize &nbytesSent, int flags=0)
Definition: BSocket.cpp:462
BError getMTU(uint32_t &mtu)
Definition: BSocket.cpp:664
BError listen(int backlog=5)
Definition: BSocket.cpp:412
int getFd()
Definition: BSocket.cpp:371
BError recvFromWithTimeout(BSocketAddress &address, void *buf, BSize maxbytes, BSize &nbytesRecv, int timeout, int flags=0)
Definition: BSocket.cpp:573
BError recvFrom(BSocketAddress &address, void *buf, BSize maxbytes, BSize &nbytesRecv, int flags=0)
Definition: BSocket.cpp:557
BError close()
Definition: BSocket.cpp:402
~BSocket()
Definition: BSocket.cpp:337
BError setBroadCast(int on)
Definition: BSocket.cpp:647
Priority
Definition: BSocket.h:83
@ PriorityLow
Definition: BSocket.h:83
@ PriorityNormal
Definition: BSocket.h:83
@ PriorityHigh
Definition: BSocket.h:83
BError bind(const BSocketAddress &add)
Definition: BSocket.cpp:375
BError getAddress(BSocketAddress &address)
Definition: BSocket.cpp:671
BError setPriority(Priority priority)
Definition: BSocket.cpp:652
This class stores and manipulates ASCII strings.
Definition: BString.h:20