uThreads  0.3.0
Public Member Functions | List of all members
Connection Class Reference

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...
 
Connectionaccept (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...
 

Detailed Description

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.

Constructor & Destructor Documentation

Connection::Connection ( )
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

Connection::Connection ( int  fd)
inline

Create a connection object with the provided fd.

Parameters
fdIf 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.

Returns
same as socket syscall

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

Member Function Documentation

int Connection::accept ( Connection conn,
struct sockaddr *  addr,
socklen_t *  addrlen 
)

nonblocking accept syscall and updating the passed Connection object

Parameters
connPointer to a Connection object that is not initialized
Returns
same as accept system call

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.

Returns
Newly created connection

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.

Returns
Same as bind syscall
int Connection::close ( )

closes the socket

Returns
the same as close system call
int Connection::connect ( const struct sockaddr *  addr,
socklen_t  addrlen 
)

Same as connect syscall.

Returns
Same as connect syscall
int Connection::getFd ( ) const
inline

return the Connection's file descriptor

Returns
file descriptor
int Connection::listen ( int  backlog)

Same as listen syscall on current fd.

Returns
Same as listen syscall
ssize_t Connection::read ( void *  buf,
size_t  count 
)

Call the underlying system call on Connection's file descriptor.

Returns
Same as what the related systemcall returns

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.

Returns
Same as what the related systemcall returns

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.

Returns
Same as what the related systemcall returns

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.

Returns
Same as what the related systemcall returns

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.

Returns
Same as what the related systemcall returns

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.

Returns
Same as what the related systemcall returns

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.

Returns
Same as what the related systemcall returns

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.

Returns
Same as what the related systemcall returns

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.

Returns
Same as what the related systemcall returns

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.

Returns
same as socket syscall The unerlying socket is always nonbelocking. This is achieved by adding a (| SOCK_NONBLOCK) to type, thus requires linux kernels > 2.6.27
ssize_t Connection::write ( const void *  buf,
size_t  count 
)

Call the underlying system call on Connection's file descriptor.

Returns
Same as what the related systemcall returns

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.


The documentation for this class was generated from the following files: