Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

ns2net.c File Reference

#include <malloc.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <nsnet.h>
#include <fcntl.h>
#include <assert.h>
#include <neuro/neuro.h>
#include <neuro/ns2net.h>

Go to the source code of this file.

Data Structures

struct  NSNet
struct  NSNetBindHandlerInternal
struct  NSNetConnectionController
struct  NSNetConnectionHandlerInternal
struct  NSNetReadHandlerInternal

Defines

#define MAXCONN   256

Functions

int attachConnectionReadHandler (struct NSNetConnectionController *nscc, const struct NSNetConnectionReadHandler *nscch, void *udata)
 Attaches a struct NSNetConnectionReadHandler to a live network connetion.

int attemptBind (struct NSNet *ns, const struct NSNetBindHandler *nsb, int isLocalOnly, unsigned short portNum, void *udata)
 Performs an asynchronous bind on a network port and begins listening for connections.

int setNSnonblocking (struct NSNet *ns, sock_t sock_fd)
int attemptConnect (struct NSNet *ns, const struct NSNetConnectionHandler *nsc, const char *destaddr, unsigned short destPort, void *udata)
 Creates a new socket and initiates an asychronous connection request to the specified hostname and port.

void waitForNetEvent (struct NSNet *ns, int timeout)
 Waits for a network event to arrive, and dispatches it, or times out.

NSNetnewNSNet (void)
 Initializes networking in a system-independent way.

void closeNSNet (struct NSNet *ns)
 Closes an NSNet interface, freeing the memory and other resources associated with it.

int closeConnection (struct NSNetConnectionController *nscc)
 Closes a network connection.

int writeNSBytes (struct NSNetConnectionController *nscc, void *buf, int len)
 Writes a number of bytes to a network connection.


Define Documentation

#define MAXCONN   256
 

Definition at line 43 of file ns2net.c.


Function Documentation

int attachConnectionReadHandler struct NSNetConnectionController nscc,
const struct NSNetConnectionReadHandler nscch,
void *  udata
 

Attaches a struct NSNetConnectionReadHandler to a live network connetion.

Parameters:
nscc a pointer to the struct NSNetConnectionController in charge of the network connection
nscch a pointer to a callback function block in a struct NSNetConnectionReadHandler
udata a pointer to user-defined data to be passed to the handler functions
Returns:
0 on success
Once a connection is established, from a successful attemptConnect() or attemptBind(), a read handler may be attached to the associated struct NSNetConnectionController. Once a read handler is attached, it will receive to types of callback messages: readBytes will be called when new bytes are read from the connection. closed will be called if either side closes the connection, or if the connection closes unexpectedly due to a network-level error.

Definition at line 62 of file ns2net.c.

References NSNetConnectionController::fd, NSNetConnectionController::ns, and putInt().

int attemptBind struct NSNet ns,
const struct NSNetBindHandler nsb,
int  isLocalOnly,
unsigned short  portNum,
void *  udata
 

Performs an asynchronous bind on a network port and begins listening for connections.

Parameters:
ns Pointer to a struct NSNet
nsb Pointer to a user-supplied struct NSNetBindHandler function table
isLocalOnly a boolean flag indicating whether only the local loopback interface should be bound if nonzero, or all network interfaces if 0.
portNum a network port number to bind a listening socket
udata User-defined data to be passed to the handler functions
This function opens a new network socket and binds it to a listening port. New connections or errors are communicated via callback functions in nsb. If clients attempt to connect to this port, then the nsb success() function will be called. If there is a network error, then nsb error() function will be called. Note that success may be called multiple times if there are multiple connections. Each time success() is called, the user-data supplied as udata will be passed as well as a new struct NSNetConnectionController to handle reading and writing on the newly accepted connection. Note that since this function is asynchronous, it will return immediately, and if your application wants to wait for an incoming connection, it must use waitForNetEvent() after calling this one.

Definition at line 78 of file ns2net.c.

References NSNet::bindMap, NSNetBindHandler::error, MAXCLIENTS, putInt(), rsockaddr_t, sock_fd, and sock_t.

int attemptConnect struct NSNet ns,
const struct NSNetConnectionHandler nsc,
const char *  destaddr,
unsigned short  destPort,
void *  udata
 

Creates a new socket and initiates an asychronous connection request to the specified hostname and port.

Parameters:
ns Pointer to a struct NSNet
nsc User-defined callback functions for later results
destaddr A string containing either a dotted-quad IP address or a hostname specifying the destination address to connect to
destPort The network port number to connect to
udata User-supplied data to be passed on to the handler functions
Returns:
0 upon successful start of the connection request.
Creates a new network socket and begins trying to connect to the address specified by destaddr and destPort. Since this function is asynchronous, it returns immediately. If the connection is successful, the nsb success() function will be called. If the connection is refused, for instance because the destination machine is not listening on the specified port, then the nsb refused() function will be called. If a hostname is supplied and there is no address available via DNS lookup, then unknownHost will be called. If too much time passes trying to connect, then timedOut will be called.

Definition at line 203 of file ns2net.c.

References NSNet::connectMap, putInt(), rassert, rsockaddr_t, setNSnonblocking(), sock_t, and NSNetConnectionHandler::success.

int closeConnection struct NSNetConnectionController nscc  ) 
 

Closes a network connection.

Parameters:
nscc The struct NSNetConnectionController associated with the network connection to be closed.
Returns:
0 on success. This function closes a network connection. Note that if there are readers attached on either or both sides, their close() method will be called.

Definition at line 372 of file ns2net.c.

References NSNetConnectionController::fd.

void closeNSNet struct NSNet ns  ) 
 

Closes an NSNet interface, freeing the memory and other resources associated with it.

Parameters:
ns A pointer to the NSNet structure that was opened previously using newNSNet()

Definition at line 364 of file ns2net.c.

References NSNet::bindMap, NSNet::connectMap, freeStringTable(), and NSNet::readerMap.

struct NSNet* newNSNet void   ) 
 

Initializes networking in a system-independent way.

Returns:
A pointer to an opaque struct NSNet

Definition at line 351 of file ns2net.c.

References newStringTable().

int setNSnonblocking struct NSNet ns,
sock_t  sock_fd
 

Definition at line 141 of file ns2net.c.

References sock_fd.

Referenced by attemptConnect(), and waitForNetEvent().

void waitForNetEvent struct NSNet ns,
int  timeout
 

Waits for a network event to arrive, and dispatches it, or times out.

Parameters:
ns A pointer to the NSNet structure that was opened previously using newNSNet()
timeout a timeout value in milliseconds. The function will return if no event occurs when the timeout value has elapsed.

Definition at line 273 of file ns2net.c.

References NSNet::bindMap, buf, NSNet::connectMap, NSNet::errfds, findInt(), NSNet::max_fd, max_fd, NSNet::readerMap, NSNet::readfds, setNSnonblocking(), sock_t, and NSNet::writefds.

int writeNSBytes struct NSNetConnectionController nscc,
void *  buf,
int  len
 

Writes a number of bytes to a network connection.

Parameters:
nscc The struct NSNetConnectionController associated with the network connection.
buf The memory buffer holding the bytes to be written
len The number of bytes to be written
Returns:
0 on success, or nonzero if a writing error occurs before all bytes have been written. Before using this function, you must obtain a struct NSNetConnectionController via callback functions (success) from attemptBind() or attemptConnection()

Definition at line 378 of file ns2net.c.

References buf, and NSNetConnectionController::fd.


Generated on Tue Feb 8 00:05:18 2005 for Neuroserver by doxygen 1.3.3