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

Description

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

  1. Create the UDP socket(instance of SFXUDPSocket class).
  2. Open the UDP socket using the SFXUDPSocket::Open function.
  3. Bind the local IP address and port number with the UDP socket using the SFXUDPSocket::Bind function. If the return value of SFXUDPSocket::Bind function is AEE_NET_WOULDBLOCK, register the callback function using the SFXUDPSocket::ScheduleBind function and try to bind again by calling the SFXUDPSocket::Bind function in the callback function.
  4. Send data asynchronously using the SFXUDPSocket::Send function. The IP address and port number of the send destination should be also set by the SFXUDPSocket::Send function.
  5. Receive data asynchronously using the SFXUDPSocket::Receive function. The IP address and port number of the send source are also received by the SFXUDPSocket::Receive function.
  6. 4. and 5. (sending and receiving processing) are repeated.
  7. Finally, close the UDP socket using the SFXUDPSocket::Close function.

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;
}

Reference

SFXTCPSocket | SFXSSLSocket | Socket Connection

Member

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.

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

Description

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


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

Description

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

Reference

SFXUDPSocket::Close


SFXUDPSocket::AsSFBAStream
[ This function cannot be used now ]
[ public, virtual, const ]
SFCError AsSFBAStream(
    SFBAStreamSmpPtr result   // pointer to SFBAStream class instance
);

Description

Return SFERR_UNSUPPORTED.

Reference

SFBSocket | SFBAStream


SFXUDPSocket::AsSFBSource
[ This function cannot be used now ]
[ public, virtual, const ]
SFCError AsSFBSource(
    SFBSourceSmpPtr result   // pointer to SFBSource class instance
);

Description

Return SFERR_UNSUPPORTED.

Reference

SFBSocket | SFBSource


SFXUDPSocket::Bind
Bind the local IP address and port number to the UDP socket
[ public ]
SFCError Bind(
    SFXSocketAddressConstRef address   // local 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

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.

Reference

SFXUDPSocket::ScheduleBind | SFXInetAddress::LoopbackInetAddress | SFXInetAddress::AnyInetAddress | SFXUDPSocket::Send | SFXUDPSocket::Receive


SFXUDPSocket::Cancel
Cancel the UDP socket communication.
[ public, virtual ]
Void Cancel(Void);

SFXUDPSocket::Close
Close the UDP Socket.
[ public ]
Void Close(Void);

Description

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.


SFXUDPSocket::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 UDP socket connection.

Reference

SFXInetAddress::LocalInetAddress


SFXUDPSocket::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.


SFXUDPSocket::GetStreamReader
[ This function cannot be used now ]
[ 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
);

Description

Return SFERR_UNSUPPORTED.


SFXUDPSocket::GetStreamWriter
[ This function cannot be used now ]
[ 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
);

Description

return SFERR_UNSUPPORTED.


SFXUDPSocket::Open
Open the UDP 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 UDP socket(instance of SFBSocket class).

Reference

SFBNetMgr | SFBSocket


SFXUDPSocket::Read
[ This function cannot be used now ]
[ public, virtual ]
SFCError Read(
    VoidPtr buffer   // buffer to store the read data
    UInt32Ptr size   // buffer size
);

Description

Return SFERR_UNSUPPORTED.


SFXUDPSocket::Receive
Receive data using the UDP socket.
[ 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)
);

Argument

size

Specify the buffer size before calling the SFXUDPSocket::Receive function. The size of data received actually is stored after calling the SFXUDPSocket::Receive function.

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 cannot receive data : AEE_NET_ERROR

Description

The SFXUDPSocket::Receive function returns the result immediately. AEE_NET_WOULDBLOCK will be returned when the packet does not arrive.

Reference

SFXUDPSocket::ScheduleReceive


SFXUDPSocket::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 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] 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.

Reference

SFXUDPSocket::Bind


SFXUDPSocket::ScheduleRead
[ This function cannot be used now ]
[ public, virtual ]
SFCError ScheduleRead(
    CallbackSPP spp     // callback function
    VoidPtr reference   // data passed to callback function
);

Description

Return SFERR_UNSUPPORTED.


SFXUDPSocket::ScheduleReceive
Schedule to receive data.
[ public ]
SFCError ScheduleReceive(
    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 SFXUDPSocket::ScheduleReceive function is used to register the callback function in which the SFXUDPSocket::Receive function is used to receive data.

[Note] 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.

Reference

SFXUDPSocket::Receive


SFXUDPSocket::ScheduleSend
Schedule to send data.
[ public ]
SFCError ScheduleSend(
    CallbackSPP spp     // callback function
    VoidPtr reference   // pointer 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 SFXUDPSocket::ScheduleSend function is used to register the callback function in which the SFXUDPSocket::Send function is used to send data.

[Note] 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.

Reference

SFXUDPSocket::Send


SFXUDPSocket::ScheduleWrite
[ This function cannot be used now ]
[ public, virtual ]
SFCError ScheduleWrite(
    CallbackSPP spp     // callback function
    VoidPtr reference   // pointer to callback function
);

Description

Return SFERR_UNSUPPORTED.


SFXUDPSocket::Send
Send data using the UDP socket.
[ 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)
);

Argument

size

Specify the buffer size before calling the SFXUDPSocket::Send function. The size of data sent actually is stored after calling the SFXUDPSocket::Send function.

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 cannot send data : AEE_NET_ERROR

Description

The SFXUDPSocket::Send function returns the result immediately. AEE_NET_WOULDBLOCK will be returned if sending data is blocked.

Reference

SFXUDPSocket::ScheduleSend


SFXUDPSocket::Write
[ This function cannot be used now ]
[ public, virtual ]
SFCError Write(
    VoidConstPtr buffer   // data to write
    UInt32Ptr size        // size of data to write
);

Description

Return SFERR_UNSUPPORTED.


SFXUDPSocket::CallbackSPP
Prototype of the callback function.
typedef Void(* SFXUDPSocket::CallbackSPP)(SFCError error, VoidPtr reference)

Description

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