SophiaFramework UNIVERSE 5.3 |
The SFXTCPSocket class provides functions to implement both of the TCP client and the TCP server.
In this class, it is possible to send and receive data using the stream.
In case the stream is not used, data will be sent / received using the SFXTCPSocket::Write / SFXTCPSocket::Read function.
Note | |
---|---|
The SFXTCPSocket class encapsulates BREW API ISocket interface to provide high level functions for the TCP socket communication. |
How to implement the TCP client
The TCP client can be implemented by taking the following procedures with the SFXTCPSocket class.
Example 845. Implementation of the TCP Client
// get the time from the NTP server // The _socket variable is defined as class member variable since used in the callback function class NetworkTime { private: SFXTCPSocket _socket; SFXBinaryStreamReader _reader; SFXTimer _timer; public: Void Start(Void); // start to connect to the NTP server Void Stop(Void); // stop connecting to the NTP server // declare callback function XALLBACK_DECLARE_SFXTCPSOCKET(OnConnect) XALLBACK_DECLARE_SFXBINARYSTREAMREADER(OnFetch) XALLBACK_DECLARE_SFXTIMER(OnTimer) }; #define NTP_SERVER ("www.example.com:37") #define TIMER_INTERVAL (5000) // start to connect to the NTP server Void NetworkTime::Start(Void) { SFCError error(SFERR_NO_ERROR); // set the NTP server address SFXSocketAddress address(NTP_SERVER); // set the timer callback function _timer.Set(XALLBACK_INTERNAL(OnTimer)); // open socket if ((error = _socket.Open()) == SFERR_NO_ERROR) { // connect to the NTP server // * the connection result will be notified to the OnConnect function if ((error = _socket.Connect(address, XALLBACK_INTERNAL(OnConnect))) == SFERR_NO_ERROR) { TRACE("connecting..."); } else { // if an error occurs, close TCP socket _socket.Close(); } } if (error != SFERR_NO_ERROR) { // if an error occurs // error handling ... // set the timer. after the TIMER_INTERVAL milliseconds, the OnTimer function will be called _timer.Schedule(TIMER_INTERVAL); } return; } // stop connecting to the NTP server Void NetworkTime::Stop(Void) { // finalization _reader.Release(); _socket.Close(); return; } // callback function notified of the connection result XALLBACK_IMPLEMENT_SFXTCPSOCKET(NetworkTime, OnConnect, error) { if (error == SFERR_NO_ERROR) { // get input stream for receiving data from TCP socket if ((error = _socket.GetStreamReader(64, &_reader)) == SFERR_NO_ERROR) { // perform fetch: receive 4 bytes of data from TCP socket into to input stream buffer actually // * the fetch result will be notified to the OnFetch function if ((error = _reader.Fetch(4, XALLBACK_INTERNAL(OnFetch))) == SFERR_NO_ERROR) { TRACE("fetching..."); } if (error != SFERR_NO_ERROR) { // if an error occurs // release resource _reader.Release(); } } } if (error != SFERR_NO_ERROR) { // if an error occurs _socket.Close(); } return; } // callback function notified of the fetch result XALLBACK_IMPLEMENT_SFXBINARYSTREAMREADER(NetworkTime, OnFetch, error) { SFXDate date; // date class UInt32 time; if (error == SFERR_NO_ERROR) { // read data as big endian _reader.SetEndian(SFXBinaryStreamReader::ENDIAN_BIG); // read data as the UInt32 type from input stream buffer into the time variable if ((error = _reader.ReadUInt32(&time)) == SFERR_NO_ERROR) { // set the SFXDate instance date.Set(time); // adjust time from January 1, 1900 date -= SFXDateDuration::Offset19000101(); // convert time into local time date += SFXDateDuration::LocalTimeOffset(); // output time in specified format TRACE("%s", date.Format("YYYY/MM/DD hh:mm:ss").GetCString()); } } if (error != SFERR_NO_ERROR) { // if an error occurs // error handling ... } // finalization _reader.Release(); _socket.Close(); return; } // timer callback function XALLBACK_IMPLEMENT_SFXTIMER(NetworkTime, OnTimer) { Start(); return; }
Note | |
---|---|
For more details on the above code, see the networktime applet in the Example/networktime folder of the SophiaFramework UNIVERSE package, which supports suspend and resume functions. |
How to implement the TCP server
The TCP server can be implemented by taking the following procedures with the SFXTCPSocket class.
SFXUDPSocket | SFXSSLSocket | SFXSocketAddress | TCP Socket Communication | BREW API ISocket
Constructor/Destructor |
---|
SFXTCPSocket( Void ) Constructor of the SFXTCPSocket class.
|
~SFXTCPSocket( Void ) Destructor of the SFXTCPSocket class.
|
Public Functions | |
---|---|
SFCError |
Accept(
SFXTCPSocketPtr result
) Accept(Or get one from the connection request queue of this TCP socket in order to connect to the TCP client).
|
SFCError |
AsSFBAStream(
SFBAStreamSmpPtr result
) Convert the SFBSocket instance internally managed by this SFXTCPSocket storage into the SFBAStream instance.
|
SFCError |
AsSFBSource(
SFBSourceSmpPtr result
) Convert the SFBSocket instance internally managed by this SFXTCPSocket storage into the SFBSource instance.
|
SFCError |
Bind(
SFXSocketAddressConstRef address
) Bind(Or bind the specified local IP address and port number to this socket.
|
Void |
Cancel( Void ) Cancel this TCP socket communication.
|
Void |
Close( Void ) Close this TCP socket.
|
SFCError |
Connect(
SFXSocketAddressConstRef address
, CallbackSPP spp
, VoidPtr reference
) Send the connection request to the specified server in order to establish the connection.
|
SFCError |
GetLocalAddress(
SFXSocketAddressPtr result
) Get the local IP address and port number of this TCP socket.
|
SFCError |
GetRemoteAddress(
SFXSocketAddressPtr result
) Get the remote IP address and port number of the peer of this TCP socket.
|
SFBNetMgrSmpConstRef |
GetSFBNetMgr( Void ) Get the SFBNetMgr instance managed internally by this socket.
|
SFBSocketSmpConstRef |
GetSFBSocket( Void ) Get the SFBSocket instance managed internally by this socket.
|
SFCError |
GetStreamReader(
UInt32 size
, SFXStreamReaderPtr result
) Get the input stream for reading data from this TCP socket.
|
SFCError |
GetStreamReader(
SFXStreamReaderPtr result
) Get the input stream for reading data from this TCP socket.
|
SFCError |
GetStreamWriter(
UInt32 size
, SFXStreamWriterPtr result
) Get the output stream for writing data onto this TCP socket.
|
SFCError |
GetStreamWriter(
SFXStreamWriterPtr result
) Get the output stream for writing data onto this TCP socket.
|
SFCError |
Listen(
SInt16 backlog = 1
) Listen(Or start to wait for the connection request from the TCP client).
|
SFCError |
Open(
SInt32 linger = -1
) Open this TCP socket.
|
SFCError |
Read(
VoidPtr buffer
, UInt32Ptr size
) Read data from this TCP socket without input stream.
|
SFCError |
ScheduleAccept(
CallbackSPP spp
, VoidPtr reference
) Schedule to accept(or get one from the connection request queue of this TCP socket in order to connect to the TCP client).
|
SFCError |
ScheduleBind(
CallbackSPP spp
, VoidPtr reference
) Schedule to bind the local IP address and port number to this TCP socket.
|
SFCError |
ScheduleListen(
CallbackSPP spp
, VoidPtr reference
) Schedule to listen.
|
SFCError |
ScheduleRead(
CallbackSPP spp
, VoidPtr reference
) Schedule to read data from this TCP socket without input stream.
|
SFCError |
ScheduleWrite(
CallbackSPP spp
, VoidPtr reference
) Schedule to write data into this TCP socket without output stream.
|
SFCError |
Write(
VoidConstPtr buffer
, UInt32Ptr size
) Write data into this TCP socket without output stream.
|
Types |
---|
CallbackSPP
(inherits from SFXStorage)
Type of the callback function for the Storage class.
|
[ public, explicit ] SFXTCPSocket(Void);
This constructor does nothing.
[ public, virtual ] virtual ~SFXTCPSocket(Void);
This destructor calls the SFXTCPSocket::Close function.
Note | |
---|---|
Registered callback functions will be canceled. |
[ public, const ] SFCError Accept( SFXTCPSocketPtr result // pointer to the new socket for communicating with the client );
This function accepts(or gets one from the connection request queue of this TCP socket in order to connect to the TCP client).
This function gets one from the connection request queue of this TCP socket in order to connect to the TCP client, and creates the new connected TCP socket into the result argument. Using this socket, the communication with the TCP client will be performed.
* If this function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with the SFXTCPSocket::ScheduleAccept function where accepting will be performed with this function.
Note | |
---|---|
This function internally calls the SFBSocket::Accept function. |
Prerequisite | |
---|---|
This socket needs to be under listening with the SFXTCPSocket::Listen function before this function is called. |
SFXTCPSocket::ScheduleAccept | SFXTCPSocket::Listen | SFXTCPSocket::Bind | SFBSocket::Accept | BREW API ISOCKET_Accept | BREW API ISOCKET_GetLastError
[ public, virtual, const ] SFCError AsSFBAStream( SFBAStreamSmpPtr result // pointer to the SFBAStream instance );
This function converts the SFBSocket instance internally managed by this SFXTCPSocket storage into the SFBAStream instance.
As a result, a pointer to the SFBAStream instance will be returned into the result argument.
Note | |
---|---|
The contents of the SFXTCPSocket storage can be handled through the SFBAStream instance. |
[ public, virtual, const ] SFCError AsSFBSource( SFBSourceSmpPtr result // pointer to the SFBSource instance );
This function converts the SFBSocket instance internally managed by this SFXTCPSocket storage into the SFBSource instance.
As a result, a pointer to the SFBSource instance will be returned into the result argument.
Note | |
---|---|
This function internally calls the BREW API ISOURCEUTIL_SourceFromSocket function. |
Note | |
---|---|
The contents of the SFXTCPSocket storage can be handled as the SFBSource instance. |
SFBSocket | SFBSource | BREW API ISOURCEUTIL_SourceFromSocket
[ public ] SFCError Bind( SFXSocketAddressConstRef address // local IP address and port number: Either SFXInetAddress::LoopbackInetAddress() or SFXInetAddress.AnyInetAddress() can be specified as local IP address );
This function binds(or binds the specified local IP address and port number to this TCP socket).
In case of loopback communication within the device only, specify SFXInetAddress::LoopbackInetAddress() as the local IP address. Otherwise, i.e., when communicating with an external server, specify SFXInetAddress::AnyInetAddress(). For the limitation on BREW SDK, values other than these two are not supported.
* If this function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with the SFXTCPSocket::ScheduleBind function where binding will be performed with this function.
Note | |
---|---|
In the client side, you don't necessarily have to bind. If binding is omitted, default port number will be bound to this TCP socket. |
Note | |
---|---|
This function internally calls the SFBSocket::Bind function. |
Prerequisite | |
---|---|
This socket needs to be opened with the SFXTCPSocket::Open function before this function is called. |
SFXTCPSocket::Open | SFXTCPSocket::Listen | SFXTCPSocket::Accept | SFXTCPSocket::ScheduleBind | SFXInetAddress::LoopbackInetAddress | SFXInetAddress::AnyInetAddress | SFXSocketAddress | SFBSocket::Bind | BREW API ISOCKET_Bind | BREW API ISOCKET_GetLastError | BREW API AEE_BREW_LOOPBACK | BREW API AEE_INADDR_LOOPBACK | BREW API AEE_INADDR_ANY
[ public, virtual ] Void Cancel(Void);
This function cancels various operations on this TCP socket.
Concretely, the registered callback function for each operation will be canceled and the status will be returned to before that operation is performed.
The operations to be canceled and its details are in the following table:
Table 198. Operations to be canceled and its details
Operations to be canceled | Details |
---|---|
Connecting to the server with the SFXTCPSocket::Connect function. | Cancel the callback function and restore the status to just after the SFXTCPSocket::Open function is executed. |
Scheduling with the SFXTCPSocket::ScheduleBind function. | Cancel the callback function and restore the status to just after the SFXTCPSocket::Open function is executed. |
Scheduling with the SFXTCPSocket::ScheduleListen function. | Cancel the callback function and restore the status to just after the SFXTCPSocket::Bind function is executed. |
Scheduling with the SFXTCPSocket::ScheduleAccept function. | Cancel the callback function and restore the status to just after the SFXTCPSocket::Listen function is executed. |
Scheduling with the SFXTCPSocket::ScheduleRead function. | Cancel the callback function and restore the status to just after the SFXTCPSocket::Connect function is executed. |
Scheduling with the SFXTCPSocket::ScheduleWrite function. | Cancel the callback function and restore the status to just after the SFXTCPSocket::Connect function is executed. |
Note | |
---|---|
This function is called in the SFXTCPSocket::Close function. |
SFXTCPSocket::Open | SFXTCPSocket::Connect | SFXTCPSocket::ScheduleBind | SFXTCPSocket::ScheduleListen | SFXTCPSocket::ScheduleAccept | SFXTCPSocket::ScheduleRead | SFXTCPSocket::ScheduleWrite | SFXTCPSocket::Close
[ public ] Void Close(Void);
This function closes this TCP socket.
Concretely, this function calls the SFXTCPSocket::Cancel function and releases the SFBNetMgr and SFBSocket instances that this TCP socket internally manages.
Note | |
---|---|
The registered callback functions will be canceled. |
Note | |
---|---|
This function is called in the SFXTCPSocket::~SFXTCPSocket destructor. |
Tip | |
---|---|
When suspending, resources should be released by calling this function. |
SFXTCPSocket::Open | SFXTCPSocket::Cancel | SFXTCPSocket::~SFXTCPSocket | SFBNetMgr | SFBSocket | BREW API INetMgr | BREW API ISocket
[ public ] SFCError Connect( SFXSocketAddressConstRef address // domain (or IP address) and port number of the server CallbackSPP spp // callback function that the the connection result will be notified to VoidPtr reference // data passed to callback function );
Specify the domain (or IP address) and port number of the server. The domain will be automatically resolved, if necessary.
Specify the callback function notified of the connection result.
Specify the data passed to the callback function.
This function sends the connection request to the specified server in order to establish the connection. After the connection is established, communication with the server will be performed using this TCP socket.
This function can not gets the result (error code) of the connection establishment as the return value. The the connection result will be notified to the callback function specified in the first argument as follows:
Until the callback function is called, the state of this TCP socket is "Under connecting". When the callback function is called, it will be "Connected" if the connection is established. Otherwise, it will be restored to "Opened" just before this function is called.
And, if the SFXTCPSocket::Cancel function is called before the result of the connection establishment is notified to the callback function, the connection establishment will be canceled and the state of this TCP socket will be restored to "Opened" just before this function is called.
* If this function returns the value other than SFERR_NO_ERROR, the specified callback function will not be called.
Note | |
---|---|
This function internally calls the SFBSocket::Connect function. |
Prerequisite | |
---|---|
This socket needs to be opend by calling the SFXTCPSocket::Open function. |
// define _socket, SFXTCPSocket instance, as member variable class TcpSample { private: SFXTCPSocket _socket; public: Void Start(Void); XALLBACK_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 = _socket.Open()) == SFERR_NO_ERROR) { // connect to TCP server // the connection result will be notified to the OnConnect function if ((error = _socket.Connect(address,XALLBACK_INTERNAL(OnConnect))) == SFERR_NO_ERROR) { ... }else { // if an error occurs _socket.Close(); } } if (error != SFERR_NO_ERROR) { // if an error occurs // error handling ... } } // callback function notified of the connection result XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error) { if (error == SFERR_NO_ERROR) { ... } }
SFXTCPSocket::Open | SFXTCPSocket::Cancel | SFBSocket::Connect | SFXSocketAddress | SFBNetMgr | SFXStorage::CallbackSPP | BREW API INetMgr | BREW API ISOCKET_Connect | BREW API ISOCKET_GetLastError | BREW API Network AEE error codes
[ public, const ] SFCError GetLocalAddress( SFXSocketAddressPtr result // pointer to the local IP address and port number of this TCP socket );
This function gets the local IP address and port number of this TCP socket.
The actual physical IP address will be obtained from the socket for sending and receiving data after the connection is established. Until then, the value set with the SFXTCPSocket::Bind function will be return.
Tip | |
---|---|
The actual physical IP address can be also obtained by calling the SFXInetAddress::LocalInetAddress function. |
Prerequisite | |
---|---|
To get the local IP address and port number, this socket needs to be bound with the SFXTCPSocket::Bind function or connected with the SFXTCPSocket::Connect function. |
SFXTCPSocket::Bind | SFXTCPSocket::Connect | SFXInetAddress::LocalInetAddress | SFXSocketAddress | BREW API ISOCKET_GetLastError
[ public, const ] SFCError GetRemoteAddress( SFXSocketAddressPtr result // pointer to the remote IP address and port number of the peer of this TCP socket );
This function gets the remote IP address and port number of the peer of this TCP socket.
Note | |
---|---|
This function internally calls the SFBSocket::GetPeerName function. |
Prerequisite | |
---|---|
To get the remote IP address and port number, this socket needs to be connected one returned by the SFXTCPSocket::Accept function or connected with the SFXTCPSocket::Connect function. |
SFBSocket::GetPeerName | SFXTCPSocket::Accept | SFXTCPSocket::Connect | SFXSocketAddress | BREW API ISOCKET_GetPeerName | BREW API ISOCKET_GetLastError
[ public, const ] SFBNetMgrSmpConstRef GetSFBNetMgr(Void);
SFBNetMgr instance managed internally by this socket.
This function gets the SFBNetMgr instance managed internally by this socket.
[ public, const ] SFBSocketSmpConstRef GetSFBSocket(Void);
SFBSocket instance managed internally by this socket.
This function gets the SFBSocket instance managed internally by this 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 );
This function gets the input stream for reading data from this TCP socket.
If the size argument is specified, the buffer size of the input stream will be fixed with the specified value. Otherwise, the buffer size will be variable and the SFXElasticStreamReader class will be used internally.
Tip | |
---|---|
You have to use the SFXBinaryStreamReader, SFXAnsiStringStreamReader, or SFXWideStringStreamReader class for the input stream properly depending on the type of data to be read. |
Prerequisite | |
---|---|
To get the input stream, this socket needs to be connected one returned by the SFXTCPSocket::Accept function or connected with the SFXTCPSocket::Connect function. |
The code to get the input stream for reading data from the TCP socket using the SFXBinaryStreamReader class is as follows:
// define _socket, the SFXTCPSocket instance, as the member variable class NetworkTime { private: SFXTCPSocket _socket; SFXBinaryStreamReader _reader; public: // declare the callback function XALLBACK_DECLARE_SFXTCPSOCKET(OnConnect) }; // callback function notified of the connection result XALLBACK_IMPLEMENT_SFXTCPSOCKET(NetworkTime, OnConnect, error) { if (error == SFERR_NO_ERROR) { // get the input stream for reading data from the TCP socket if ((error = _socket.GetStreamReader(64, &_reader)) == SFERR_NO_ERROR) { ... } } return; }
SFXTCPSocket::GetStreamWriter | SFXBinaryStreamReader | SFXAnsiStringStreamReader | SFXWideStringStreamReader SFXTCPSocket::Accept | SFXTCPSocket::Connect | Stream Buffer
[ 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 );
This function gets the output stream for writing data onto this TCP socket.
If the size argument is specified, the buffer size of the output stream will be fixed with the specified value. Otherwise, the buffer size will be variable and the SFXElasticStreamWriter class will be used internally.
Tip | |
---|---|
You have to use the SFXBinaryStreamWriter, SFXAnsiStringStreamWriter, or SFXWideStringStreamWriter class for the output stream properly depending on the type of data to write. |
Prerequisite | |
---|---|
To get the output stream, this socket needs to be connected one returned by the SFXTCPSocket::Accept function or connected with the SFXTCPSocket::Connect function. |
The code to get the output stream for writing data onto the TCP socket using the SFXBinaryStreamWriter class is as follows:
// define _socket, the SFXTCPSocket instance, as the member variable class NetworkTime { private: SFXTCPSocket _socket; SFXBinaryStreamwriter _writer; public: // declare the callback function XALLBACK_DECLARE_SFXTCPSOCKET(OnConnect) }; // callback function notified of the connection result XALLBACK_IMPLEMENT_SFXTCPSOCKET(NetworkTime, OnConnect, error) { if (error == SFERR_NO_ERROR) { // get the output stream for writing data onto the TCP socket if ((error = _socket.GetStreamWriter(64, &_writer)) == SFERR_NO_ERROR) { ... } } return; }
SFXTCPSocket::GetStreamReader | SFXBinaryStreamWriter | SFXAnsiStringStreamWriter | SFXWideStringStreamWriter SFXTCPSocket::Accept | SFXTCPSocket::Connect | Stream Buffer
[ public ] SFCError Listen( SInt16 backlog = 1 // queue size to store the waiting connection requests );
This function listens(or starts to wait for the connection request from the TCP client).
The backlog parameter indicates the maximum length for the queue of pending connections. If backlog is less than one, it will be silently increased to one. If backlog is larger than the system maximum, it will be silently reduced to the system maximum.
After this function is executed, the connection can be established by calling the SFXTCPSocket::Accept function to one from the connection request queue of this TCP socket in order to connect to the TCP client.
* If this function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with the SFXTCPSocket::ScheduleListen function where listening will be performed with this function.
Note | |
---|---|
This function internally calls the SFBSocket::Listen function. |
Prerequisite | |
---|---|
This socket needs to be bound with the SFXTCPSocket::Bind function before this function is called. |
SFXTCPSocket::Bind | SFXTCPSocket::Accept | SFXTCPSocket::ScheduleListen | SFBSocket::Listen | BREW API ISOCKET_Listen | BREW API ISOCKET_GetLastError
[ public ] SFCError Open( SInt32 linger = -1 // linger time ( linger time is not set up when linger = -1 ) );
This function opens (or initializes) this this TCP socket.
Concretely, this function creates the SFBNetMgr instance and calls the BREW API INETMGR_OpenSocket function to create the SFBSocket instance of the AEE_SOCK_STREAM type. These instances are internally managed by this TCP socket.
The SFBNetMgr / SFBSocket instance can be obtained with the SFXTCPSocket::GetSFBNetMgr / SFXTCPSocket::GetSFBSocket function. More detailed setting can be realized by using these instances.
Linger time(waiting time for connecting a network) | |
---|---|
The linger time(waiting time for connecting a network) set up by the BREW API INETMGR_SetLinger function, can be specified by the linger argument of this function. Default value of this argument is -1. At this time the linger time is not set up. For more details on the linger time settings, refer to BREW API INETMGR_SetLinger. |
Tip | |
---|---|
If the SFXTCPSocket::Close function is called, this TCP socket will be closed. |
// define _socket, SFXTCPSocket instance, as member variable class TcpSample { private: SFXTCPSocket _socket; public: Void Start(Void); XALLBACK_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 = _socket.Open()) == SFERR_NO_ERROR) { // connect to TCP server // the connection result will be notified to the OnConnect function if ((error = _socket.Connect(address,XALLBACK_INTERNAL(OnConnect))) == SFERR_NO_ERROR) { ... }else { // if an error occurs _socket.Close(); } } if (error != SFERR_NO_ERROR) { // if an error occurs // error handling ... } } // callback function notified of the connection result XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error) { if (error == SFERR_NO_ERROR) { ... } }
SFXTCPSocket::Close | SFXTCPSocket::GetSFBNetMgr | SFXTCPSocket::GetSFBSocket | SFBNetMgr | SFBSocket | BREW API INetMgr | BREW API ISocket | BREW API INETMGR_SetLinger | BREW API INETMGR_OpenSocket
[ public, virtual ] SFCError Read( VoidPtr buffer // buffer to read data into UInt32Ptr size // before this function is called: size of the buffer to read data into; just after this function is returned: size of the data to be read actually );
Specify the buffer to read data into.
Before calling this function, specify the size of the buffer to read data into. Just after this function is returned, the size of data to be read actually will be stored into this argument.
This function reads data from this TCP socket without input stream.
When the peer closes the connection, this function will return SFERR_NO_ERROR and 0 will be returned into the area that the size argument points to.
* If this function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with the SFXTCPSocket::ScheduleRead function where data will be read with this function.
Note | |
---|---|
This function internally calls the SFBAStream::Read function. |
Prerequisite | |
---|---|
The connection of this socket needs to be established with the SFXTCPSocket::Connect function before this function is called. |
// define _socket, SFXTCPSocket instance, as member variable class TcpSample { private: SFXTCPSocket _socket; public: // callback function declaration XALLBACK_DECLARE_SFXTCPSOCKET(OnWrite) XALLBACK_DECLARE_SFXTCPSOCKET(OnRead) }; // callback function notified that data can be sent XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error) { UInt32 size; static ACharConst data[] = "GET / HTTP/1.0\n\n"; // write data size = sizeof(data) - 1; switch (error = _socket.Write(data, &size)) { case SFERR_NO_ERROR: // schedule to read data _socket.ScheduleRead(XALLBACK_INTERNAL(OnRead)); // register callback function break; case AEE_NET_WOULDBLOCK: // schedule to write data _socket.ScheduleWrite(XALLBACK_INTERNAL(OnWrite)); // register callback function break; } } // callback function notified that data is ready to be read XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnRead, error) { SFXBuffer buffer; UInt32 size; // read data buffer.SetSize(32); size = buffer.GetSize(); switch (_socket.Read(buffer.GetBuffer(), &size)) { case SFERR_NO_ERROR: // check whether or not the specified size of data is read if (size == buffer.GetSize()) { // display the read data buffer.SetSize(buffer.GetSize() + 1); buffer[buffer.GetSize() - 1] = '\0'; TRACE("%s", SFXAnsiString(buffer).GetCString()); // close the connection _socket.Close(); } else { ... } break; case AEE_NET_WOULDBLOCK: // schedule to read data _socket.ScheduleRead(XALLBACK_INTERNAL(OnRead)); // register callback function break; } return; }
SFXTCPSocket::ScheduleRead | SFXTCPSocket::Accept | SFBAStream::Read | BREW API ISOCKET_Read
[ public ] SFCError ScheduleAccept( CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
This function schedules to accept(or get one from the connection request queue of this TCP socket in order to connect to the TCP client).
Concretely, this function registers the callback function that will accept(or get one from the connection request queue of this TCP socket in order to connect to the TCP client) with the SFXTCPSocket::Accept function. The registered callback function will be called by BREW AEE when it is possible to accept.
The status of this socket is "under scheduling to accept" until the callback function is called. And after the callback function is called, it will become the status before calling this function, i.e., "this socket is under listening".
*1. If the SFXTCPSocket::Accept function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with this function where accepting will be performed with the SFXTCPSocket::Accept function.
*2. If this function returns the value other than SFERR_NO_ERROR, the specified callback function will not be called.
Note | |
---|---|
This function internally calls the SFBSocket::Writeable function. |
Prerequisite | |
---|---|
Before this function is called, this socket needs to be under listening with the SFXTCPSocket::Listen function. If accepting has already been scheduled, the SFERR_INVALID_STATE error will be returned too. |
SFXTCPSocket::Accept | SFXTCPSocket::Listen | SFBAStream::Readable | SFXStorage::CallbackSPP | BREW API ISOCKET_Readable
[ public ] SFCError ScheduleBind( CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
This function schedules to bind the local IP address and port number to this TCP socket.
Concretely, this function registers the callback function that will bind the local IP address and port number to this TCP socket with the SFXTCPSocket::Bind function. The registered callback function will be called by BREW AEE when it is possible to bind.
The status of this socket is "under scheduling to bind" until the callback function is called. And after the callback function is called, it will become the status before calling this function, i.e., "this socket is opened".
If you call the SFXTCPSocket::Cancel function before the callback function is called, binding will be canceled and the status will be returned to the status before calling this function, i.e., "this socket is opened".
*1. If the SFXTCPSocket::Bind function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with this function where binding will be performed with the SFXTCPSocket::Bind function.
*2. If this function returns the value other than SFERR_NO_ERROR, the specified callback function will not be called.
Note | |
---|---|
This function internally calls the SFBSocket::Writeable function. |
Prerequisite | |
---|---|
Before this function is called, this socket needs to be opened with the SFXTCPSocket::Open function. However, if it is connected or under being connected, the SFERR_INVALID_STATE error will be returned. In addition, if binding has already been scheduled, the SFERR_INVALID_STATE error will be returned too. |
SFXTCPSocket::Bind | SFXTCPSocket::Cancel | SFXTCPSocket::Open | SFBSocket::Writeable | SFXStorage::CallbackSPP | BREW API ISOCKET_Writeable
[ public ] SFCError ScheduleListen( CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
This function schedules to listen(or start to wait for the connection request from the TCP client).
Concretely, this function registers the callback function that will listen(or start to wait for the connection request from the TCP client) with the SFXTCPSocket::Listen function. The registered callback function will be called by BREW AEE when it is possible to listen.
The status of this socket is "under scheduling to listen" until the callback function is called. And after the callback function is called, it will become the status before calling this function, i.e., "this socket is bound".
*1. If the SFXTCPSocket::Listen function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with this function where listening will be performed with the SFXTCPSocket::Listen function.
*2. If this function returns the value other than SFERR_NO_ERROR, the specified callback function will not be called.
Note | |
---|---|
This function internally calls the SFBSocket::Writeable function. |
Prerequisite | |
---|---|
Before this function is called, this socket needs to be bound with the SFXTCPSocket::Bind function. If listening has already been scheduled, the SFERR_INVALID_STATE error will be returned too. |
SFXTCPSocket::Listen | SFBSocket::Writeable | SFXStorage::CallbackSPP | BREW API ISOCKET_Writeable
[ public, virtual ] SFCError ScheduleRead( CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
This function schedules to read data from this TCP socket without input stream.
Concretely, this function registers the callback function that will read data with the SFXTCPSocket::Read function. The registered callback function will be called by BREW AEE when it is possible to read data.
The status of this socket is "under scheduling to read data" until the callback function is called. And after the callback function is called, it will become the status before calling this function, i.e., "the connection of this socket is established".
If you call the SFXTCPSocket::Cancel function before the callback function is called, reading data will be canceled and the status will be returned to the status before calling this function, i.e., "the connection of this socket is established".
*1. If the SFXTCPSocket::Read function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with this function where data will be read with the SFXTCPSocket::Read function.
*2. If this function returns the value other than SFERR_NO_ERROR, the specified callback function will not be called.
Note | |
---|---|
This function internally calls the SFBAStream::Readable function. |
Prerequisite | |
---|---|
The connection of this socket needs to be established with the SFXTCPSocket::Connect function before this function is called. And if this socket has been already scheduled to read data, the SFERR_INVALID_STATE error will be returned. |
// define _socket, SFXTCPSocket instance, as member variable class TcpSample { private: SFXTCPSocket _socket; public: // callback function declaration XALLBACK_DECLARE_SFXTCPSOCKET(OnWrite) XALLBACK_DECLARE_SFXTCPSOCKET(OnRead) }; // callback function notified that data can be sent XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error) { UInt32 size; static ACharConst data[] = "GET / HTTP/1.0\n\n"; // write data size = sizeof(data) - 1; switch (error = _socket.Write(data, &size)) { case SFERR_NO_ERROR: // schedule to read data _socket.ScheduleRead(XALLBACK_INTERNAL(OnRead)); // register callback function break; case AEE_NET_WOULDBLOCK: // schedule to write data _socket.ScheduleWrite(XALLBACK_INTERNAL(OnWrite)); // register callback function break; } } // callback function notified that data is ready to be read XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnRead, error) { SFXBuffer buffer; UInt32 size; // read data buffer.SetSize(32); size = buffer.GetSize(); switch (_socket.Read(buffer.GetBuffer(), &size)) { case SFERR_NO_ERROR: // check whether or not the specified size of data is read if (size == buffer.GetSize()) { // display the read data buffer.SetSize(buffer.GetSize() + 1); buffer[buffer.GetSize() - 1] = '\0'; TRACE("%s", SFXAnsiString(buffer).GetCString()); // close the connection _socket.Close(); } else { ... } break; case AEE_NET_WOULDBLOCK: // schedule to read data _socket.ScheduleRead(XALLBACK_INTERNAL(OnRead)); // register callback function break; } return; }
SFXTCPSocket::Read | SFXTCPSocket::Accept | SFBAStream::Readable | SFXStorage::CallbackSPP | BREW API ISOCKET_Readable
[ public, virtual ] SFCError ScheduleWrite( CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
This function schedules to write data into this TCP socket without output stream.
Concretely, this function registers the callback function that will write data with the SFXTCPSocket::Read function. The registered callback function will be called by BREW AEE when it is possible to write data.
The status of this socket is "under scheduling to write data" until the callback function is called. And after the callback function is called, it will become the status before calling this function, i.e., "the connection of this socket is established".
If you call the SFXTCPSocket::Cancel function before the callback function is called, writing data will be canceled and the status will be returned to the status before calling this function, i.e., "the connection of this socket is established".
*1. If the SFXTCPSocket::Write function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with this function where data will be written with the SFXTCPSocket::Write function.
*2. If this function returns the value other than SFERR_NO_ERROR, the specified callback function will not be called.
Note | |
---|---|
This function internally calls the SFBSocket::Writeable function. |
Prerequisite | |
---|---|
The connection of this socket needs to be established with the SFXTCPSocket::Connect function or this socket needs to be the established socket returned via the return argument of the SFXTCPSocket::Accept function before this function is called. And if this socket has been already scheduled to write data, the SFERR_INVALID_STATE error will be returned. |
// define _socket, SFXTCPSocket instance, as member variable class TcpSample { private: SFXTCPSocket _socket; public: // callback function declaration XALLBACK_DECLARE_SFXTCPSOCKET(OnConnect) XALLBACK_DECLARE_SFXTCPSOCKET(OnWrite) }; // callback function notified of the connection result XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error) { if (error == SFERR_NO_ERROR) { _socket.ScheduleWrite(XALLBACK_INTERNAL(OnWrite)); } } // callback function notified that data is ready to be written XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error) { UInt32 size; static ACharConst data[] = "GET / HTTP/1.0\n\n"; // write data size = sizeof(data) - 1; switch (error = _socket.Write(data, &size)) { case SFERR_NO_ERROR: ... break; case AEE_NET_WOULDBLOCK: // schedule to write data _socket.ScheduleWrite(XALLBACK_INTERNAL(OnWrite)); // register callback function break; } }
SFXTCPSocket::Write | SFBSocket::Writeable | SFXTCPSocket::Accept | SFXStorage::CallbackSPP | BREW API ISOCKET_Writeable
[ public, virtual ] SFCError Write( VoidConstPtr buffer // buffer to write data into UInt32Ptr size // before this function is called: size of the buffer to write data into; just after this function is returned: size of the data to be written actually );
Specify the buffer to write data into.
Before calling this function, specify the size of the buffer to write data into. Just after this function is returned, the size of data to be written into the buffer actually will be stored into this argument.
This function writes data to this TCP socket without output stream.
* If this function returns AEE_NET_WOULDBLOCK, it will be necessary to register a callback function with the SFXTCPSocket::ScheduleWrite function where data will be written with this function.
Note | |
---|---|
This function internally calls the SFBSocket::Write function. |
Prerequisite | |
---|---|
The connection of this socket needs to be established with the SFXTCPSocket::Connect function or be the established socket returned via the return argument of the SFXTCPSocket::Accept function before this function is called. |
// define _socket, SFXTCPSocket instance, as member variable class TcpSample { private: SFXTCPSocket _socket; public: // callback function declaration XALLBACK_DECLARE_SFXTCPSOCKET(OnConnect) XALLBACK_DECLARE_SFXTCPSOCKET(OnWrite) }; // callback function notified of the connection result XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnConnect, error) { if (error == SFERR_NO_ERROR) { _socket.ScheduleWrite(XALLBACK_INTERNAL(OnWrite)); } } // callback function notified that data is ready to be written XALLBACK_IMPLEMENT_SFXTCPSOCKET(TcpSample, OnWrite, error) { UInt32 size; static ACharConst data[] = "GET / HTTP/1.0\n\n"; // write data size = sizeof(data) - 1; switch (error = _socket.Write(data, &size)) { case SFERR_NO_ERROR: ... break; case AEE_NET_WOULDBLOCK: // schedule to write data _socket.ScheduleWrite(XALLBACK_INTERNAL(OnWrite)); // register callback function break; } }
Copyright(c) 2002 - 2024 Sophia Cradle Incorporated All Rights Reserved. |