![]() ![]() ![]()
|
BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1 |
#include <SFXSSLSocket.h.hpp>
class SFXSSLSocket : public SFXStorage;
typedef SFXSSLSocket& SFXSSLSocketRef; typedef SFXSSLSocket* SFXSSLSocketPtr; typedef SFXSSLSocket*& SFXSSLSocketPtrRef; typedef SFXSSLSocket** SFXSSLSocketHandle; typedef const SFXSSLSocket ConstSFXSSLSocket; typedef const SFXSSLSocket& ConstSFXSSLSocketRef; typedef const SFXSSLSocket* ConstSFXSSLSocketPtr; typedef const SFXSSLSocket*& ConstSFXSSLSocketPtrRef; typedef const SFXSSLSocket** ConstSFXSSLSocketHandle;


SFXSSLSocket class encapsulates BREW API ISocket/ISSL interface and provides high level SSL socket communication functions.
How to use the SSL socket
Example 492. Sample code for the SSL socket communication
// define _socket, instance of SFXSSLSocket class, as member variable class MyClass { private: SFXSSLSocket_socket; SFXAnsiStringStreamWriter _writer; // stream for sending data SFXAnsiStringStreamReader _reader; // stream for receiving data public: Void Start(Void); // callback function CALLBACK_DECLARE_SFXSSLSOCKET(OnConnect) CALLBACK_DECLARE_SFXSSLSOCKET(OnNegotiate) CALLBACK_DECLARE_SFXANSISTRINGSTREAMWRITER(OnFlush) CALLBACK_DECLARE_SFXANSISTRINGSTREAMREADER(OnFetch) }; Void MyClass::Start(Void) { SFCError error; SFXSocketAddress host("www.example.com:995"); // open TCP socket if ((error = _socket.Open()) == SFERR_NO_ERROR) { // host name is automatically resolved // connect to TCP server // connection establishment will be notified to OnConnect function error = _socket.Connect(host, CALLBACK_FUNCTION(OnConnect)); } if (error != SFERR_NO_ERROR) { // if error occurs _socket.Close(); } return; } // callback function notified of connection establishment CALLBACK_IMPLEMENT_SFXSSLSOCKET(MyClass, OnConnect, error) { if (error == SFERR_NO_ERROR) { // no error // negotiate // negotiation completion will be notified to OnNegotiate function error = _socket.Negotiate(CALLBACK_FUNCTION(OnNegotiate)); } if (error != SFERR_NO_ERROR) { // if error occurs _socket.Close(); } return; } // callback function notified of negotiation completion CALLBACK_IMPLEMENT_SFXSSLSOCKET(MyClass, OnNegotiate, error) { static AChar sendingMessage[] = "GET / HTTP/1.0\r\n\r\n"; if (error == SFERR_NO_ERROR) { // get the stream for data sending (buffer size: 1024 ) if ((error = _socket.GetStreamWriter(1024, &_writer)) == SFERR_NO_ERROR) { // write the data if ((error = _writer.Write(sendingMessage, lengthof(sendingMessage))) == SFERR_NO_ERROR) { // send data actually // completion of sending data actually will be notified to OnFlush function error = _writer.Flush(CALLBACK_FUNCTION(OnFlush)); } if (error != SFERR_NO_ERROR) { // if error occurs _writer.Release(); } } } if (error != SFERR_NO_ERROR) { // if error occurs _socket.Close(); } return; } // callback function notified of completion of sending data actually CALLBACK_IMPLEMENT_SFXANSISTRINGSTREAMWRITER(MyClass, OnFlush, error) { // sending completed, release the stream for the data sending _writer.Release(); if (error == SFERR_NO_ERROR) { // get input stream for receiving data (buffer size: 1024 ) if ((error = _socket.GetStreamReader(1024, &_reader)) == SFERR_NO_ERROR) { // receive data actually // completion of receiving data actually will be notified to onFetch function if ((error = _reader.Fetch(CALLBACK_FUNCTION(OnFetch))) != SFERR_NO_ERROR) { // if error occurs _reader.Release(); } } } if (error != SFERR_NO_ERROR) { // if error occurs _socket.Close(); } return; } // callback function notified of completion of receiving data actually CALLBACK_IMPLEMENT_SFXANSISTRINGSTREAMREADER(MyClass, OnFetch, error) { SFXAnsiString receivedString; if (error == SFERR_NO_ERROR) { // read data through input stream _reader >> receivedString; // display response TRACE("%s", receivedString.GetCString()); } // release input stream after receiving data _reader.Release(); // close socket _socket.Close(); return; }
| Constructor/Destructor |
|---|
|
SFXSSLSocket( Void ) Constructor of SFXSSLSocket class.
|
|
~SFXSSLSocket( Void ) Destructor of SFXSSLSocket class.
|
| Public Functions | |
|---|---|
| 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 SSL socket communication.
|
| Void |
Close( Void ) Close the SSL Socket.
|
| SFCError |
Connect(
SFXSocketAddressConstRef address
, CallbackSPP spp
, VoidPtr reference
) Connect to the TCP server over SSL.
|
| SFCError |
GetLocalAddress(
SFXSocketAddressPtr result
) Get the local IP address and port number.
|
| SFCError |
GetRemoteAddress(
SFXSocketAddressPtr result
) Get the remote IP address and port number.
|
| SFBSSLSmpConstRef |
GetSFBSSL( Void ) Get the instance of SFBSSL class that is internally used.
|
| 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 SSL socket.
|
| SFCError |
GetStreamWriter(
UInt32 size
, SFXStreamWriterPtr result
) GetStreamWriter( SFXStreamWriterPtr result ) Get the output stream for writing data to the SSL socket.
|
| UInt32 |
GetTrustMode( Void ) Get the SSL trust mode.
|
| SFCError |
Negotiate(
CallbackSPP spp
, VoidPtr reference
) Perform the Negotiation for the SSL socket communication.
|
| SFCError |
Open( Void ) Open the SSL socket.
|
| SFCError |
Permit( Void ) Set to perform the communication that does not use SSL.
|
| SFCError |
Read(
VoidPtr buffer
, UInt32Ptr size
) Receive data.
|
| SFCError |
ScheduleBind(
CallbackSPP spp
, VoidPtr reference
) Schedule to bind.
|
| SFCError |
ScheduleRead(
CallbackSPP spp
, VoidPtr reference
) Schedule to receive data.
|
| SFCError |
ScheduleWrite(
CallbackSPP spp
, VoidPtr reference
) Schedule to send data.
|
| SFCError |
SetTrustMode(
UInt32 param
) Set the SSL trust mode.
|
| SFCError |
Write(
VoidConstPtr buffer
, UInt32Ptr size
) Send data.
|
| Types |
|---|
|
CallbackSPP
(inherits from SFXStorage)
The prototype of the callback function for the SFXStorage class.
|
[ public, explicit ] SFXSSLSocket(Void);
The instances of SFBNetMgr, SFBSocket, SFBSSL, and SFBSSLRootCerts classes are not created in the constructor.
[ public, virtual ] virtual ~SFXSSLSocket(Void);
The SFXSSLSocket::Close function is called in the destructor.
[ public, virtual, const ] SFCError AsSFBAStream( SFBAStreamSmpPtr result // pointer to the result SFBAStream class instance );
[ public, virtual, const ] SFCError AsSFBSource( SFBSourceSmpPtr result // pointer to the result SFBSource class instance );
The SFXSSLSocket::AsSFBSource function creates and releases the instance of SFBSourceUtil class used internally.
[ public ] SFCError Bind( SFXSocketAddressConstRef address // IP address and port number );
When the return value of SFXSSLSocket::Bind function is AEE_NET_WOULDBLOCK, register the callback function using the SFXSSLSocket::ScheduleBind function, and then schedule the SFXSSLSocket::Bind function in the callback function again.
[ public, virtual ] Void Cancel(Void);
[ public ] Void Close(Void);
Cancel the SSL socket communication, and then the ISocket and INetMgr interfaces used internally are released.
When the BREW application is suspended, call the SFXSSLSocket::Close function to release the resources.
[ public ] SFCError Connect( SFXSocketAddressConstRef address // domain (or IP address) and port number CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
Specify domain (or IP address) and port number. The SFXSSLSocket class will automatically resolve the domain when needed.
Specify the callback function notified of connection establishment.
Specify the data passed to the callback function.
The connection establishment is notified to the callback function registered using the SFXSSLSocket::Connect function.
![]() |
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. | |
[ 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 SSL socket connection. | |
[ public, const ] SFCError GetRemoteAddress( SFXSocketAddressPtr result // pointer to the remote IP address and port number );
![]() |
Change of Remote IP address |
|---|---|
The remote IP address might be changed between before and after the SSL socket connection. | |
[ public, const ] SFBSSLSmpConstRef GetSFBSSL(Void);
[ 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 input stream );
[ public, virtual ] SFCError GetStreamReader( SFXStreamReaderPtr result // pointer to the input stream );
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.
SFXSSLSocket::GetStreamWriter | SFXBinaryStreamReader | SFXAnsiStringStreamReader | SFXWideStringStreamReader
[ 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 );
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
SFXSSLSocket::GetStreamReader | SFXBinaryStreamWriter | SFXAnsiStringStreamWriter | SFXWideStringStreamWriter
[ public, const ] UInt32 GetTrustMode(Void);
The SSL trust mode can be one of the following values.
![]() |
Note |
|---|---|
| Detailed information: ISSL_NegotiateV in the BREW API Reference | |
[ public ] SFCError Negotiate( CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
Perform the Negotiation for the SSL socket communication after the connection is established by the SFXSSLSocket::Connect function.
[ public ] SFCError Open(Void);
Create the instances of SFBNetMgr and SFBSSL classes and open the SSL socket (instance of SFBSocket class).
[ public ] SFCError Permit(Void);
After the connection is established, communication without using SSL is available if calling the SFXSSLSocket::Permit function before Negotiation or after Negotiation failed.
[ public, virtual ] SFCError Read( VoidPtr buffer // buffer that stores the received data UInt32Ptr size // buffer size );
Specify the buffer to store the received data.
Specify the buffer size when calling the SFXSSLSocket::Read function. After the SFXSSLSocket::Read function is executed, the size of received data is stored.
To receive data without using the input stream, use the SFXSSLSocket::Read function.
When the return value of SFXSSLSocket::Read function is AEE_NET_WOULDBLOCK, register the callback function using the SFXSSLSocket::ScheduleRead function, and then schedule the SFXSSLSocket::Read function in the callback function again.
[ public ] SFCError ScheduleBind( CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
The SFXSSLSocket::ScheduleBind function is used to register the callback function in which Bind is done using the SFXSSLSocket::Bind function.
![]() |
Note |
|---|---|
| When the return value of SFXSSLSocket::Bind function is AEE_NET_WOULDBLOCK, register the callback function using the SFXSSLSocket::ScheduleBind function, and then schedule the SFXSSLSocket::Bind function in the callback function again. | |
[ public, virtual ] SFCError ScheduleRead( CallbackSPP spp // callback function VoidPtr reference // data passed to callback function );
The SFXSSLSocket::ScheduleRead function is used to register the callback function in which the SFXSSLSocket::Read function is used to receive data.
![]() |
Note |
|---|---|
| When the return value of SFXSSLSocket::Read function is AEE_NET_WOULDBLOCK, register the callback function using the SFXSSLSocket::ScheduleRead function, and then schedule the SFXSSLSocket::Read function in the callback function again. | |
[ public, virtual ] SFCError ScheduleWrite( CallbackSPP spp // callback function VoidPtr reference // pointer to callback function );
The SFXSSLSocket::ScheduleWrite function is used to register the callback function in which the SFXSSLSocket::Write function is used to send data.
![]() |
Note |
|---|---|
| When the return value of SFXSSLSocket::Write function is AEE_NET_WOULDBLOCK, register the callback function using the SFXSSLSocket::ScheduleWrite function, and then schedule the SFXSSLSocket::Write function in the callback function again. | |
The value set by the SFXSSLSocket::SetTrustMode function is valid until the SFXSSLSocket::Close function is called.
The SSL_RESULT_SERV_VERS error might occur on the real device when calling the SFXSSLSocket::Connect function immediately after this value is changed.
To avoid this error, call the SFXSSLSocket::Close function once, set the SSL trust mode, and then call the SFXSSLSocket::Connect function.
One of the following values are available for the SSL trust mode:
![]() |
Note |
|---|---|
| Detailed information: ISSL_NegotiateV in the BREW API Reference | |
[ public, virtual ] SFCError Write( VoidConstPtr buffer // data to write UInt32Ptr size // size of data to write );
Data to write.
Specify the buffer size when calling the SFXSSLSocket::Write function. After the SFXSSLSocket::Write function is executed, the size of actually written data is stored.
To write data without using the output stream, use the SFXSSLSocket::Write function.
When the return value of SFXSSLSocket::Write function is AEE_NET_WOULDBLOCK, register the callback function using the SFXSSLSocket::ScheduleWrite function, and then schedule the SFXSSLSocket::Write function in the callback function again.
|
Copyright (C) 2002 - 2009 Sophia Cradle, Inc. All Rights Reserved. |
![]() ![]() ![]()
|