author: bocianu email@example.com
This library provides an easy interface to estabilish TCP connection, and transfer data both directions.
It allows you to use SIO interrupts, to read data from SIO only on device request.
It uses 256 bytes circular buffer located in your MadPascal DATA block.
This library is a part of 'blibs' - set of custom Mad-Pascal libraries.
|TCP_status||FN_StatusStruct||Status operation result data. Structure defined in fn_sio module.|
|TCP_bytesWaiting||word||Number of bytes waiting in network device|
|TCP_bufferLength||word||Number of bytes in receive buffer|
|TCP_dataFlag||byte||This byte is updated by SIO interrupt. 1 is set here if there is incoming data.|
Attaches interrupt handler, to update TCP_dataFlag byte on incoming data connection.
This interrupt sets 1 into TCP_dataFlag variable. If you want to be informed on next packet, you need to reset it to zero manualy (probably after data fetch).
Removes custom SIO interrupt handler. Do not call this procedure without TCP_AttachIRQ before.
function TCP_Connect(var tcp_uri:PChar):byte;
Opens #FujiNet connection to remote host, at selected port using declared protocol.
Reads network device status and stores information in TCP_status variable.
Performs data fetch from SIO device (N:) to free space in local TCP buffer.
If amount of SIO data is bigger than available space in buffer, only part of the data is received. To fetch rest of it you need to release some buffer space, by calling TCP_ReadByte or TCP_ReadBuffer procedure. And then call TCP_SIORead again... and again...
function TCP_ReadByte: byte;
Reads one byte from TCP receive buffer (if available). It also frees one byte in buffer for future SIOReads.
Always check if there is at least one byte available in buffer (TCP_bufferLength > 0). If you will call this function on empty buffer, returned value is unpredictable.
function TCP_ReadBuffer(buf: pointer; len: word): word;
Reads block of bytes from TCP receive buffer. It also frees space in buffer for future SIOReads.
If amount of data available in buffer is smaller then desired (len), only available part is received. Function will return exact number of received/freed bytes.
This function performs check if there is any incoming data available. If there is and we have free space in receive buffer, it reads biggest possible chunk of data from SIO to our TCP buffer. After operation it returns amount of data added to buffer, and updates value of TCP_bufferLength.
Also TCP_dataFlag is set to 0 after every succesful poll.
This function should be called periodicaly to retrieve incoming data, and process it when it shows up in buffer.
procedure TCP_SendString(var s:string);
Sends string using existing connection.
procedure TCP_SendBuffer(buf: pointer;len: word);
Sends data buffer using already opened connection.
Closes #FujiNet network connection.
Waits for declared time (in frames) for incoming data.
Clears TCP data buffer.