unit fn_tcp

#FujiNet interface TCP communication library.

author: bocianu bocianu@gmail.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.

Global Variables:

TCP_statusFN_StatusStructStatus operation result data. Structure defined in fn_sio module.
TCP_bytesWaitingwordNumber of bytes waiting in network device
TCP_bufferLengthwordNumber of bytes in receive buffer
TCP_dataFlagbyteThis byte is updated by SIO interrupt. 1 is set here if there is incoming data.



procedure TCP_AttachIRQ;

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).

    procedure TCP_DetachIRQ;

    Removes custom SIO interrupt handler. Do not call this procedure without TCP_AttachIRQ before.

      function TCP_Connect(var tcp_uri:PChar):byte;overload;

      Opens #FujiNet connection to remote host, at selected port using declared protocol.

      function TCP_Connect(var tcp_uri: PChar; aux1,aux2: byte):byte;overload;

      Opens #FujiNet connection to remote host, at selected port using declared protocol.

      function TCP_GetStatus:byte;

      Reads network device status and stores information in TCP_status variable.

        function TCP_SIORead:word;

        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.
          • (byte) - value from buffer

          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.
          • buf - pointer of buffer to store the incoming data
          • len - data length (in bytes)
          • returns:
          • (word) - number of bytes received.

          function TCP_CheckAndPoll:word;

          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.
          • (word) - number of bytes received.

          procedure TCP_SendString(var s:string);

          Sends string using existing connection.
          • s - the string to be sent

          procedure TCP_SendBuffer(buf: pointer;len: word);

          Sends data buffer using already opened connection.
          • buf - pointer to starting address of data
          • len - data length (in bytes)

          procedure TCP_Close;

          Closes #FujiNet network connection.

            function TCP_WaitForData(timeout:word):byte;

            Waits for declared time (in frames) for incoming data.
            • (byte) - return sioStatus for success, and $ff for timeout

            procedure TCP_ClearBuffer;

            Clears TCP data buffer.