\section{block\_\-receive.c File Reference}
\label{block__receive_8c}\index{block_receive.c@{block\_\-receive.c}}
{\tt \#include $<$stdio.h$>$}\par
{\tt \#include $<$sys/types.h$>$}\par
{\tt \#include $<$sys/socket.h$>$}\par
{\tt \#include $<$pthread.h$>$}\par
{\tt \#include $<$sys/time.h$>$}\par
{\tt \#include $<$unistd.h$>$}\par
{\tt \#include $<$sys/poll.h$>$}\par
{\tt \#include \char`\"{}libcomm.h\char`\"{}}\par
\subsection*{Functions}
\begin{CompactItemize}
\item 
void {\bf thread1} (struct {\bf LIBCOMMPTHREADS} $\ast$libcommpthreads)
\begin{CompactList}\small\item\em This is a part of {\bf block\_\-call}() {\rm (p.\,\pageref{block__receive_8c_a1})} and must not called from the user.\item\end{CompactList}\item 
int {\bf block\_\-call} (int fd, int id, int term, void($\ast$block\_\-call\_\-do)(int fd, int id, unsigned int type, char $\ast$buf, unsigned int size, int term), void($\ast$block\_\-call\_\-term)(int fd, int id))
\begin{CompactList}\small\item\em Start in thread, wait for a datablock and call the function block\_\-call\_\-do() if one was received or block\_\-call\_\-term() when the connection terminates.\item\end{CompactList}\item 
int {\bf block\_\-ifdata} (int fd)
\begin{CompactList}\small\item\em This function tests if new data is available to read on a stream.\item\end{CompactList}\item 
char $\ast$ {\bf block\_\-receive\_\-poll} (int fd, unsigned int $\ast$type, char $\ast$buf, unsigned int $\ast$size, unsigned int maxsize, int term)
\begin{CompactList}\small\item\em Test if is there data available on the socket and read it in a buffer if there is.\item\end{CompactList}\item 
char $\ast$ {\bf block\_\-receive} (int fd, unsigned int $\ast$type, char $\ast$buf, unsigned int $\ast$size, unsigned int maxsize, int term)
\begin{CompactList}\small\item\em Receive a block (composition of: type, size of datablock and datablock) from a socket.\item\end{CompactList}\item 
int {\bf block\_\-receive\_\-integer} (int fd, unsigned int $\ast$recvi)
\begin{CompactList}\small\item\em Receive an integer (two bytes; 16Bit) from the socket.\item\end{CompactList}\item 
int {\bf block\_\-receive\_\-nbytes} (int fd, char $\ast$buf, int n)
\begin{CompactList}\small\item\em Receive n bytes from socket.\item\end{CompactList}\end{CompactItemize}


\subsection{Function Documentation}
\index{block_receive.c@{block\_\-receive.c}!block_call@{block\_\-call}}
\index{block_call@{block\_\-call}!block_receive.c@{block\_\-receive.c}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}int block\_\-call (int {\em fd}, int {\em id}, int {\em term}, void($\ast$ {\em block\_\-call\_\-do})(int fd, int id, unsigned int type, char $\ast$buf, unsigned int size, int term), void($\ast$ {\em block\_\-call\_\-term})(int fd, int id))}\label{block__receive_8c_a1}


Start in thread, wait for a datablock and call the function block\_\-call\_\-do() if one was received or block\_\-call\_\-term() when the connection terminates.

\begin{Desc}
\item[Parameters: ]\par
\begin{description}
\item[{\em 
fd}](int) descriptor of socket\item[{\em 
id}](int) arbitrary id of background process / thread\item[{\em 
term}](int) 0: do not terminate the buffer, 1: terminate the buffer by appending a 0x00.\item[{\em 
block\_\-call\_\-do}](int fd, int id, unsigned int type, char $\ast$buf, unsigned int size, int term) (function) this function is called if a datablock was received. fd, id and term are the same as in {\bf block\_\-call}() {\rm (p.\,\pageref{block__receive_8c_a1})}. type describes the type of the received datablock, buf is a pointer to this datablock and size is the number of bytes of the datablock\item[{\em 
block\_\-call\_\-term}](int fd, int id) (function) this function is called if the connection terminates. fd and id are the same as in {\bf block\_\-call}() {\rm (p.\,\pageref{block__receive_8c_a1})}.\end{description}
\end{Desc}
\begin{Desc}
\item[Returns: ]\par
If all right zero otherwise non zero. \end{Desc}
\index{block_receive.c@{block\_\-receive.c}!block_ifdata@{block\_\-ifdata}}
\index{block_ifdata@{block\_\-ifdata}!block_receive.c@{block\_\-receive.c}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}int block\_\-ifdata (int {\em fd})}\label{block__receive_8c_a2}


This function tests if new data is available to read on a stream.

\begin{Desc}
\item[Parameters: ]\par
\begin{description}
\item[{\em 
fd}](int) discriptor of stream to test\end{description}
\end{Desc}
\begin{Desc}
\item[Returns: ]\par
(int) 1: Data to read; 0: No data to read \end{Desc}
\index{block_receive.c@{block\_\-receive.c}!block_receive@{block\_\-receive}}
\index{block_receive@{block\_\-receive}!block_receive.c@{block\_\-receive.c}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}char$\ast$ block\_\-receive (int {\em fd}, unsigned int $\ast$ {\em type}, char $\ast$ {\em buf}, unsigned int $\ast$ {\em size}, unsigned int {\em maxsize}, int {\em term})}\label{block__receive_8c_a4}


Receive a block (composition of: type, size of datablock and datablock) from a socket.

WARNING: The integers (type and size; excluding fd) are only 16 bit values (0 - 65535).\begin{Desc}
\item[Parameters: ]\par
\begin{description}
\item[{\em 
fd}](int) descriptor of socket\item[{\em 
type}](unsigned int $\ast$) pointer to integer, this value can be used as buyer's option\item[{\em 
buf}](char $\ast$) buffer for datablock. Memory will be allocated if this parameter is equal to null.\item[{\em 
size}](unsigned int $\ast$) pointer to integer in which the size of the received datablock is saved.\item[{\em 
maxsize}](unsigned int $\ast$) describes size of buf. This parameter will be ignored if bus is equal to null.\item[{\em 
term}](int) 0: do not terminate the buffer, 1: terminate the buffer by appending a 0x00.\end{description}
\end{Desc}
\begin{Desc}
\item[Returns: ]\par
(char $\ast$) pointer to buffer which contains the received datablock; NULL if fail. \end{Desc}
\index{block_receive.c@{block\_\-receive.c}!block_receive_integer@{block\_\-receive\_\-integer}}
\index{block_receive_integer@{block\_\-receive\_\-integer}!block_receive.c@{block\_\-receive.c}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}int block\_\-receive\_\-integer (int {\em fd}, unsigned int $\ast$ {\em recvi})}\label{block__receive_8c_a5}


Receive an integer (two bytes; 16Bit) from the socket.

\begin{Desc}
\item[Parameters: ]\par
\begin{description}
\item[{\em 
fd}](int) descriptor of socket\item[{\em 
recvi}](unsigned int $\ast$) pointer to integer in which the received integer is saved.\end{description}
\end{Desc}
\begin{Desc}
\item[Returns: ]\par
(int) 2: OK; -1: fail \end{Desc}
\index{block_receive.c@{block\_\-receive.c}!block_receive_nbytes@{block\_\-receive\_\-nbytes}}
\index{block_receive_nbytes@{block\_\-receive\_\-nbytes}!block_receive.c@{block\_\-receive.c}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}int block\_\-receive\_\-nbytes (int {\em fd}, char $\ast$ {\em buf}, int {\em n})}\label{block__receive_8c_a6}


Receive n bytes from socket.

\begin{Desc}
\item[Parameters: ]\par
\begin{description}
\item[{\em 
fd}](integer) descriptor of socket\item[{\em 
buf}](char $\ast$) buffer for saving the received bytes\item[{\em 
n}](integer) number of bytes to receive\end{description}
\end{Desc}
\begin{Desc}
\item[Returns: ]\par
(integer) n: OK; -1 fial \end{Desc}
\index{block_receive.c@{block\_\-receive.c}!block_receive_poll@{block\_\-receive\_\-poll}}
\index{block_receive_poll@{block\_\-receive\_\-poll}!block_receive.c@{block\_\-receive.c}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}char$\ast$ block\_\-receive\_\-poll (int {\em fd}, unsigned int $\ast$ {\em type}, char $\ast$ {\em buf}, unsigned int $\ast$ {\em size}, unsigned int {\em maxsize}, int {\em term})}\label{block__receive_8c_a3}


Test if is there data available on the socket and read it in a buffer if there is.

WARNING: The integers (type and size; excluding fd) are only 16 bit values (0 - 65535).\begin{Desc}
\item[Parameters: ]\par
\begin{description}
\item[{\em 
fd}](int) descriptor of socket\item[{\em 
type}](unsigned int $\ast$) pointer to integer, this value can be used as buyer's option\item[{\em 
buf}](char $\ast$) buffer for datablock. Memory will be allocated if this parameter is equal to null.\item[{\em 
size}](unsigned int $\ast$) pointer to integer in which the size of the received datablock is saved.\item[{\em 
maxsize}](unsigned int $\ast$) describes size of buf. This parameter will be ignored if bus is equal to null.\item[{\em 
term}](int) 0: do not terminate the buffer, 1: terminate the buffer by appending a 0x00.\end{description}
\end{Desc}
\begin{Desc}
\item[Returns: ]\par
(char $\ast$) pointer to buffer which contains the received datablock; NULL if fail; 1 if no data available. \end{Desc}
\index{block_receive.c@{block\_\-receive.c}!thread1@{thread1}}
\index{thread1@{thread1}!block_receive.c@{block\_\-receive.c}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void thread1 (struct {\bf LIBCOMMPTHREADS} $\ast$ {\em libcommpthreads})}\label{block__receive_8c_a0}


This is a part of {\bf block\_\-call}() {\rm (p.\,\pageref{block__receive_8c_a1})} and must not called from the user.

This function is the thread which is started from {\bf block\_\-call}() {\rm (p.\,\pageref{block__receive_8c_a1})} and runs in background.\begin{Desc}
\item[Parameters: ]\par
\begin{description}
\item[{\em 
libcommpthreads}](struct {\bf LIBCOMMPTHREADS} {\rm (p.\,\pageref{structLIBCOMMPTHREADS})} $\ast$) holds pointers to the functions to be call, fd (socket discriptor) and id. \end{description}
\end{Desc}
