libkipr  1.0.0
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);
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_ */
Definition: socket.hpp:32
bool setHost(const char *const host)
Address(const char *const host, const unsigned short port)
Address(const sockaddr_in &addr)
const sockaddr * addr() const
void setPort(const unsigned short port)
bool isValid() const
const char * ip() const
socklen_t addrLength() const
unsigned short port() const
Definition: socket.hpp:56
ssize_t recv(void *const buffer, const size_t length, int flags=0)
bool close()
ssize_t send(const void *const buffer, const size_t length, int flags=0)
static Socket udp()
ssize_t recvfrom(void *const buffer, const size_t length, Address &address, int flags=0)
bool isOpen() const
bool setReusable(const bool reusable)
bool setBlocking(const bool blocking)
bool connect(const Address &addr)
bool bind(const unsigned short port)
static Socket tcp()
bool open(int domain, int type, int protocol)
ssize_t sendto(const void *const buffer, const size_t length, const Address &dest, int flags=0)
bool disconnect()
socket_fd_t fd() const
int socket_fd_t
Definition: socket.hpp:27