uThreads
0.3.0
|
Represents a network connection. More...
#include <Network.h>
Public Member Functions | |
Connection () | |
Create a Connection that does not have. More... | |
Connection (int fd) | |
Create a connection object with the provided fd. More... | |
Connection (int domain, int type, int protocol) throw (std::system_error) | |
Same as socket syscall adds | SOCK_NONBLOCK to type. More... | |
int | accept (Connection *conn, struct sockaddr *addr, socklen_t *addrlen) |
nonblocking accept syscall and updating the passed Connection object More... | |
Connection * | accept (struct sockaddr *addr, socklen_t *addrlen) throw (std::system_error) |
Accepts a connection and returns a connection object. More... | |
int | socket (int domain, int type, int protocol) |
Same as socket syscall, set the fd for current connection. More... | |
int | listen (int backlog) |
Same as listen syscall on current fd. More... | |
int | bind (const struct sockaddr *addr, socklen_t addrlen) |
Same as bind syscall. More... | |
int | connect (const struct sockaddr *addr, socklen_t addrlen) |
Same as connect syscall. More... | |
ssize_t | recv (void *buf, size_t len, int flags) |
Call the underlying system call on Connection's file descriptor. More... | |
ssize_t | recvfrom (void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen) |
Call the underlying system call on Connection's file descriptor. More... | |
ssize_t | recvmsg (int sockfd, struct msghdr *msg, int flags) |
Call the underlying system call on Connection's file descriptor. More... | |
int | recvmmsg (int sockfd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags, struct timespec *timeout) |
Call the underlying system call on Connection's file descriptor. More... | |
ssize_t | send (const void *buf, size_t len, int flags) |
Call the underlying system call on Connection's file descriptor. More... | |
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's file descriptor. More... | |
ssize_t | sendmsg (const struct msghdr *msg, int flags) |
Call the underlying system call on Connection's file descriptor. More... | |
int | sendmmsg (int sockfd, struct mmsghdr *msgvec, unsigned int vlen, unsigned int flags) |
Call the underlying system call on Connection's file descriptor. More... | |
ssize_t | read (void *buf, size_t count) |
Call the underlying system call on Connection's file descriptor. More... | |
ssize_t | write (const void *buf, size_t count) |
Call the underlying system call on Connection's file descriptor. More... | |
void | blockOnRead () |
Block uThread, waiting for fd to become ready for read. | |
void | blockOnWrite () |
Block uThread, waiting for fd to become ready for write. | |
int | close () |
closes the socket More... | |
int | getFd () const |
return the Connection's file descriptor More... | |
Represents a network connection.
Connection class is a wrapper around socket and provides the ability to do nonblocking read/write on sockets, and nonblocking accept. It first tries to read/write/accept and if the fd is not ready uses the underlying polling structure to wait for the fd to be ready. Thus, the uThread that is calling these functions is blocked if the fd is not ready, and kThread never blocks.
|
inline |
Create a Connection that does not have.
This is useful for accept or socket functions that require a Connection object without fd being set
|
inline |
Create a connection object with the provided fd.
fd | If the connection is already established by other means, set the fd and add it to the polling structure |
Connection::Connection | ( | int | domain, |
int | type, | ||
int | protocol | ||
) | |||
throw | ( | std::system_error | |
) |
Same as socket syscall adds | SOCK_NONBLOCK to type.
Throws a std::system_error exception. Do not call from C code. The unerlying socket is always nonbelocking. This is achieved by adding a (| SOCK_NONBLOCK) to type, thus requires linux kernels > 2.6.27
int Connection::accept | ( | Connection * | conn, |
struct sockaddr * | addr, | ||
socklen_t * | addrlen | ||
) |
nonblocking accept syscall and updating the passed Connection object
conn | Pointer to a Connection object that is not initialized |
This format is used for compatibility with C
Connection * Connection::accept | ( | struct sockaddr * | addr, |
socklen_t * | addrlen | ||
) | |||
throw | ( | std::system_error | |
) |
Accepts a connection and returns a connection object.
Throws a std::system_error exception on error. Never call from C.
int Connection::bind | ( | const struct sockaddr * | addr, |
socklen_t | addrlen | ||
) |
Same as bind syscall.
int Connection::close | ( | ) |
closes the socket
int Connection::connect | ( | const struct sockaddr * | addr, |
socklen_t | addrlen | ||
) |
Same as connect syscall.
|
inline |
return the Connection's file descriptor
int Connection::listen | ( | int | backlog | ) |
Same as listen syscall on current fd.
ssize_t Connection::read | ( | void * | buf, |
size_t | count | ||
) |
Call the underlying system call on Connection's file descriptor.
This function calls the system call with the same name. If the socket is ready for the required function it returns immediately, otherwise it blocks in the user-level (blocks uThread not kThread), and polls the file descriptor until it becomes ready.
The return results is the same as the underlying system call except that the following condition is never true when the function returns: (res == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
which means the Connection object does the polling and only returns when an error occurs or the socket is ready.
ssize_t Connection::recv | ( | void * | buf, |
size_t | len, | ||
int | flags | ||
) |
Call the underlying system call on Connection's file descriptor.
This function calls the system call with the same name. If the socket is ready for the required function it returns immediately, otherwise it blocks in the user-level (blocks uThread not kThread), and polls the file descriptor until it becomes ready.
The return results is the same as the underlying system call except that the following condition is never true when the function returns: (res == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
which means the Connection object does the polling and only returns when an error occurs or the socket is ready.
ssize_t Connection::recvfrom | ( | void * | buf, |
size_t | len, | ||
int | flags, | ||
struct sockaddr * | src_addr, | ||
socklen_t * | addrlen | ||
) |
Call the underlying system call on Connection's file descriptor.
This function calls the system call with the same name. If the socket is ready for the required function it returns immediately, otherwise it blocks in the user-level (blocks uThread not kThread), and polls the file descriptor until it becomes ready.
The return results is the same as the underlying system call except that the following condition is never true when the function returns: (res == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
which means the Connection object does the polling and only returns when an error occurs or the socket is ready.
int Connection::recvmmsg | ( | int | sockfd, |
struct mmsghdr * | msgvec, | ||
unsigned int | vlen, | ||
unsigned int | flags, | ||
struct timespec * | timeout | ||
) |
Call the underlying system call on Connection's file descriptor.
This function calls the system call with the same name. If the socket is ready for the required function it returns immediately, otherwise it blocks in the user-level (blocks uThread not kThread), and polls the file descriptor until it becomes ready.
The return results is the same as the underlying system call except that the following condition is never true when the function returns: (res == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
which means the Connection object does the polling and only returns when an error occurs or the socket is ready.
ssize_t Connection::recvmsg | ( | int | sockfd, |
struct msghdr * | msg, | ||
int | flags | ||
) |
Call the underlying system call on Connection's file descriptor.
This function calls the system call with the same name. If the socket is ready for the required function it returns immediately, otherwise it blocks in the user-level (blocks uThread not kThread), and polls the file descriptor until it becomes ready.
The return results is the same as the underlying system call except that the following condition is never true when the function returns: (res == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
which means the Connection object does the polling and only returns when an error occurs or the socket is ready.
ssize_t Connection::send | ( | const void * | buf, |
size_t | len, | ||
int | flags | ||
) |
Call the underlying system call on Connection's file descriptor.
This function calls the system call with the same name. If the socket is ready for the required function it returns immediately, otherwise it blocks in the user-level (blocks uThread not kThread), and polls the file descriptor until it becomes ready.
The return results is the same as the underlying system call except that the following condition is never true when the function returns: (res == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
which means the Connection object does the polling and only returns when an error occurs or the socket is ready.
int Connection::sendmmsg | ( | int | sockfd, |
struct mmsghdr * | msgvec, | ||
unsigned int | vlen, | ||
unsigned int | flags | ||
) |
Call the underlying system call on Connection's file descriptor.
This function calls the system call with the same name. If the socket is ready for the required function it returns immediately, otherwise it blocks in the user-level (blocks uThread not kThread), and polls the file descriptor until it becomes ready.
The return results is the same as the underlying system call except that the following condition is never true when the function returns: (res == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
which means the Connection object does the polling and only returns when an error occurs or the socket is ready.
ssize_t Connection::sendmsg | ( | const struct msghdr * | msg, |
int | flags | ||
) |
Call the underlying system call on Connection's file descriptor.
This function calls the system call with the same name. If the socket is ready for the required function it returns immediately, otherwise it blocks in the user-level (blocks uThread not kThread), and polls the file descriptor until it becomes ready.
The return results is the same as the underlying system call except that the following condition is never true when the function returns: (res == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
which means the Connection object does the polling and only returns when an error occurs or the socket is ready.
ssize_t Connection::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's file descriptor.
This function calls the system call with the same name. If the socket is ready for the required function it returns immediately, otherwise it blocks in the user-level (blocks uThread not kThread), and polls the file descriptor until it becomes ready.
The return results is the same as the underlying system call except that the following condition is never true when the function returns: (res == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
which means the Connection object does the polling and only returns when an error occurs or the socket is ready.
int Connection::socket | ( | int | domain, |
int | type, | ||
int | protocol | ||
) |
Same as socket syscall, set the fd for current connection.
ssize_t Connection::write | ( | const void * | buf, |
size_t | count | ||
) |
Call the underlying system call on Connection's file descriptor.
This function calls the system call with the same name. If the socket is ready for the required function it returns immediately, otherwise it blocks in the user-level (blocks uThread not kThread), and polls the file descriptor until it becomes ready.
The return results is the same as the underlying system call except that the following condition is never true when the function returns: (res == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
which means the Connection object does the polling and only returns when an error occurs or the socket is ready.