![]() ![]() ![]()
|
BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1 |
The SFXUDPSocket class encapsulates the ISocket/ISSL interface of BREW API and provides high level functions for the UDP socket communication.
How to use the UDP socket
Example 496. Sample code for the UDP socket communication
// define _socket, instance of SFXUDPSocket class, as member variable class MyClass { private: SFXUDPSocket _socket; public: Void Start(Void); // callback function CALLBACK_DECLARE_SFXUDPSOCKET(OnBind) CALLBACK_DECLARE_SFXUDPSOCKET(OnSend) CALLBACK_DECLARE_SFXUDPSOCKET(OnReceive) }; Void MyClass::Start(Void) { SFCError error; // open socket if ((error = _socket.Open()) == SFERR_NO_ERROR) { // bind local IP address and port number to socket OnBind(SFERR_NO_ERROR); } return; } // callback function notified of completion of binding CALLBACK_IMPLEMENT_SFXUDPSOCKET(MyClass, OnBind, error) { SFXSocketAddress address(SFXInetAddress::LoopbackInetAddress(), 1024); // check error if (error == SFERR_NO_ERROR) { error = _socket.Bind(address); switch (error) { case SFERR_NO_ERROR: // send data asynchronously OnSend(SFERR_NO_ERROR); break; case AEE_NET_WOULDBLOCK: // register callback function notified of completion of binding _socket.ScheduleBind(CALLBACK_FUNCTION(OnBind)); // register callback function break; } } return; } // callback function notified of completion of sending data CALLBACK_IMPLEMENT_SFXUDPSOCKET(MyClass, OnSend, error) { static ACharConst data[] = "udp!"; SFXSocketAddress address(SFXInetAddress::LoopbackInetAddress(), 1024); UInt32 size; // check error if (error == SFERR_NO_ERROR) { size = sizeof(data) - 1; // send data asynchronously error = _socket.Send(address, data, &size); switch (error) { case SFERR_NO_ERROR: // check whether data of specified size is written or not // SFXUDPSocket::Send function may not send data of specified size at a time // here for simple explanation, display error message if (size == sizeof(data) - 1) { // asynchronously read the data OnReceive(SFERR_NO_ERROR); } else { TRACE("...send failed..."); } break; case AEE_NET_WOULDBLOCK: // register callback function notified of completion of sending data _socket.ScheduleSend(CALLBACK_FUNCTION(OnSend)); break; } } return; } // callback function notified of completion of receiving data CALLBACK_IMPLEMENT_SFXUDPSOCKET(MyClass, OnReceive, error) { SFXSocketAddress socket; SFXBuffer buffer; UInt32 size; // check error if (error == SFERR_NO_ERROR) { buffer.SetSize(4); size = static_cast<UInt16>(buffer.GetSize()); // receive data asynchronously switch (_socket.Receive(&socket, buffer.GetBuffer(), &size)) { case SFERR_NO_ERROR: // check whether data of specified size is read or not // SFXUDPSocket::Receive function may not receive data of specified size at a time // here for simple explanation, display error message if (size == buffer.GetSize()) { // display received data buffer.SetSize(buffer.GetSize() + 1); buffer[buffer.GetSize() - 1] = '\0'; TRACE(":%s", SFXAnsiString(buffer).GetCString()); // close socket _socket.Close(); } else { TRACE("...receive failed..."); } break; case AEE_NET_WOULDBLOCK: // register callback function notified of completion of receiving data _socket.ScheduleReceive(CALLBACK_FUNCTION(OnReceive)); break; } } return; }
| Constructor/Destructor |
|---|
|
SFXUDPSocket( Void ) Constructor of SFXUDPSocket class.
|
|
~SFXUDPSocket( Void ) Destructor of SFXUDPSocket class.
|
| Public Functions | |
|---|---|
| SFCError |
AsSFBAStream(
SFBAStreamSmpPtr result
) [ This function cannot be used now ]
|
| SFCError |
AsSFBSource(
SFBSourceSmpPtr result
) [ This function cannot be used now ]
|
| SFCError |
Bind(
SFXSocketAddressConstRef address
) Bind the local IP address and port number to the UDP socket
|
| Void |
Cancel( Void ) Cancel the UDP socket communication.
|
| Void |
Close( Void ) Close the UDP Socket.
|
| SFCError |
GetLocalAddress(
SFXSocketAddressPtr result
) Get the local IP address and port number.
|
| SFBSocketSmpConstRef |
GetSFBSocket( Void ) Get the socket that is internally used.
|
| SFCError |
GetStreamReader(
UInt32 size
, SFXStreamReaderPtr result
) GetStreamReader( SFXStreamReaderPtr result ) [ This function cannot be used now ]
|
| SFCError |
GetStreamWriter(
UInt32 size
, SFXStreamWriterPtr result
) GetStreamWriter( SFXStreamWriterPtr result ) [ This function cannot be used now ]
|
| SFCError |
Open( Void ) Open the UDP socket.
|
| SFCError |
Read(
VoidPtr buffer
, UInt32Ptr size
) [ This function cannot be used now ]
|
| SFCError |
Receive(
SFXSocketAddressPtr address
, VoidPtr buffer
, UInt32Ptr size
, UInt16 option = 0x0000
) Receive data using the UDP socket.
|
| SFCError |
ScheduleBind(
CallbackSPP spp
, VoidPtr reference
) Schedule to bind.
|
| SFCError |
ScheduleRead(
CallbackSPP spp
, VoidPtr reference
) [ This function cannot be used now ]
|
| SFCError |
ScheduleReceive(
CallbackSPP spp
, VoidPtr reference
) Schedule to receive data.
|
| SFCError |
ScheduleSend(
CallbackSPP spp
, VoidPtr reference
) Schedule to send data.
|
| SFCError |
ScheduleWrite(
CallbackSPP spp
, VoidPtr reference
) [ This function cannot be used now ]
|
| SFCError |
Send(
SFXSocketAddressConstRef address
, VoidConstPtr buffer
, UInt32Ptr size
, UInt16 option = 0x0000
) Send data using the UDP socket.
|
| SFCError |
Write(
VoidConstPtr buffer
, UInt32Ptr size
) [ This function cannot be used now ]
|
| Types |
|---|
|
CallbackSPP Prototype of the callback function.
|
[ public, explicit ] SFXUDPSocket(Void);
[ public, virtual ] ~SFXUDPSocket(Void);
The SFXUDPSocket::Close function is called in the destructor.
[ public, virtual, const ] SFCError AsSFBAStream( SFBAStreamSmpPtr result // pointer to SFBAStream class instance );
Return SFERR_UNSUPPORTED.
[ public, virtual, const ] SFCError AsSFBSource( SFBSourceSmpPtr result // pointer to SFBSource class instance );
Return SFERR_UNSUPPORTED.
[ public ] SFCError Bind( SFXSocketAddressConstRef address // local IP address and port number );
In case of the loopback communication within the device only, obtain the local IP address by calling the SFXInetAddress::LoopbackInetAddress function. And when communication with an external server, obtain the local IP address by calling the SFXInetAddress::AnyInetAddress function.
The IP address bound to the UDP socket by SFXUDPSocket::Bind function is the source IP address when sending data using the SFXUDPSocket::Send function (i.e. the source IP address of data that is received using the SFXUDPSocket::Receive function).
When the return value of SFXUDPSocket::Bind function is AEE_NET_WOULDBLOCK, register the callback function using the SFXUDPSocket::ScheduleBind function, and then schedule the SFXUDPSocket::Bind function in the callback function again.
SFXUDPSocket::ScheduleBind | SFXInetAddress::LoopbackInetAddress | SFXInetAddress::AnyInetAddress | SFXUDPSocket::Send | SFXUDPSocket::Receive
[ public, virtual ] Void Cancel(Void);
[ public ] Void Close(Void);
Cancel the UDP socket communication, and then and then the ISocket and INetMgr interfaces used internally are released.
When the BREW application is suspended, call the SFXUDPSocket::Close function to release the resources.
[ public, const ] SFCError GetLocalAddress( SFXSocketAddressPtr result // pointer to the local IP address and port number );
![]() |
Change of Local IP address |
|---|---|
The local IP address might be changed between before and after the UDP socket connection. | |
[ public, const ] SFBSocketSmpConstRef GetSFBSocket(Void);
Get the socket (instance of SFBSocket class) that is used internally.
[ public, virtual ] SFCError GetStreamReader( UInt32 size // buffer size SFXStreamReaderPtr result // pointer to the stream for data receiving );
[ public, virtual ] SFCError GetStreamReader( SFXStreamReaderPtr result // pointer to the stream for data receiving );
Return SFERR_UNSUPPORTED.
[ public, virtual ] SFCError GetStreamWriter( UInt32 size // size SFXStreamWriterPtr result // pointer to the stream for data sending );
[ public, virtual ] SFCError GetStreamWriter( SFXStreamWriterPtr result // pointer to the stream for data sending );
return SFERR_UNSUPPORTED.
[ public ] SFCError Open(Void);
Create the instance of SFBNetMgr class and open the UDP socket(instance of SFBSocket class).
[ public, virtual ] SFCError Read( VoidPtr buffer // buffer to store the read data UInt32Ptr size // buffer size );
Return SFERR_UNSUPPORTED.
[ public ] SFCError Receive( SFXSocketAddressPtr address // pointer to the IP address and port number of sending source VoidPtr buffer // buffer to store the received data UInt32Ptr size // buffer size UInt16 option = 0x0000 // 0 is set. (unused) );
Specify the buffer size before calling the SFXUDPSocket::Receive function. The size of data received actually is stored after calling the SFXUDPSocket::Receive function.
The SFXUDPSocket::Receive function returns the result immediately. AEE_NET_WOULDBLOCK will be returned when the packet does not arrive.
[ public ] SFCError ScheduleBind( CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
The SFXUDPSocket::ScheduleBind function is used to register the callback function in which Bind is done using the SFXUDPSocket::Bind function. SFXUDPSocket::ScheduleBind function is used to register the callback function in which Bind is done by using SFXUDPSocket::Bind function.
![]() |
Note |
|---|---|
| When the return value of SFXUDPSocket::Bind function is AEE_NET_WOULDBLOCK, register the callback function using the SFXUDPSocket::ScheduleBind function, and then schedule the SFXUDPSocket::Bind function in the callback function again. | |
[ public, virtual ] SFCError ScheduleRead( CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
Return SFERR_UNSUPPORTED.
[ public ] SFCError ScheduleReceive( CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
The SFXUDPSocket::ScheduleReceive function is used to register the callback function in which the SFXUDPSocket::Receive function is used to receive data.
![]() |
Note |
|---|---|
| When the return value of SFXUDPSocket::Receive function is AEE_NET_WOULDBLOCK, register the callback function using the SFXUDPSocket::ScheduleReceive function, and then schedule the SFXUDPSocket::Receive function in the callback function again. | |
[ public ] SFCError ScheduleSend( CallbackSPP spp // callback function VoidPtr reference // pointer to callback function );
The SFXUDPSocket::ScheduleSend function is used to register the callback function in which the SFXUDPSocket::Send function is used to send data.
![]() |
Note |
|---|---|
| When the return value of SFXUDPSocket::Send function is AEE_NET_WOULDBLOCK, register the callback function using the SFXUDPSocket::ScheduleSend function, and then schedule the SFXUDPSocket::Send function in the callback function again. | |
[ public, virtual ] SFCError ScheduleWrite( CallbackSPP spp // callback function VoidPtr reference // pointer to callback function );
Return SFERR_UNSUPPORTED.
[ public ] SFCError Send( SFXSocketAddressConstRef address // address and port number of sending destination VoidConstPtr buffer // data to send UInt32Ptr size // size of data to send UInt16 option = 0x0000 // 0 is set. (unused) );
Specify the buffer size before calling the SFXUDPSocket::Send function. The size of data sent actually is stored after calling the SFXUDPSocket::Send function.
The SFXUDPSocket::Send function returns the result immediately. AEE_NET_WOULDBLOCK will be returned if sending data is blocked.
[ public, virtual ] SFCError Write( VoidConstPtr buffer // data to write UInt32Ptr size // size of data to write );
Return SFERR_UNSUPPORTED.
typedef Void(* SFXUDPSocket::CallbackSPP)(SFCError error, VoidPtr reference)
SFXUDPSocket::CallbackSPP is the type of callback function for the SFXUDPSocket class.
The result of the UDP socket communication is notified to this callback function.
The 1st argument is an error code, and the 2nd argument is a parameter for the callback function(in general, instance of SFXUDPSocket class).
|
Copyright (C) 2002 - 2009 Sophia Cradle, Inc. All Rights Reserved. |
![]() ![]() ![]()
|