#ifndef SOCK_TCP_H
#define SOCK_TCP_H
/**
* @file
*
* TCP implementation of sock_stream interface.
*/
#include "sock_internal.h"
#include "sock_fd.h"
#include <netdb.h>
/**
* Contains the base sock_stream struct, and the file descriptor
*/
struct sock_tcp {
/** The base struct for sock_stream_* functions */
struct sock_fd base_fd;
/** The current connect_async resolved address */
struct addrinfo *async_res, *async_cur;
};
/**
* Get a sock_fd pointer from a sock_tcp pointer
*/
#define SOCK_TCP_FD(sock_ptr) (&(sock_ptr)->base_fd)
/**
* Get a sock_base pointer from a sock_tcp pointer
*/
#define SOCK_TCP_BASE(sock_ptr) SOCK_FD_BASE(SOCK_TCP_FD(sock_ptr))
/**
* Get the sock_stream.err pointer from a sock_tcp pointer
*/
#define SOCK_TCP_ERR(sock_ptr) SOCK_ERR(SOCK_TCP_BASE(sock_ptr))
/**
* Initialize a blank sock_tcp by creating a new socket (using the socket() syscall), but doesn't do anything further.
*
* This uses the ai_family, ai_socktype and ai_protocol fields from the given addrinfo.
*/
err_t sock_tcp_init_socket (struct sock_tcp *sock, struct addrinfo *addr, struct error_info *err);
/**
* Initiate an async connection operation on the given socket to the given address. Once the connect() completes,
* either the on_connect or the on_error callback will be called.
*
* If, for some weird reason, this ends up doing a blocking connect(), the on_connect callback will be called directly.
* If the async connect fails, this just returns the error.
*
* @param sock the unconnected TCP sockect to connect with
* @param addr the address to connect to
* @param err returned error info
*/
err_t sock_tcp_connect_async_addr (struct sock_tcp *sock, struct addrinfo *addr, struct error_info *err);
/**
* Attempt to connect asyncronously to the given hostname/service. Once a connection has been established, the
* on_connect() callback will be called.
*
* In case of errors, either on_error() will be called, or an error returned - depending on when the error happaned.
*
* @param sock the unconnected TCP socket to connect with
* @param hostname the hostname to resolve
* @param service the service to connect to
* @param err returned error info
*/
err_t sock_tcp_connect_async_begin (struct sock_tcp *sock, const char *hostname, const char *service, struct error_info *err);
/**
* Initialize a blank sock_tcp by connecting in a blocking fashion.
*/
err_t sock_tcp_connect_blocking (struct sock_tcp *sock, const char *hostname, const char *service, struct error_info *err);
/**
* Free a non-connected sock_tcp
*/
void sock_tcp_free (struct sock_tcp *sock);
#endif /* SOCK_TCP_H */