PrevNextUpHome BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1
SFXTCPSocket
Class for the TCP socket communication.
#include <SFXTCPSocket.h.hpp>
class SFXTCPSocket : public SFXStorage;
SFMTYPEDEFCLASS(SFXTCPSocket)

Inheritance diagram

 Inheritance diagram of SFXTCPSocketClass

Collaboration diagram

 Collaboration diagram of SFXTCPSocketClass

Description

SFXTCPSocket class encapsulates BREW API ISocket interface and provides high level TCP socket communication functions.

How to use the TCP socket

  1. Create the TCP socket(instance of SFXTCPSocket class).
  2. Open the TCP socket using the SFXTCPSocket::Open function.
  3. Connect to the TCP server using the SFXTCPSocket::Connect function. Register the address and port number(instance of SFXSocketAddress class) to be connected to and the callback function that will receive the notification of the connection establishment. Domain name is resolved automatically in the SFXTCPSocket class.
  4. The callback function above will be called after notified of the connection establishment.
  5. How to send data: Get the output stream (instance of SFXBinaryStreamWriter or SFXAnsiStringStreamWriter or SFXWideStringStreamWriter class) using the SFXTCPSocket::GetStreamWriter function. Write data through the output stream. And then send data actually using the SFXStreamWriter::Flush function. In the SFXStreamWriter::Flush function, register the callback function that will receive the notification of sending data actually. The processing after sending data should be described in this callback function.
  6. How to receive data: Get the the input stream(instance of SFXBinaryStreamReader or SFXAnsiStringStreamReader or SFXWideStringStreamReader class) using the SFXTCPSocket::GetStreamReader. Receive data actually using the SFXStreamReader::Fetch function. In the SFXStreamReader::Fetch function, register the callback function that will receive the notification of receiving data actually. And then read data through the input stream in this callback function.
  7. Repeat the above step 5. or 6. until no other data needs to be sent or received.
  8. Finally, close the TCP socket using the SFXTCPSocket::Close function.

Example 495. Sample code for the TCP socket communication

// define _tcp, instance of SFXTCPSocket class, as member variable
class NetworkTime {
private:
    SFXTCPSocket _tcp;
    SFXBinaryStreamReader _reader;

public:
    Void Start(Void); // start to connect to NTP server
    Void Stop(Void);  // stop connecting to NTP server
    
    // callback function declaration
    CALLBACK_DECLARE_SFXTCPSOCKET(OnConnect)
    CALLBACK_DECLARE_SFXBINARYSTREAMREADER(OnFetch)
};

//  start to connect to NTP server
Void NetworkTime::Start(Void)
{
    SFCError error(SFERR_NO_ERROR);
    // set NTP server address
    SFXSocketAddress address("www.example.com:37");

    // initialize connection to NTP server
    if ((error = _tcp.Open()) == SFERR_NO_ERROR) {

        // connect to NTP server
        // connection establishment will be notified to OnConnect function 
        if ((error = _tcp.Connect(address,
                CALLBACK_FUNCTION(OnConnect))) == SFERR_NO_ERROR) {
            ...
        }
        else {
            // if error occurs
            _tcp.Close();
        }
    }
	
    if (error != SFERR_NO_ERROR) { 
        // if error occurs
        // error handling
        ...
    }
    return;
}

// stop connecting to NTP server
Void NetworkTime::Stop(Void)
{
    // release resource
    _reader.Release();
    _tcp.Close();
    return;
}

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXTCPSOCKET(NetworkTime, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        // get input stream for reading data
        if ((error = _tcp.GetStreamReader(64, &_reader)) == SFERR_NO_ERROR) {

            // receive data actually (completion of receiving data actually will be notified to OnFetch callback function )
            if ((error = _reader.Fetch(4, CALLBACK_FUNCTION(OnFetch)))
                == SFERR_NO_ERROR) {
                ...
            }
            if (error != SFERR_NO_ERROR) { 
                // if error occurs
                _reader.Release();
            }
        }
    }
	
    if (error != SFERR_NO_ERROR) {  
        // if error occurs
        _tcp.Close();
    }
    return;
}

// callback function notified of completion of receiving data actually
CALLBACK_IMPLEMENT_SFXBINARYSTREAMREADER(NetworkTime, OnFetch, error)
{
    SFXDate date; // date
    UInt32 time;

    if (error == SFERR_NO_ERROR) {

        // set to read data as Big Endian
        _reader.SetEndian(SFXBinaryStreamReader::ENDIAN_BIG);
        
        // read data as UInt32 type from input stream into time variable
        if ((error = _reader.ReadUInt32(&time)) == SFERR_NO_ERROR) {
            
            // set time (in seconds)
            date.Set(time);
            
            // addjust time since from January 1, 1900
            date -= SFXDateDuration::Offset19000101();
            
            // convert into local time
            date += SFXDateDuration::LocalTimeOffset();

            // format and out put
            TRACE("%s", date.Format("YYYY/MM/DD hh:mm:ss").GetCString());
        }
    }
	
    if (error != SFERR_NO_ERROR) {
        // if error occurs
        // error handling

        ...

    }

    // release resource
    _reader.Release();
    _tcp.Close();
    return;
}

Reference

SFXUDPSocket | SFXSSLSocket | Socket Connection

Member

Constructor/Destructor
SFXTCPSocket( Void )
Constructor of SFXTCPSocket class.
~SFXTCPSocket( Void )
Destructor of SFXTCPSocket class.
Public Functions
SFCError Accept( SFXTCPSocketPtr result )
Wait for connecting to the TCP server.
SFCError AsSFBAStream( SFBAStreamSmpPtr result )
Convert the instance of SFBSocket class internally managed into the instance of SFBAStream class.
SFCError AsSFBSource( SFBSourceSmpPtr result )
Convert the instance of SFBSocket class internally managed into the instance of SFBSource class.
SFCError Bind( SFXSocketAddressConstRef address )
Bind the local IP address and port number to the socket.
Void Cancel( Void )
Cancel the TCP socket communication.
Void Close( Void )
Close the TCP Socket.
SFCError Connect( SFXSocketAddressConstRef address , CallbackSPP spp , VoidPtr reference )
Connect to the TCP server.
SFCError GetLocalAddress( SFXSocketAddressPtr result )
Get the local IP address and port number.
SFCError GetRemoteAddress( SFXSocketAddressPtr result )
Get the remote IP address and port number.
SFBSocketSmpConstRef GetSFBSocket( Void )
Get the socket that is internally used.
SFCError GetStreamReader( UInt32 size , SFXStreamReaderPtr result )
GetStreamReader( SFXStreamReaderPtr result )
Get the input stream for reading data from the TCP socket.
SFCError GetStreamWriter( UInt32 size , SFXStreamWriterPtr result )
GetStreamWriter( SFXStreamWriterPtr result )
Get the output stream for writing data to the TCP socket.
SFCError Listen( SInt16 backlog = 1 )
Start to wait for the TCP connection.
SFCError Open( Void )
Open the TCP socket.
SFCError Read( VoidPtr buffer , UInt32Ptr size )
Receive data.
SFCError ScheduleAccept( CallbackSPP spp , VoidPtr reference )
Schedule to accept.
SFCError ScheduleBind( CallbackSPP spp , VoidPtr reference )
Schedule to bind.
SFCError ScheduleListen( CallbackSPP spp , VoidPtr reference )
Schedule to listen.
SFCError ScheduleRead( CallbackSPP spp , VoidPtr reference )
Schedule to receive data.
SFCError ScheduleWrite( CallbackSPP spp , VoidPtr reference )
Schedule to send data.
SFCError Write( VoidConstPtr buffer , UInt32Ptr size )
Send data.
Types
CallbackSPP (inherits from SFXStorage)
The prototype of the callback function for the SFXStorage class.

SFXTCPSocket::SFXTCPSocket
Constructor of SFXTCPSocket class.
[ public, explicit ]
SFXTCPSocket(Void);

Description

The instances of SFBNetMgr and SFBSocket classes are not created in the constructor.


SFXTCPSocket::~SFXTCPSocket
Destructor of SFXTCPSocket class.
[ public, virtual ]
virtual ~SFXTCPSocket(Void);

Description

The SFXTCPSocket::Close function is called in the destructor.

Reference

SFXTCPSocket::Close


SFXTCPSocket::Accept
Wait for connecting to the TCP server.
[ public, const ]
SFCError Accept(
    SFXTCPSocketPtr result   // pointer to TCP socket
);

Return value

  • Success : SFERR_NO_ERROR
  • If not in the Listen status : SFERR_INVALID_STATE
  • If argument is null, or in an invalid status : SFERR_INVALID_PARAM
  • If operation is blocked : AEE_NET_WOULDBLOCK
  • If other network error occurs: To get the error value, call the SFBSocket::GetLastError function.

Description

When the return value of the SFXTCPSocket::Accept function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleAccept function, and then schedule the SFXTCPSocket::Accept in the callback function again.

Reference

SFXTCPSocket::ScheduleAccept


SFXTCPSocket::AsSFBAStream
Convert the instance of SFBSocket class internally managed into the instance of SFBAStream class.
[ public, virtual, const ]
SFCError AsSFBAStream(
    SFBAStreamSmpPtr result   // pointer to the result SFBAStream class instance
);

Return value

  • Success : SFERR_NO_ERROR
  • Socket has been closed : SFERR_INVALID_STATE
  • Argument is null : SFERR_INVALID_PARAM

Reference

SFBSocket | SFBAStream


SFXTCPSocket::AsSFBSource
Convert the instance of SFBSocket class internally managed into the instance of SFBSource class.
[ public, virtual, const ]
SFCError AsSFBSource(
    SFBSourceSmpPtr result   // pointer to the result SFBSource class instance
);

Return value

  • Success : SFERR_NO_ERROR
  • If socket has been closed : SFERR_INVALID_STATE
  • If argument is null : SFERR_INVALID_PARAM
  • If failed to convert : SFERR_FAILED

Description

The SFXTCPSocket::AsSFBSource function creates and releases the instance of SFBSourceUtil class used internally.

Reference

SFBSocket | SFBSource


SFXTCPSocket::Bind
Bind the local IP address and port number to the socket.
[ public ]
SFCError Bind(
    SFXSocketAddressConstRef address   // IP address and port number
);

Return value

  • Success : SFERR_NO_ERROR
  • If socket has been closed : SFERR_INVALID_STATE
  • If insufficient memory : SFERR_NO_MEMORY
  • If operation is blocked : AEE_NET_WOULDBLOCK
  • If other network error occurs: To get the error value, call the SFBSocket::GetLastError function.

Description

When the return value of SFXTCPSocket::Bind function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleBind function, and then schedule the SFXTCPSocket::Bind function in the callback function again.

Reference

SFXTCPSocket::ScheduleBind


SFXTCPSocket::Cancel
Cancel the TCP socket communication.
[ public, virtual ]
Void Cancel(Void);

SFXTCPSocket::Close
Close the TCP Socket.
[ public ]
Void Close(Void);

Description

Cancel the TCP socket communication, and then the ISocket and INetMgr interfaces used internally are released.

When the BREW application is suspended, call the SFXTCPSocket::Close function to release the resources.


SFXTCPSocket::Connect
Connect to the TCP server.
[ public ]
SFCError Connect(
    SFXSocketAddressConstRef address   // domain (or IP address) and port number
    CallbackSPP spp                    // callback function
    VoidPtr reference                  // data passed to callback function
);

Argument

address

Specify domain (or IP address) and port number. The SFXTCPSocket class will automatically resolve the domain when needed.

spp

Specify the callback function notified of connection establishment.

reference

Specify the data passed to the callback function.

Return value

  • Success : SFERR_NO_ERROR
  • If socket is not opened or it has already been connected (including under processing) : SFERR_INVALID_STATE
  • If domain name is not found : SFERR_INVALID_STATE
  • If failed to create the instance of SFBNetMgr class : SFERR_FAILED
  • If insufficient memory : SFERR_NO_MEMORY
  • If other network error occurs: To get the error value, call the SFBSocket::GetLastError function.

Description

The connection establishment is notified to the callback function registered using the SFXTCPSocket::Connect function.

[Caution] Errors during the connection

The return value does not include the errors while connecting to the TCP server, which are notified to the callback function.

Example

// define _tcp, instance of SFXTCPSocket class, as member variable
class TcpSample {
private:
    SFXTCPSocket _tcp;
public:
    Void Start(Void);                       
    CALLBACK_DECLARE_SFXTCPSOCKET(OnConnect) 
};

// start to connect TCP server 
Void TcpSample::Start(Void)
{
    SFCError error(SFERR_NO_ERROR);

    // set address of TCP server
    SFXSocketAddress address("www.example.com:37");

    // open TCP socket 
    if ((error = _tcp.Open()) == SFERR_NO_ERROR) {
        
        // connect to TCP server
        if ((error = _tcp.Connect(address,CALLBACK_FUNCTION(OnConnect))) == SFERR_NO_ERROR) {
            ...
        }else {
            // if error occurs
            _tcp.Close();
        }
    }
	
    if (error != SFERR_NO_ERROR) { 
        // if error occurs
        // error handling
        ...
    }    
}

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {
        ...
    }
}

SFXTCPSocket::GetLocalAddress
Get the local IP address and port number.
[ public, const ]
SFCError GetLocalAddress(
    SFXSocketAddressPtr result   // pointer to the local IP address and port number
);

Return value

  • Success : SFERR_NO_ERROR
  • If socket is not opened or Bind has not been done : SFERR_INVALID_STATE
  • If argument is null : SFERR_INVALID_PARAM
  • If other network error occurs: To get the error value, call the SFBSocket::GetLastError function.

Description

[Note] Change of Local IP address

The local IP address might be changed between before and after the TCP socket connection.

Reference

SFXInetAddress::LocalInetAddress


SFXTCPSocket::GetRemoteAddress
Get the remote IP address and port number.
[ public, const ]
SFCError GetRemoteAddress(
    SFXSocketAddressPtr result   // pointer to the remote IP address and port number
);

Return value

  • Success : SFERR_NO_ERROR
  • If connection has not been established : SFERR_INVALID_STATE
  • If argument is null : SFERR_INVALID_PARAM
  • If other network error occurs: To get the error value, call the SFBSocket::GetLastError function.

Description

[Note] Change of Remote IP address

The remote IP address might be changed between before and after the TCP socket connection.


SFXTCPSocket::GetSFBSocket
Get the socket that is internally used.
[ public, const ]
SFBSocketSmpConstRef GetSFBSocket(Void);

Description

Get the socket (instance of SFBSocket class) that is used internally.

Reference

SFBSocket


SFXTCPSocket::GetStreamReader
Get the input stream for reading data from the TCP socket.
[ public, virtual ]
SFCError GetStreamReader(
    UInt32 size                 // buffer size
    SFXStreamReaderPtr result   // pointer to the stream
);
[ public, virtual ]
SFCError GetStreamReader(
    SFXStreamReaderPtr result   // pointer to the stream
);

Return value

  • Success : SFERR_NO_ERROR
  • If argument is null : SFERR_INVALID_PARAM
  • If connection has not been established : SFERR_INVALID_STATE
  • If insufficient memory: SFERR_NO_MEMORY

Description

The buffer size of the input stream can be specified. Use the SFXBinaryStreamReader, SFXAnsiStringStreamReader, or SFXWideStringStreamReader class for the input stream properly according to the type of data to read.

Example

Using SFXBinaryStreamReader.

// define _tcp, instance of SFXTCPSocket class, as member variable
class NetworkTime {
private:
    SFXTCPSocket _tcp;
    SFXBinaryStreamReader _reader;
public:
    // callback function declaration
    CALLBACK_DECLARE_SFXTCPSOCKET(OnConnect)
};

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXTCPSOCKET(NetworkTime, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        // get input stream for receiving data
        if ((error = _tcp.GetStreamReader(64, &_reader)) == SFERR_NO_ERROR) {
            ...
        }
    }
    return;
}

Reference

SFXTCPSocket::GetStreamWriter | SFXBinaryStreamReader | SFXAnsiStringStreamReader | SFXWideStringStreamReader


SFXTCPSocket::GetStreamWriter
Get the output stream for writing data to the TCP socket.
[ public, virtual ]
SFCError GetStreamWriter(
    UInt32 size                 // buffer size
    SFXStreamWriterPtr result   // pointer to the output stream
);
[ public, virtual ]
SFCError GetStreamWriter(
    SFXStreamWriterPtr result   // pointer to the output stream
);

Return value

  • Success : SFERR_NO_ERROR
  • If argument is null : SFERR_INVALID_PARAM
  • If connection has not been established : SFERR_INVALID_STATE
  • If insufficient memory: SFERR_NO_MEMORY

Description

The buffer size of the output stream can be specified. Use the SFXBinaryStreamWriter, SFXAnsiStringStreamWriter, or SFXWideStringStreamWriter class for the output stream properly according to the written data type

Example

Using SFXBinaryStreamwriter.

// define _tcp, instance of SFXTCPSocket class, as member variable
class NetworkTime {
private:
    SFXTCPSocket _tcp;
    SFXBinaryStreamwriter _writer;
public:
    // callback function declaration
    CALLBACK_DECLARE_SFXTCPSOCKET(OnConnect)
};
    
// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXTCPSOCKET(NetworkTime, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {

        // get output stream for sending data
        if ((error = _tcp.GetStreamWriter(64, &_writer)) == SFERR_NO_ERROR) {
            ...
        }
    }
    return;
}

Reference

SFXTCPSocket::GetStreamReader | SFXBinaryStreamWriter | SFXAnsiStringStreamWriter | SFXWideStringStreamWriter


SFXTCPSocket::Listen
Start to wait for the TCP connection.
[ public ]
SFCError Listen(
    SInt16 backlog = 1   // connection maximum number to be waited
);

Return value

  • Success : SFERR_NO_ERROR
  • If socket has been closed, or Bind has not been done : SFERR_INVALID_STATE
  • If Listen or ScheduleListen has already been done : SFERR_INVALID_STATE
  • If operation is blocked : AEE_NET_WOULDBLOCK
  • If other network error occurs: To get the error value, call the SFBSocket::GetLastError function.

Description

If the return value of the SFXTCPSocket::Listen function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleListen function, and then schedule the SFXTCPSocket::Listen function in the callback function again.

Reference

SFXTCPSocket::ScheduleListen


SFXTCPSocket::Open
Open the TCP socket.
[ public ]
SFCError Open(Void);

Return value

  • Success : SFERR_NO_ERROR
  • If socket has already been opened : SFERR_INVALID_STATE
  • If failed to create the instance of SFBNetMgr class : SFERR_FAILED
  • Otherwise : AEE_NET_ERROR

Description

Create the instance of SFBNetMgr class and open the TCP socket(instance of SFBSocket class).

Example

// define _tcp, instance of SFXTCPSocket class, as member variable
class TcpSample {
private:
    SFXTCPSocket _tcp;
public:
    Void Start(Void); 
    CALLBACK_DECLARE_SFXTCPSOCKET(OnConnect)
};

// start to connect TCP server
Void TcpSample::Start(Void)
{
    SFCError error(SFERR_NO_ERROR);

    // set address of TCP server 
    SFXSocketAddress address("www.example.com:37");

    // open TCP socket
    if ((error = _tcp.Open()) == SFERR_NO_ERROR) { 
        
        // connect to TCP server
        if ((error = _tcp.Connect(address,CALLBACK_FUNCTION(OnConnect))) == SFERR_NO_ERROR) { 
            ...
        }else {
            // if error occurs
            _tcp.Close();
        }
    }
	
    if (error != SFERR_NO_ERROR) {
        // if error occurs
        // error handling
        ...
    }    
}

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {
        ...
    }
}

Reference

SFBNetMgr | SFBSocket |


SFXTCPSocket::Read
Receive data.
[ public, virtual ]
SFCError Read(
    VoidPtr buffer   // buffer that stores the received data
    UInt32Ptr size   // buffer size
);

Argument

buffer

Specify the buffer to store the received data.

size

Specify the buffer size when calling the SFXTCPSocket::Read function. After the SFXTCPSocket::Read function is executed, the size of received data is stored.

Return value

  • Success : SFERR_NO_ERROR
  • If connection has not been established : SFERR_INVALID_STATE
  • If size is null : SFERR_INVALID_PARAM
  • If necessary to retry : AEE_NET_WOULDBLOCK
  • If impossible to receive : AEE_NET_ERROR

Description

To receive data without using the input stream, use the SFXTCPSocket::Read function.

When the return value of SFXTCPSocket::Read function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleRead function, and then schedule the SFXTCPSocket::Read function in the callback function again.

Example

// define _tcp, instance of SFXTCPSocket class, as member variable   
class TcpSample {
private:
    SFXTCPSocket _tcp;
public:
    // callback function declaration
    CALLBACK_DECLARE_SFXTCPSOCKET(OnWrite)  
    CALLBACK_DECLARE_SFXTCPSOCKET(OnRead)  
};

// callback function notified of sending data
CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error)
{
    UInt32 size;
    static ACharConst data[] = "GET / HTTP/1.0\n\n";
    
    // write data
    size = sizeof(data) - 1;
    switch (error = _tcp.Write(data, &size)) {
        case SFERR_NO_ERROR:
             // schedule to receive data
            _tcp.ScheduleRead(CALLBACK_FUNCTION(OnRead)); // register callback function
            break;
        case AEE_NET_WOULDBLOCK:
            // schedule to send data 
            _tcp.ScheduleWrite(CALLBACK_FUNCTION(OnWrite)); // register callback function
            break;
    }
}

// callback function notified of completion of receiving data actually
CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnRead, error)
{
    SFXBuffer buffer;
    UInt32 size;

    // read data
    buffer.SetSize(32);
    size = buffer.GetSize();
    switch (_tcp.Read(buffer.GetBuffer(), &size)) {
        case SFERR_NO_ERROR:

           // check whether data of specified size is read or not
           if (size == buffer.GetSize()) {

                // display received data
                buffer.SetSize(buffer.GetSize() + 1);
                buffer[buffer.GetSize() - 1] = '\0';
                TRACE("%s", SFXAnsiString(buffer).GetCString());
                
               // close connection
                _tcp.Close();
            }
            else {
                ...
            }
            break;

        case AEE_NET_WOULDBLOCK:
            // schedule to receive data 
            _tcp.ScheduleRead(CALLBACK_FUNCTION(OnRead)); // register callback function
            break;
    }
    return;
}

Reference

SFXTCPSocket::ScheduleRead


SFXTCPSocket::ScheduleAccept
Schedule to accept.
[ public ]
SFCError ScheduleAccept(
    CallbackSPP spp     // callback function
    VoidPtr reference   // data passed to callback function
);

Return value

  • Success : SFERR_NO_ERROR
  • If not in the Listen status, or the callback has already been registered : SFERR_INVALID_STATE

Description

The SFXTCPSocket::ScheduleAccept function is used to register the callback function in which the SFXTCPSocket::Accept function is used to wait for connecting to the TCP server.

[Note] Note
When the return value of SFXTCPSocket::Accept function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleAccept function, and then schedule the SFXTCPSocket::Accept function in the callback function again.

Reference

SFXTCPSocket::Accept


SFXTCPSocket::ScheduleBind
Schedule to bind.
[ public ]
SFCError ScheduleBind(
    CallbackSPP spp     // callback function
    VoidPtr reference   // data passed to callback function
);

Return value

  • Success : SFERR_NO_ERROR
  • If socket has been closed, or the callback function has already been registered : SFERR_INVALID_STATE

Description

The SFXTCPSocket::ScheduleBind function is used to register the callback function in which Bind is done using the SFXTCPSocket::Bind function.

[Note] Note
When the return value of SFXTCPSocket::Bind function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleBind function, and then schedule the SFXTCPSocket::Bind function in the callback function again.

Reference

SFXTCPSocket::Bind


SFXTCPSocket::ScheduleListen
Schedule to listen.
[ public ]
SFCError ScheduleListen(
    CallbackSPP spp     // callback function
    VoidPtr reference   // data passed to callback function
);

Return value

  • Success : SFERR_NO_ERROR
  • If socket has been closed, or Bind has not been done : SFERR_INVALID_STATE
  • If Bind has not been done, or the callback function has already been registered : SFERR_INVALID_STATE

Description

The SFXTCPSocket::ScheduleListen function is used to register the callback function in which the SFXTCPSocket::Listen function is used to wait(Listen) for the TCP connection.

[Note] Note
When the return value of SFXTCPSocket::Listen function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleListen, and then schedule the SFXTCPSocket::Listen function in the callback function again.

Reference

SFXTCPSocket::Listen


SFXTCPSocket::ScheduleRead
Schedule to receive data.
[ public, virtual ]
SFCError ScheduleRead(
    CallbackSPP spp     // callback function
    VoidPtr reference   // data passed to callback function
);

Return value

  • Success : SFERR_NO_ERROR
  • If connection has not been established, or the callback has already been registered : SFERR_INVALID_STATE

Description

The SFXTCPSocket::ScheduleRead function is used to register the callback function in which the SFXTCPSocket::Read function is used to receive data.

[Note] Note
When the return value of SFXTCPSocket::Read function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleRead function, and then schedule the SFXTCPSocket::Read function in the callback function again.

Example

// define _tcp, instance of SFXTCPSocket class, as member variable   
class TcpSample {
private:
    SFXTCPSocket _tcp;
public:
    // callback function declaration
    CALLBACK_DECLARE_SFXTCPSOCKET(OnWrite)  
    CALLBACK_DECLARE_SFXTCPSOCKET(OnRead)  
};

// callback function notified of completion of sending data

CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error)
{
    UInt32 size;
    static ACharConst data[] = "GET / HTTP/1.0\n\n";
    
    // send data
    size = sizeof(data) - 1;
    switch (error = _tcp.Write(data, &size)) {
        case SFERR_NO_ERROR:
            // schedule to receive data 
            _tcp.ScheduleRead(CALLBACK_FUNCTION(OnRead)); // register callback function
            break;
        case AEE_NET_WOULDBLOCK:
            // schedule to send data 
            _tcp.ScheduleWrite(CALLBACK_FUNCTION(OnWrite)); // register callback function
            break;
    }
}

//callback function notified of completion of receiving data actually
CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnRead, error)
{
    SFXBuffer buffer;
    UInt32 size;

    // read data
    buffer.SetSize(32);
    size = buffer.GetSize();
    switch (_tcp.Read(buffer.GetBuffer(), &size)) {
        case SFERR_NO_ERROR:

           // check whether data with specified size is received or not
           if (size == buffer.GetSize()) {

                // display received data
                buffer.SetSize(buffer.GetSize() + 1);
                buffer[buffer.GetSize() - 1] = '\0';
                TRACE("%s", SFXAnsiString(buffer).GetCString());
                
                _tcp.Close();  // close TCP socket
            }
            else {
                ...
            }
            break;

        case AEE_NET_WOULDBLOCK:
            // schedule to receive data 
            _tcp.ScheduleRead(CALLBACK_FUNCTION(OnRead)); // register callback function
            break;
    }
    return;
}

Reference

SFXTCPSocket::Read


SFXTCPSocket::ScheduleWrite
Schedule to send data.
[ public, virtual ]
SFCError ScheduleWrite(
    CallbackSPP spp     // callback function
    VoidPtr reference   // pointer to callback function
);

Return value

  • Success : SFERR_NO_ERROR
  • If connection has not been established, or the callback function has already been registered : SFERR_INVALID_STATE

Description

The SFXTCPSocket::ScheduleWrite function is used to register the callback function in which the SFXTCPSocket::Write function is used to send data.

[Note] Note
When the return value of SFXTCPSocket::Write function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleWrite function, and then schedule the SFXTCPSocket::Write function in the callback function again.

Example

 // define _tcp, instance of SFXTCPSocket class, as member variable   
class TcpSample {
private:
    SFXTCPSocket _tcp;
public:
    // callback function declaration
    CALLBACK_DECLARE_SFXTCPSOCKET(OnConnect)  
    CALLBACK_DECLARE_SFXTCPSOCKET(OnWrite)  
};

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {
       _tcp.ScheduleWrite(CALLBACK_FUNCTION(OnWrite));
    }
}

// callback function notified of completion of sending data

CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error)
{
    UInt32 size;
    static ACharConst data[] = "GET / HTTP/1.0\n\n";
    
    // send data
    size = sizeof(data) - 1;
    switch (error = _tcp.Write(data, &size)) {
        case SFERR_NO_ERROR:
            ...
            break;
        case AEE_NET_WOULDBLOCK:
            // schedule to send data 
            _tcp.ScheduleWrite(CALLBACK_FUNCTION(OnWrite)); // register callback function
            break;
    }
}

Reference

SFXTCPSocket::Write


SFXTCPSocket::Write
Send data.
[ public, virtual ]
SFCError Write(
    VoidConstPtr buffer   // data to write
    UInt32Ptr size        // size of data to write
);

Argument

buffer

Data to write.

size

Specify the buffer size when calling the SFXTCPSocket::Write function. After the SFXTCPSocket::Write function is executed, the size of actually written data is stored.

Return value

  • Success : SFERR_NO_ERROR
  • If connection has not been established : SFERR_INVALID_STATE
  • If size is null : SFERR_INVALID_PARAM
  • If necessary to retry : AEE_NET_WOULDBLOCK
  • If impossible to receive : AEE_NET_ERROR

Description

To write data without using the output stream, use the SFXTCPSocket::Write function.

When the return value of SFXTCPSocket::Write function is AEE_NET_WOULDBLOCK, register the callback function using the SFXTCPSocket::ScheduleWrite function, and then call the SFXTCPSocket::Write function in the callback function again.

Example

 // define _tcp, instance of SFXTCPSocket class, as member variable   
class TcpSample {
private:
    SFXTCPSocket _tcp;
public:
    // callback function declaration
    CALLBACK_DECLARE_SFXTCPSOCKET(OnConnect)  
    CALLBACK_DECLARE_SFXTCPSOCKET(OnWrite)  
};

// callback function notified of connection establishment
CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error)
{
    if (error == SFERR_NO_ERROR) {
        _tcp.ScheduleWrite(CALLBACK_FUNCTION(OnWrite));
    }
}

// callback function notified of completion of sending data

CALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error)
{
    UInt32 size;
    static ACharConst data[] = "GET / HTTP/1.0\n\n";
    
    // write data
    size = sizeof(data) - 1;
    switch (error = _tcp.Write(data, &size)) {
        case SFERR_NO_ERROR:
            ...
            break;
        case AEE_NET_WOULDBLOCK:
            // schedule to send data 
            _tcp.ScheduleWrite(CALLBACK_FUNCTION(OnWrite)); // register callback function
            break;
    }
}

Reference

SFXTCPSocket::ScheduleWrite