Remote Call Framework 3.4
Server-side Sessions

Server-side Session Objects - Server

This sample demonstrates using RCF::RcfSession::getSessionObject() and RCF::RcfServer::getServerObject(), to maintain connection sessions and server sessions.

#include <iostream>
#include <RCF/RCF.hpp>
// Define RCF interface.
RCF_BEGIN(I_PrintService, "I_PrintService")
RCF_METHOD_V2(void, Print, const std::string&, const std::string &)
RCF_END(I_PrintService)
// This class represents state associated with a single network connection to the server.
class ConnectionSession
{
public:
int mCallsMade = 0;
};
// This class represents state associated with a server session on the server.
// It is not linked to any particular network connection.
class ServerSession
{
public:
std::string mUserName;
int mCallsMade = 0;
};
typedef std::shared_ptr<ServerSession> ServerSessionPtr;
class PrintService
{
public:
void Print(const std::string& userName, const std::string & msg)
{
// Update the connection session.
ConnectionSession & session = rcfSession.getSessionObject<ConnectionSession>(true);
++session.mCallsMade;
// Update the server session.
RCF::RcfServer & server = rcfSession.getRcfServer();
ServerSessionPtr sessionPtr = server.getServerObject<ServerSession>(userName, 60 * 1000);
sessionPtr->mUserName = userName;
++sessionPtr->mCallsMade;
std::cout << std::endl;
std::cout << "I_PrintService service (" << userName << "): " << msg << std::endl;
std::cout << "Print() calls made on this connection: " << session.mCallsMade << std::endl;
std::cout << "Print() calls made by this user: " << sessionPtr->mCallsMade << std::endl;
}
};
int main()
{
try
{
RCF::RcfInit rcfInit;
PrintService printService;
RCF::RcfServer server(RCF::TcpEndpoint("127.0.0.1", 50001));
server.bind<I_PrintService>(printService);
server.start();
std::cout << "Press Enter to exit..." << std::endl;
std::cin.get();
}
catch ( const RCF::Exception & e )
{
std::cout << "Error: " << e.getErrorMessage() << std::endl;
}
return 0;
}

Server-side Session Objects - Client

This sample demonstrates a client calling a server and maintaining a connection session and a server session on the server.

#include <iostream>
#include <RCF/RCF.hpp>
// Define RCF interface.
RCF_BEGIN(I_PrintService, "I_PrintService")
RCF_METHOD_V2(void, Print, const std::string&, const std::string &)
RCF_END(I_PrintService)
int main()
{
try
{
RCF::RcfInit rcfInit;
std::string userName = "Joe Bloggs";
for ( int i = 0; i < 3; ++i )
{
RcfClient<I_PrintService> client(RCF::TcpEndpoint("127.0.0.1", 50001));
for ( int i = 0; i < 5; ++i )
{
client.Print(userName, "Hello World");
}
}
}
catch ( const RCF::Exception & e )
{
std::cout << "Error: " << e.getErrorMessage() << std::endl;
}
return 0;
}