Beam-lib  2.16.3
This is the Beam C++ class library.
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) 2012 All Right Reserved, Beam Ltd, http://www.beam.ltd.uk
5  *******************************************************************************
6  */
7 #ifndef BSOCKET_H
8 #define BSOCKET_H 1
9 
10 #include <BString.h>
11 #include <BError.h>
12 #include <BTypes.h>
13 #include <stdint.h>
14 #include <sys/types.h>
15 
16 #if TARGET_win32 || TARGET_win64
17 #include <winsock2.h>
18 #else
19 #ifdef __linux__
20 #include <sys/prctl.h>
21 #else
22 #include <netinet/in.h>
23 #define SOL_IP 0
24 #define SO_PRIORITY 12
25 #define MSG_NOSIGNAL 0
26 #endif
27 #endif
28 
31 public:
32  typedef struct sockaddr SockAddr;
33 
35  BSocketAddress(const BSocketAddress& add);
38 
39  BError set(SockAddr* address, int len);
40 
41  const SockAddr* raw() const;
42  int len() const;
43 
44  BString getString() const;
45 
46  // Operator functions
48  operator const SockAddr*() const { return raw(); }
49  int operator==(const BSocketAddress& add) const;
50  int operator!=(const BSocketAddress& add) const;
51 private:
52  int olen;
53  SockAddr* oaddress;
54 };
55 
58 public:
59  typedef struct sockaddr_in SockAddrIP;
60 
61  BError set(BString hostName, uint32_t port);
62  BError set(uint32_t address, uint32_t port);
63  BError set(BString hostName, BString service, BString type);
64 
65  void setPort(uint32_t port);
66  uint32_t address();
67  uint32_t port();
68 
69  BString getString();
70 
71  // Some useful generic system functions
72  static BString getHostName();
76 };
77 
78 class BSocket {
79 public:
80  enum NType { STREAM, DGRAM };
82 
83  BSocket();
84  BSocket(int fd);
85  BSocket(NType type);
86  BSocket(int domain, int type, int protocol);
87  ~BSocket();
88 
89  BError init(int domain, int type, int protocol);
90  BError init(NType type);
91  void setFd(int fd);
92  int getFd();
93 
94  // Main functions
95  BError bind(const BSocketAddress& add);
96  BError connect(const BSocketAddress& add);
97  BError shutdown(int how);
98  BError close();
99  BError listen(int backlog = 5);
100  BError accept(int& fd);
101  BError accept(int& fd, BSocketAddress& address);
102 
103  // Main data transfer functions
104  BError send(const void* buf, BSize nbytes, BSize& nbytesSent, int flags = 0);
105  BError sendTo(const BSocketAddress& address, const void* buf, BSize nbytes, BSize& nbytesSent, int flags = 0);
106  BError sendChunks(const BDataChunk* chunks, BSize nChunks, BSize& nbytesSent, int flags = 0);
107  BError recv(void* buf, BSize maxbytes, BSize& nbytesRecv, int flags = 0);
108  BError recvFrom(BSocketAddress& address, void* buf, BSize maxbytes, BSize& nbytesRecv, int flags = 0);
109  BError recvWithTimeout(void* buf, BSize maxbytes, BSize& nbytesRecv, int timeout, int flags = 0);
110  BError recvFromWithTimeout(BSocketAddress& address, void* buf, BSize maxbytes, BSize& nbytesRecv, int timeout, int flags = 0);
111 
113 
114  // Configuration functions
115  BError setSockOpt(int level, int optname, void* optval, unsigned int optlen);
116  BError getSockOpt(int level, int optname, void* optval, unsigned int* optlen);
117  BError setReuseAddress(int on);
118  BError setBroadCast(int on);
119  BError setPriority(Priority priority);
120 
121  BError getMTU(uint32_t& mtu);
123 
124 private:
125  int osocket;
126 };
127 #endif
int getFd()
Definition: BSocket.cpp:369
BSocketAddress()
Definition: BSocket.cpp:51
Definition: BSocket.h:80
static BList< BString > getIpAddressList()
Get a list of all the IP addresses of this host under hostname.
Definition: BSocket.cpp:250
size_t BSize
Definition: BTypes.h:33
BError setReuseAddress(int on)
Definition: BSocket.cpp:640
BError accept(int &fd)
Definition: BSocket.cpp:418
void setFd(int fd)
Definition: BSocket.cpp:365
Definition: BString.h:18
struct sockaddr SockAddr
Definition: BSocket.h:32
uint32_t port()
Returns socket port.
Definition: BSocket.cpp:203
Definition: BSocket.h:78
Socket Address.
Definition: BSocket.h:30
Template based list class.
Definition: BList.h:30
BString getString()
Return string version of address <ip>:<port>
Definition: BSocket.cpp:212
BError recvFrom(BSocketAddress &address, void *buf, BSize maxbytes, BSize &nbytesRecv, int flags=0)
Definition: BSocket.cpp:555
Definition: BSocket.h:81
BError getAddress(BSocketAddress &address)
Definition: BSocket.cpp:669
static BList< BString > getIpAddressListAll()
Get a list of all the IP addresses of this host looking at physical interfaces.
Definition: BSocket.cpp:267
Definition: BSocket.h:81
const SockAddr * raw() const
Definition: BSocket.cpp:87
BSocketAddress & operator=(const BSocketAddress &add)
Definition: BSocket.cpp:111
BError sendTo(const BSocketAddress &address, const void *buf, BSize nbytes, BSize &nbytesSent, int flags=0)
Definition: BSocket.cpp:460
BError setSockOpt(int level, int optname, void *optval, unsigned int optlen)
Definition: BSocket.cpp:619
BSocket()
Definition: BSocket.cpp:306
NType
Definition: BSocket.h:80
BError sendChunks(const BDataChunk *chunks, BSize nChunks, BSize &nbytesSent, int flags=0)
Definition: BSocket.cpp:490
struct sockaddr_in SockAddrIP
Definition: BSocket.h:59
BError getMTU(uint32_t &mtu)
Definition: BSocket.cpp:662
uint32_t address()
Returns socket ip address.
Definition: BSocket.cpp:194
BError setBroadCast(int on)
Definition: BSocket.cpp:645
int operator!=(const BSocketAddress &add) const
Definition: BSocket.cpp:126
BError getSockOpt(int level, int optname, void *optval, unsigned int *optlen)
Definition: BSocket.cpp:627
Definition: BSocket.h:81
BError listen(int backlog=5)
Definition: BSocket.cpp:410
~BSocket()
Definition: BSocket.cpp:335
void setPort(uint32_t port)
Definition: BSocket.cpp:183
BUInt32 BUInt
Definition: BTypes.h:30
BString getString() const
Return string version of address <ip>:<port>
Definition: BSocket.cpp:95
BError init(int domain, int type, int protocol)
Definition: BSocket.cpp:341
BError set(BString hostName, uint32_t port)
Definition: BSocket.cpp:132
Priority
Definition: BSocket.h:81
Definition: BTypes.h:91
BError set(SockAddr *address, int len)
Definition: BSocket.cpp:74
BError shutdown(int how)
Definition: BSocket.cpp:391
BError send(const void *buf, BSize nbytes, BSize &nbytesSent, int flags=0)
Definition: BSocket.cpp:447
static BList< uint32_t > getIpAddresses()
Get a list of all the IP addresses of this host.
Definition: BSocket.cpp:237
BError setPriority(Priority priority)
Definition: BSocket.cpp:650
~BSocketAddress()
Definition: BSocket.cpp:68
Definition: BSocket.h:80
BUInt recvAvailable()
Definition: BSocket.cpp:606
BUInt32 address
Definition: BFirmware.h:53
BError close()
Definition: BSocket.cpp:400
IP aware socket address.
Definition: BSocket.h:57
int operator==(const BSocketAddress &add) const
Definition: BSocket.cpp:122
Definition: BError.h:25
BError recvWithTimeout(void *buf, BSize maxbytes, BSize &nbytesRecv, int timeout, int flags=0)
Definition: BSocket.cpp:533
BError bind(const BSocketAddress &add)
Definition: BSocket.cpp:373
static BString getHostName()
Get this hosts network name.
Definition: BSocket.cpp:229
BError connect(const BSocketAddress &add)
Definition: BSocket.cpp:382
BError recv(void *buf, BSize maxbytes, BSize &nbytesRecv, int flags=0)
Definition: BSocket.cpp:518
int len() const
Definition: BSocket.cpp:91
BError recvFromWithTimeout(BSocketAddress &address, void *buf, BSize maxbytes, BSize &nbytesRecv, int timeout, int flags=0)
Definition: BSocket.cpp:571