libwallaby  v24
The wallaby standard library
socket.hpp
Go to the documentation of this file.
1 /*
2  * socket.hpp
3  *
4  * Created on: Nov 13, 2015
5  * Author: Joshua Southerland
6  */
7 
8 #ifndef INCLUDE_WALLABY_SOCKET_HPP_
9 #define INCLUDE_WALLABY_SOCKET_HPP_
10 
11 #ifndef _WIN32
12 
13 
14 #ifndef _WIN32
15 #include <netinet/in.h>
16 #else
17 #ifndef _WIN32_WINNT
18 #define _WIN32_WINNT 0x0501
19 #endif
20 #include <winsock2.h>
21 #include <winsock.h>
22 typedef u_long socklen_t;
23 #endif
24 
25 #include <unistd.h>
26 
27 typedef int socket_fd_t;
28 
29 struct sockaddr;
30 
31 class Address
32 {
33 public:
34  Address(const char *const host, const unsigned short port);
35  Address(const sockaddr_in &addr);
36  Address();
37 
38  bool isValid() const;
39 
40  bool setHost(const char *const host);
41  void setPort(const unsigned short port);
42 
43  unsigned short port() const;
44 
45  const sockaddr *addr() const;
46  socklen_t addrLength() const;
47 
48  const char *ip() const;
49 
50 private:
51  bool m_valid;
52  sockaddr_in m_addr;
53 };
54 
55 class Socket
56 {
57 public:
58  Socket();
59 
60  bool open(int domain, int type, int protocol);
61  bool isOpen() const;
62  bool setBlocking(const bool blocking);
63  bool setReusable(const bool reusable);
64  bool bind(const unsigned short port);
65  bool connect(const Address &addr);
66  bool disconnect();
67 
68  bool close();
69 
70  ssize_t recv(void *const buffer, const size_t length, int flags = 0);
71  ssize_t recvfrom(void *const buffer, const size_t length, Address &address, int flags = 0);
72 
73  ssize_t send(const void *const buffer, const size_t length, int flags = 0);
74  ssize_t sendto(const void *const buffer, const size_t length, const Address &dest, int flags = 0);
75 
76  socket_fd_t fd() const;
77 
78  static Socket udp();
79  static Socket tcp();
80 
81 private:
82  socket_fd_t m_fd;
83 };
84 
85 #endif
86 
87 
88 
89 #endif /* INCLUDE_WALLABY_SOCKET_HPP_ */
Socket::connect
bool connect(const Address &addr)
Socket::recvfrom
ssize_t recvfrom(void *const buffer, const size_t length, Address &address, int flags=0)
Socket::setBlocking
bool setBlocking(const bool blocking)
Address::setHost
bool setHost(const char *const host)
Socket::fd
socket_fd_t fd() const
Socket::tcp
static Socket tcp()
Socket::recv
ssize_t recv(void *const buffer, const size_t length, int flags=0)
Socket::open
bool open(int domain, int type, int protocol)
Socket::close
bool close()
Address
Definition: socket.hpp:31
Address::isValid
bool isValid() const
Address::port
unsigned short port() const
Socket::setReusable
bool setReusable(const bool reusable)
Socket::isOpen
bool isOpen() const
Socket::udp
static Socket udp()
Socket::sendto
ssize_t sendto(const void *const buffer, const size_t length, const Address &dest, int flags=0)
Address::addr
const sockaddr * addr() const
socket_fd_t
int socket_fd_t
Definition: socket.hpp:27
Socket::disconnect
bool disconnect()
Address::ip
const char * ip() const
Address::addrLength
socklen_t addrLength() const
Socket::Socket
Socket()
Socket
Definition: socket.hpp:55
Socket::send
ssize_t send(const void *const buffer, const size_t length, int flags=0)
Address::Address
Address()
Address::setPort
void setPort(const unsigned short port)
Socket::bind
bool bind(const unsigned short port)