uThreads  0.3.0
Network.h
1 /*******************************************************************************
2  * Copyright © 2015, 2016 Saman Barghi
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  *******************************************************************************/
17 
18 #ifndef UTHREADS_INCLUDE_NETWORK_H_
19 #define UTHREADS_INCLUDE_NETWORK_H_
20 #include "IOHandler.h"
21 
33 class Connection {
34  friend IOHandler;
35 private:
36  /* used whith polling */
37  PollData* pd = nullptr;
38  //related file descriptor
39  int fd = -1;
40 
46  void init();
47 
48  void setFD(int fd) {
49  this->fd = fd;
50  this->pd->fd = fd;
51  }
52 
53 public:
54 
62  fd(-1){
63  init();
64  }
72  Connection(int fd) :
73  fd(fd) {
74  init();
75  }
76 
87  Connection(int domain, int type, int protocol) throw(std::system_error);
88 
89  ~Connection();
90 
99  int accept(Connection *conn, struct sockaddr *addr, socklen_t *addrlen);
100 
107  Connection* accept(struct sockaddr *addr, socklen_t *addrlen) throw(std::system_error);
108 
117  int socket(int domain, int type, int protocol);
118 
123  int listen(int backlog);
124 
129  int bind(const struct sockaddr *addr,
130  socklen_t addrlen);
131 
136  int connect(const struct sockaddr *addr,socklen_t addrlen);
153  ssize_t recv(void *buf, size_t len, int flags);
155  ssize_t recvfrom(void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
157  ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
159  int recvmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen,
160  unsigned int flags, struct timespec *timeout);
161 
163  ssize_t send(const void *buf, size_t len, int flags);
165  ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
166  const struct sockaddr *dest_addr, socklen_t addrlen);
168  ssize_t sendmsg(const struct msghdr *msg, int flags);
170  int sendmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen,
171  unsigned int flags);
172 
174  ssize_t read(void *buf, size_t count);
176  ssize_t write(const void *buf, size_t count);
177 
181  void blockOnRead();
182 
186  void blockOnWrite();
191  int close();
192 
197  int getFd() const {return fd;}
198 
199 };
200 
201 #endif /* UTHREADS_INCLUDE_NETWORK_H_ */
int recvmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags, struct timespec *timeout)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: Network.cpp:174
int socket(int domain, int type, int protocol)
Same as socket syscall, set the fd for current connection.
Definition: Network.cpp:45
ssize_t sendmsg(const struct msghdr *msg, int flags)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: Network.cpp:214
void blockOnRead()
Block uThread, waiting for fd to become ready for read.
Definition: Network.cpp:127
ssize_t send(const void *buf, size_t len, int flags)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: Network.cpp:189
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: Network.cpp:161
int listen(int backlog)
Same as listen syscall on current fd.
Definition: Network.cpp:56
Connection(int fd)
Create a connection object with the provided fd.
Definition: Network.h:72
int accept(Connection *conn, struct sockaddr *addr, socklen_t *addrlen)
nonblocking accept syscall and updating the passed Connection object
Definition: Network.cpp:65
ssize_t recvfrom(void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: Network.cpp:147
ssize_t recv(void *buf, size_t len, int flags)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: Network.cpp:135
int bind(const struct sockaddr *addr, socklen_t addrlen)
Same as bind syscall.
Definition: Network.cpp:103
ssize_t write(const void *buf, size_t count)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: Network.cpp:249
void blockOnWrite()
Block uThread, waiting for fd to become ready for write.
Definition: Network.cpp:131
int connect(const struct sockaddr *addr, socklen_t addrlen)
Same as connect syscall.
Definition: Network.cpp:108
Connection()
Create a Connection that does not have.
Definition: Network.h:61
ssize_t read(void *buf, size_t count)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: Network.cpp:237
int close()
closes the socket
Definition: Network.cpp:261
Represents a network connection.
Definition: Network.h:33
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: Network.cpp:200
int sendmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags)
Call the underlying system call on Connection&#39;s file descriptor.
Definition: Network.cpp:224
int getFd() const
return the Connection&#39;s file descriptor
Definition: Network.h:197