libtasks Documentation  1.6
socket.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 ADTECH GmbH
3  * Licensed under MIT (https://github.com/adtechlabs/libtasks/blob/master/COPYING)
4  *
5  * Author: Andreas Pohl
6  */
7 
8 #ifndef _TASKS_SOCKET_H_
9 #define _TASKS_SOCKET_H_
10 
11 #include <string>
12 #include <exception>
13 #include <memory>
14 #include <cerrno>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 
18 #include <tasks/io_base.h>
19 #include <tasks/tasks_exception.h>
20 
21 #ifdef _OS_LINUX_
22 #define SEND_RECV_FLAGS MSG_NOSIGNAL
23 #else
24 #define SEND_RECV_FLAGS 0
25 #endif
26 
27 struct sockaddr_in;
28 
29 namespace tasks {
30 namespace net {
31 
32 enum class socket_type { TCP, UDP };
33 
35 class socket : public io_base {
36  public:
40  socket(int fd) : io_base(fd) {}
41 
47 
49  inline bool udp() const { return m_type == socket_type::UDP; }
50 
52  inline bool tcp() const { return m_type == socket_type::TCP; }
53 
55  inline socket_type type() const { return m_type; }
56 
58  inline std::shared_ptr<struct sockaddr_in> addr() { return m_addr; }
59 
61  inline void set_blocking() { m_blocking = true; }
62 
69  void bind(int port, std::string ip = "");
70 
75  void listen(std::string path, int queue_size = 128);
76 
77  // Listen for tcp sockets.
82  void listen(int port, std::string ip = "", int queue_size = 128);
83 
85  socket accept();
86 
90  void connect(std::string path);
91 
96  void connect(std::string host, int port);
97 
99  void shutdown();
100 
107  std::streamsize write(const char* data, std::size_t len, int port = -1, std::string ip = "");
108 
113  std::streamsize read(char* data, std::size_t len);
114 
115  private:
117  bool m_blocking = false;
118  std::shared_ptr<struct sockaddr_in> m_addr;
119 
120  void bind(int port, std::string ip, bool udp);
121  void init_sockaddr(int port, std::string ip = "");
122 };
123 
124 } // net
125 } // tasks
126 
127 #endif // _TASKS_SOCKET_H_
void bind(int port, std::string ip="")
Definition: socket.cpp:86
socket_type
Definition: socket.h:32
std::streamsize write(const char *data, std::size_t len, int port=-1, std::string ip="")
Definition: socket.cpp:225
int fd() const
Definition: io_base.h:22
The socket class.
Definition: socket.h:35
std::shared_ptr< struct sockaddr_in > addr()
Definition: socket.h:58
std::streamsize read(char *data, std::size_t len)
Definition: socket.cpp:251
Base class for socket and term.
Definition: io_base.h:16
socket accept()
Accept new client connections.
Definition: socket.cpp:136
void shutdown()
Call shutdown on the fd.
Definition: socket.cpp:218
bool tcp() const
Definition: socket.h:52
socket_type type() const
Definition: socket.h:55
void init_sockaddr(int port, std::string ip="")
Definition: socket.cpp:119
void set_blocking()
Set the socket to blocking mode.
Definition: socket.h:61
socket_type m_type
Definition: socket.h:116
void connect(std::string path)
Definition: socket.cpp:147
socket(int fd)
Definition: socket.h:40
bool udp() const
Definition: socket.h:49
void listen(std::string path, int queue_size=128)
Definition: socket.cpp:36
std::shared_ptr< struct sockaddr_in > m_addr
Definition: socket.h:118