PrevNextUpHome SophiaFramework UNIVERSE 5.3

17.6. Booting up the Callback by the Trigger

The callback function registered with the Fetch function will be booted up when the end of the storage is read or the stream buffer is full of data read from the storage.

On the other hand, the callback function registered with the Flush function will be booted up when all data of the stream buffer is written into the storage.

To boot up the callback function at the timimg other than the above, you have to use the SetTrigger function.

For instance, the method to boot up the callback function at the timing of reading "\r\n" (carriage return character) as the trigger in the TCP socket communication is as follows:

Example 17.28. Booting up the Callback Function with the SetTrigger Function

// error handling is omitted

// callback function notified of the connection result
XALLBACK_IMPLEMENT_SFXTCPSOCKET(MyClass, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        // get the input stream for receiving data from the socket
        // * the stream buffer is variable since the size argument is not specified
        if ((error = _socket.GetStreamReader(&_reader)) == SFERR_NO_ERROR) {

            // set the trigger to the timing when "\r\n"(carriage return character) is received
            if ((error = _reader.SetTrigger("\r\n", 2) == SFERR_NO_ERROR) {

                // perform fetch: receive data from the socket into the stream buffer actually
                // *1. the fetch result will be notified to the OnFetch function
                // *2. the stream buffer will be expanded automatically depending on the size of data to be received
                error = _reader.Fetch(XALLBACK_INTERNAL(OnFetch));
            }
        }
    }
    if (error != SFERR_NO_ERROR) { 

        // if an error occurs
        // release the input stream for receiving data from the socket
        _reader.Release(); 
        // close the socket
        _socket.Close();
    }
    return;
}

// callback function notified of the fetch result
XALLBACK_IMPLEMENT_SFXANSISTRINGSTREAMREADER(MyClass, OnFetch, error)
{
    SFXAnsiString string;

    if (error == SFERR_NO_ERROR) {

        // whether or not this callback function is notified of the trigger that is set using the SetTrigger function
        // (in this case, the triger is that the line feed and the carriage return characters appear)
        if (_reader.Triggers()) {  

            TRACE("This callback function has been triggered by reading CRLF.");
        }

        // read data from the stream buffer into the string variable
        if ((error = _reader.ReadSFXAnsiString(&string)) != SFERR_NO_ERROR) { 

            // dispaly the received data on BREW Output Window
            TRACE("--------");
            TRACE("%s", string.GetCString());
            TRACE("--------");

            // check whether or not data is still remaining
            if (!_reader.Ends()) { 

                // perform fetch: receive data from the socket into the stream buffer actually
                // *1. the fetch result will be notified to the OnFetch function
                // *2. the stream buffer will be expanded automatically depending on the size of data to be received
                error = _reader.Fetch(XALLBACK_INTERNAL(OnFetch)); 
            } else {

                // when all data have been received: 

                // release the input stream for receiving data from the socket
                _reader.Release();
                // close the socket
                _socket.Close();
            }
        } 
    }
    if (error != SFERR_NO_ERROR) { 

        // if an error occurs
        // release the input stream for receiving data from the socket
        _reader.Release(); 
        // close the socket
        _socket.Close();
    }
    return;
}
[Note] Note
In case of calling the SetTrigger function with only the size argument specified, the timing to read / write the specified length of data from / into the storage will become the trigger to boot up the callback function.