\hypertarget{nmsgLib_8c}{
\section{nmsg\-Lib.c File Reference}
\label{nmsgLib_8c}\index{nmsgLib.c@{nmsgLib.c}}
}
Routines for handling formatted network messages. 

{\tt \#include \char`\"{}NMSG/nmsg\-Lib.h\char`\"{}}\par
{\tt \#include $<$stdlib.h$>$}\par
{\tt \#include $<$unistd.h$>$}\par
{\tt \#include $<$sys/types.h$>$}\par
{\tt \#include $<$sys/socket.h$>$}\par
{\tt \#include $<$netdb.h$>$}\par
{\tt \#include $<$netinet/in.h$>$}\par
{\tt \#include $<$pthread.h$>$}\par
{\tt \#include $<$errno.h$>$}\par
{\tt \#include $<$string.h$>$}\par
{\tt \#include $<$stdio.h$>$}\par


Include dependency graph for nmsg\-Lib.c:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=125pt]{nmsgLib_8c__incl}
\end{center}
\end{figure}
\subsection*{Functions}
\begin{CompactItemize}
\item 
int \hyperlink{nmsgLib_8c_a0}{nmsg\-Check} (void $\ast$hndl)
\begin{CompactList}\small\item\em Check a network connection handle. \item\end{CompactList}\item 
int \hyperlink{nmsgLib_8c_a1}{nmsg\-Close} (void $\ast$hndl, int force)
\begin{CompactList}\small\item\em Terminate a message client or server. \item\end{CompactList}\item 
int \hyperlink{nmsgLib_8c_a2}{nmsg\-Connect} (void $\ast$$\ast$hndl, const char $\ast$node, int port)
\begin{CompactList}\small\item\em Connect to a message server. \item\end{CompactList}\item 
int \hyperlink{nmsgLib_8c_a3}{nmsg\-Connect\-Asy} (void $\ast$$\ast$hndl, const char $\ast$node, int port, int prio, \hyperlink{nmsgLib_8h_a3}{nmsg\-Conn\-CB\_\-t} conn\-Rtn, \hyperlink{nmsgLib_8h_a4}{nmsg\-Disc\-CB\_\-t} disc\-Rtn, void $\ast$u\-Parm)
\begin{CompactList}\small\item\em Connect asynchronously (robustly) to a message server. \item\end{CompactList}\item 
const char $\ast$ \hyperlink{nmsgLib_8c_a4}{nmsg\-Error\-Msg} (int status)
\begin{CompactList}\small\item\em Obtain the text of an error message. \item\end{CompactList}\item 
void \hyperlink{nmsgLib_8c_a5}{nmsg\-Error\-Report} (int status, const char $\ast$func)
\begin{CompactList}\small\item\em Display an error message. \item\end{CompactList}\item 
int \hyperlink{nmsgLib_8c_a6}{nmsg\-Listen} (void $\ast$$\ast$hndl, int port, int max\-Conn, int max\-Data, int prio, \hyperlink{nmsgLib_8h_a3}{nmsg\-Conn\-CB\_\-t} conn\-Rtn, \hyperlink{nmsgLib_8h_a4}{nmsg\-Disc\-CB\_\-t} disc\-Rtn, \hyperlink{nmsgLib_8h_a5}{nmsg\-Rcve\-CB\_\-t} rcve\-Rtn, void $\ast$u\-Parm)
\begin{CompactList}\small\item\em Establish a message server. \item\end{CompactList}\item 
int \hyperlink{nmsgLib_8c_a7}{nmsg\-Remote\-IP} (void $\ast$hndl, int $\ast$ip)
\begin{CompactList}\small\item\em Get the IP address of the remote end. \item\end{CompactList}\item 
int \hyperlink{nmsgLib_8c_a8}{nmsg\-Send} (void $\ast$hndl, \hyperlink{structnmsgBuff__s}{nmsg\-Buff\_\-t} $\ast$msg)
\begin{CompactList}\small\item\em Send a message and return immediately. \item\end{CompactList}\item 
int \hyperlink{nmsgLib_8c_a9}{nmsg\-Send\-W} (void $\ast$hndl, \hyperlink{structnmsgBuff__s}{nmsg\-Buff\_\-t} $\ast$msg, \hyperlink{structnmsgBuff__s}{nmsg\-Buff\_\-t} $\ast$$\ast$reply\-P, \hyperlink{structnmsgBuff__s}{nmsg\-Buff\_\-t} $\ast$reply, int max\-Data)
\begin{CompactList}\small\item\em Send a message and receive reply. \item\end{CompactList}\item 
int \hyperlink{nmsgLib_8c_a10}{nmsg\-Stats} (void $\ast$hndl, \hyperlink{structnmsgStats__s}{nmsg\-Stats\_\-t} $\ast$stats)
\begin{CompactList}\small\item\em Get connection statistics. \item\end{CompactList}\item 
int \hyperlink{nmsgLib_8c_a11}{nmsg\-Wait} (void $\ast$hndl, \hyperlink{structnmsgBuff__s}{nmsg\-Buff\_\-t} $\ast$$\ast$reply\-P, \hyperlink{structnmsgBuff__s}{nmsg\-Buff\_\-t} $\ast$reply, int max\-Data)
\begin{CompactList}\small\item\em Wait for a reply to a sent message. \item\end{CompactList}\end{CompactItemize}


\subsection{Detailed Description}
Routines for handling formatted network messages. 

\begin{Desc}
\item[Author:]Owen H Saxton\end{Desc}
\begin{Desc}
\item[Id]\hyperlink{nmsgLib_8c}{nmsg\-Lib.c},v 1.11 2008/06/06 23:38:56 saxton Exp \end{Desc}


\subsection{Function Documentation}
\hypertarget{nmsgLib_8c_a0}{
\index{nmsgLib.c@{nmsg\-Lib.c}!nmsgCheck@{nmsgCheck}}
\index{nmsgCheck@{nmsgCheck}!nmsgLib.c@{nmsg\-Lib.c}}
\subsubsection[nmsgCheck]{\setlength{\rightskip}{0pt plus 5cm}int nmsg\-Check (void $\ast$ {\em hndl})}}
\label{nmsgLib_8c_a0}


Check a network connection handle. 

This routine checks whether a network handle is valid or not.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hndl}]The handle for the connection.\end{description}
\end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em 1}]The handle is valid. \item[{\em 0}]The handle is invalid.\end{description}
\end{Desc}
\hypertarget{nmsgLib_8c_a1}{
\index{nmsgLib.c@{nmsg\-Lib.c}!nmsgClose@{nmsgClose}}
\index{nmsgClose@{nmsgClose}!nmsgLib.c@{nmsg\-Lib.c}}
\subsubsection[nmsgClose]{\setlength{\rightskip}{0pt plus 5cm}int nmsg\-Close (void $\ast$ {\em hndl}, int {\em force})}}
\label{nmsgLib_8c_a1}


Terminate a message client or server. 

This routine closes a network connection or stops a network server.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hndl}]The handle of the connection or server.\item[{\em force}]For a client connection, if TRUE, the connection is closed even if another task is in the process of sending a message. If FALSE, the closing waits until any activity has ceased. Not used for a server.\end{description}
\end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em NMSG\_\-SUCCESS}]Success. \item[{\em NMSG\_\-INVHNDL}]Invalid network handle.\end{description}
\end{Desc}
\hypertarget{nmsgLib_8c_a2}{
\index{nmsgLib.c@{nmsg\-Lib.c}!nmsgConnect@{nmsgConnect}}
\index{nmsgConnect@{nmsgConnect}!nmsgLib.c@{nmsg\-Lib.c}}
\subsubsection[nmsgConnect]{\setlength{\rightskip}{0pt plus 5cm}int nmsg\-Connect (void $\ast$$\ast$ {\em hndl}, const char $\ast$ {\em node}, int {\em port})}}
\label{nmsgLib_8c_a2}


Connect to a message server. 

This routine establishes a network connection to a message server.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hndl}]The address of a pointer to receive the identifying handle for the network connection.\item[{\em node}]The address of the name of the node containing the message server.\item[{\em port}]The port number on which to establish the connection.\end{description}
\end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em NMSG\_\-SUCCESS}]The connection was established successfully. \item[{\em NMSG\_\-NOMEMORY}]Insuffucuent memory available. \item[{\em NMSG\_\-UNKNHOST}]Host is unknown. \item[{\em NMSG\_\-NOSEMCRE}]Unable to create sync. semaphore (Vx\-Works) \item[{\em NMSG\_\-xxx}]Various connection errors.\end{description}
\end{Desc}
\hypertarget{nmsgLib_8c_a3}{
\index{nmsgLib.c@{nmsg\-Lib.c}!nmsgConnectAsy@{nmsgConnectAsy}}
\index{nmsgConnectAsy@{nmsgConnectAsy}!nmsgLib.c@{nmsg\-Lib.c}}
\subsubsection[nmsgConnectAsy]{\setlength{\rightskip}{0pt plus 5cm}int nmsg\-Connect\-Asy (void $\ast$$\ast$ {\em hndl}, const char $\ast$ {\em node}, int {\em port}, int {\em prio}, \hyperlink{nmsgLib_8h_a3}{nmsg\-Conn\-CB\_\-t} {\em conn\-Rtn}, \hyperlink{nmsgLib_8h_a4}{nmsg\-Disc\-CB\_\-t} {\em disc\-Rtn}, void $\ast$ {\em u\-Parm})}}
\label{nmsgLib_8c_a3}


Connect asynchronously (robustly) to a message server. 

This routine establishes a robust network connection to a message server. The connection is done asynchronously, and the attempt is retried periodically if the initial attempt fails, or if the connection is broken.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hndl}]The address of a pointer to receive the identifying handle for the network connection.\item[{\em node}]The address of the name of the node containing the message server.\item[{\em port}]The port number on which to establish the connection.\item[{\em prio}]The priority of the connection task, where applicable. If 0, the default value (100) is used.\item[{\em conn\-Rtn}]The address of a routine to be called when the connection has been made.\item[{\em disc\-Rtn}]The address of a routine to be called when the connection is broken.\item[{\em u\-Parm}]A parameter to be passed to the connect and disconnect callback routines.\end{description}
\end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em NMSG\_\-SUCCESS}]The connection was established successfully. \item[{\em NMSG\_\-NOMEMORY}]Insuffucuent memory available. \item[{\em NMSG\_\-UNKNHOST}]Host is unknown. \item[{\em NMSG\_\-NOSEMCRE}]Unable to create sync. semaphore (Vx\-Works) \item[{\em NMSG\_\-NOTASKCR}]Unable to spawn connection task.\end{description}
\end{Desc}
\hypertarget{nmsgLib_8c_a4}{
\index{nmsgLib.c@{nmsg\-Lib.c}!nmsgErrorMsg@{nmsgErrorMsg}}
\index{nmsgErrorMsg@{nmsgErrorMsg}!nmsgLib.c@{nmsg\-Lib.c}}
\subsubsection[nmsgErrorMsg]{\setlength{\rightskip}{0pt plus 5cm}const char$\ast$ nmsg\-Error\-Msg (int {\em status})}}
\label{nmsgLib_8c_a4}


Obtain the text of an error message. 

This routine returns a pointer to the message corresponding to a given NMSG status code.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em status}]The NMSG status code.\end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]The address of the corresponding message text.\end{Desc}
\hypertarget{nmsgLib_8c_a5}{
\index{nmsgLib.c@{nmsg\-Lib.c}!nmsgErrorReport@{nmsgErrorReport}}
\index{nmsgErrorReport@{nmsgErrorReport}!nmsgLib.c@{nmsg\-Lib.c}}
\subsubsection[nmsgErrorReport]{\setlength{\rightskip}{0pt plus 5cm}void nmsg\-Error\-Report (int {\em status}, const char $\ast$ {\em func})}}
\label{nmsgLib_8c_a5}


Display an error message. 

This routine outputs to stderr the message corresponding to a given NMSG status code.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em status}]The NMSG status code.\item[{\em func}]The function name to be displayed at the start of the message.\end{description}
\end{Desc}
\hypertarget{nmsgLib_8c_a6}{
\index{nmsgLib.c@{nmsg\-Lib.c}!nmsgListen@{nmsgListen}}
\index{nmsgListen@{nmsgListen}!nmsgLib.c@{nmsg\-Lib.c}}
\subsubsection[nmsgListen]{\setlength{\rightskip}{0pt plus 5cm}int nmsg\-Listen (void $\ast$$\ast$ {\em hndl}, int {\em port}, int {\em max\-Conn}, int {\em max\-Data}, int {\em prio}, \hyperlink{nmsgLib_8h_a3}{nmsg\-Conn\-CB\_\-t} {\em conn\-Rtn}, \hyperlink{nmsgLib_8h_a4}{nmsg\-Disc\-CB\_\-t} {\em disc\-Rtn}, \hyperlink{nmsgLib_8h_a5}{nmsg\-Rcve\-CB\_\-t} {\em rcve\-Rtn}, void $\ast$ {\em u\-Parm})}}
\label{nmsgLib_8c_a6}


Establish a message server. 

This routine establishes a network message server (listener).

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hndl}]The address of a pointer to receive the identifying handle for the network listener.\item[{\em port}]The port number on which to establish the server.\item[{\em max\-Conn}]The maximum number of concurrent connections that the server will allow.\item[{\em max\-Data}]The maximum number of data bytes that can be transferred in a single message.\item[{\em prio}]The priority of the listener (server) and receiver tasks, where applicable. If 0, the default value (100) is used.\item[{\em conn\-Rtn}]The address of a routine to be called when a new connection has been made. The connection will be rejected if this routine returns ERROR status.\item[{\em disc\-Rtn}]The address of a routine to be called when a connection is broken.\item[{\em rcve\-Rtn}]The address of a routine to be called whenever a message is received.\item[{\em u\-Parm}]A parameter to be passed to the connect, disconnect and receive callback routines.\end{description}
\end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em NMSG\_\-SUCCESS}]The connection was established successfully. \item[{\em NMSG\_\-NOMEMORY}]Insuffucuent memory available. \item[{\em NMSG\_\-NOTASKCR}]Unable to spawn listener task. \item[{\em NMSG\_\-xxx}]Various system errors.\end{description}
\end{Desc}
\hypertarget{nmsgLib_8c_a7}{
\index{nmsgLib.c@{nmsg\-Lib.c}!nmsgRemoteIP@{nmsgRemoteIP}}
\index{nmsgRemoteIP@{nmsgRemoteIP}!nmsgLib.c@{nmsg\-Lib.c}}
\subsubsection[nmsgRemoteIP]{\setlength{\rightskip}{0pt plus 5cm}int nmsg\-Remote\-IP (void $\ast$ {\em hndl}, int $\ast$ {\em ip})}}
\label{nmsgLib_8c_a7}


Get the IP address of the remote end. 

This routine returns the IP address of the remote end of a connection. It can be used on either end of a connection.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hndl}]The handle for the connection.\item[{\em ip}]The addressof an integer to receive the IP address.\end{description}
\end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em NMSG\_\-SUCCESS}]Success. \item[{\em NMSG\_\-INVHNDL}]Invalid network handle.\end{description}
\end{Desc}
\hypertarget{nmsgLib_8c_a8}{
\index{nmsgLib.c@{nmsg\-Lib.c}!nmsgSend@{nmsgSend}}
\index{nmsgSend@{nmsgSend}!nmsgLib.c@{nmsg\-Lib.c}}
\subsubsection[nmsgSend]{\setlength{\rightskip}{0pt plus 5cm}int nmsg\-Send (void $\ast$ {\em hndl}, \hyperlink{structnmsgBuff__s}{nmsg\-Buff\_\-t} $\ast$ {\em msg})}}
\label{nmsgLib_8c_a8}


Send a message and return immediately. 

This routine sends a message on a network connection and returns without waiting for a reply.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hndl}]The handle for the connection.\item[{\em msg}]The address of message to be sent. It must be formatted using the nmsg\-Buff\_\-t structure type.\end{description}
\end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em NMSG\_\-SUCCESS}]Success. \item[{\em NMSG\_\-INVHNDL}]Invalid network handle. \item[{\em NMSG\_\-NOTCONN}]Not connected to server. \item[{\em NMSG\_\-INVSEMA}]Invalid semaphore. \item[{\em NMSG\_\-DISCONN}]Server connection was broken.\end{description}
\end{Desc}
\hypertarget{nmsgLib_8c_a9}{
\index{nmsgLib.c@{nmsg\-Lib.c}!nmsgSendW@{nmsgSendW}}
\index{nmsgSendW@{nmsgSendW}!nmsgLib.c@{nmsg\-Lib.c}}
\subsubsection[nmsgSendW]{\setlength{\rightskip}{0pt plus 5cm}int nmsg\-Send\-W (void $\ast$ {\em hndl}, \hyperlink{structnmsgBuff__s}{nmsg\-Buff\_\-t} $\ast$ {\em msg}, \hyperlink{structnmsgBuff__s}{nmsg\-Buff\_\-t} $\ast$$\ast$ {\em reply\-P}, \hyperlink{structnmsgBuff__s}{nmsg\-Buff\_\-t} $\ast$ {\em reply}, int {\em max\-Data})}}
\label{nmsgLib_8c_a9}


Send a message and receive reply. 

This routine sends a message on a network connection and waits for a reply.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hndl}]The handle for the connection.\item[{\em msg}]The address of message to be sent. It must be formatted using the nmsg\-Buff\_\-t structure type.\item[{\em reply\-P}]The address of a pointer to receive the address of the reply. If a reply area is supplied and it is big enough, this will be the address of this area. Otherwise a sufficiently large reply area is allocated and its address returned here. In this latter case, the memory must be relinquished using free() after it is no longer needed.\item[{\em reply}]The address of an area to receive the reply to the message, or NULL if no area is to be used.\item[{\em max\-Data}]The maximum number of data bytes that can be put into the supplied reply area.\end{description}
\end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em NMSG\_\-SUCCESS}]Success. \item[{\em NMSG\_\-INVHNDL}]Invalid network handle. \item[{\em NMSG\_\-NOTCONN}]Not connected to server. \item[{\em NMSG\_\-INVSEMA}]Invalid semaphore. \item[{\em NMSG\_\-DISCONN}]Server connection was broken.\end{description}
\end{Desc}
\hypertarget{nmsgLib_8c_a10}{
\index{nmsgLib.c@{nmsg\-Lib.c}!nmsgStats@{nmsgStats}}
\index{nmsgStats@{nmsgStats}!nmsgLib.c@{nmsg\-Lib.c}}
\subsubsection[nmsgStats]{\setlength{\rightskip}{0pt plus 5cm}int nmsg\-Stats (void $\ast$ {\em hndl}, \hyperlink{structnmsgStats__s}{nmsg\-Stats\_\-t} $\ast$ {\em stats})}}
\label{nmsgLib_8c_a10}


Get connection statistics. 

This routine returns a block of connection statistics containing the number of connects, disconnects, messages sent/received, bytes sent and bytes received.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hndl}]The handle for the connection.\item[{\em stats}]The address of an area to receive the statistics block.\end{description}
\end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em NMSG\_\-SUCCESS}]Success. \item[{\em NMSG\_\-INVHNDL}]Invalid network handle.\end{description}
\end{Desc}
\hypertarget{nmsgLib_8c_a11}{
\index{nmsgLib.c@{nmsg\-Lib.c}!nmsgWait@{nmsgWait}}
\index{nmsgWait@{nmsgWait}!nmsgLib.c@{nmsg\-Lib.c}}
\subsubsection[nmsgWait]{\setlength{\rightskip}{0pt plus 5cm}int nmsg\-Wait (void $\ast$ {\em hndl}, \hyperlink{structnmsgBuff__s}{nmsg\-Buff\_\-t} $\ast$$\ast$ {\em reply\-P}, \hyperlink{structnmsgBuff__s}{nmsg\-Buff\_\-t} $\ast$ {\em reply}, int {\em max\-Data})}}
\label{nmsgLib_8c_a11}


Wait for a reply to a sent message. 

This routine waits for a reply to a previously sent message.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em hndl}]The handle for the connection.\item[{\em reply\-P}]The address of a pointer to receive the address of the reply. If a reply area is supplied and it is big enough, this will be the address of this area. Otherwise a sufficiently large reply area is allocated and its address returned here. In this latter case, the memory must be relinquished using free() after it is no longer needed.\item[{\em reply}]The address of an area to receive the reply to the message, or NULL if no area is to be used.\item[{\em max\-Data}]The maximum number of data bytes that can be put into the supplied reply area.\end{description}
\end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em NMSG\_\-SUCCESS}]Success. \item[{\em NMSG\_\-INVHNDL}]Invalid network handle. \item[{\em NMSG\_\-NOTCONN}]Not connected to server. \item[{\em NMSG\_\-INVSEMA}]Invalid semaphore. \item[{\em NMSG\_\-DISCONN}]Server connection was broken.\end{description}
\end{Desc}
