example/C++/Makefile.am
noinst_PROGRAMS = Filter1$(EXEEXT) mulserver$(EXEEXT) mulclient$(EXEEXT) manfilter$(EXEEXT) \
testserver$(EXEEXT) testclient$(EXEEXT) MyClient$(EXEEXT) MyServer$(EXEEXT) Filter2$(EXEEXT) \
Filter3$(EXEEXT)
AM_CXXFLAGS = -I$(srcdir)/../../ccmsgque -DMQ_LINK_WITH_LIBRARY_OBJECT_FILES
LDADD = ../../ccmsgque/*.lo
MyClient_SOURCES = MyClient.cc MyServer_SOURCES = MyServer.cc
win.example/C++/MyServer.cc
#include "ccmsgque.h" using namespace ccmsgque; class MyServer : public MqC, public IServerSetup, public IFactory { // service to serve all incomming requests for token "HLWO" void MyFirstService () { SendSTART(); SendC("Hello World"); SendRETURN(); } // factory to create objects MqC* Factory() const {return new MyServer();} // define a service as link between the token "HLWO" and the callback "MyFirstService" void ServerSetup() { ServiceCreate("HLWO", CallbackF(&MyServer::MyFirstService)); } }; int MQ_CDECL main(int argc, MQ_CST argv[]) { MyServer srv; try { srv.LinkCreateVC(argc, argv); srv.ProcessEvent(); } catch (const exception& e) { srv.ErrorSet(e); } srv.Exit(); }
The server is started as network visible TCP server listen on PORT 2345 using a THREAD for every new connection request:
> MyServer --tcp --port 2345 --thread
If you are using UNIX and if you want to setup a high-performance local server then use the build-in UDS (Unix-Domain-Sockets) capability to listen on the FILE /path/to/any/file.uds instead on a network port:
> MyServer --uds --file /path/to/any/file.uds --thread
Three things are important:
Services are created with the ctx.ServiceCreate(MQ_CST const token, IService * const service) function. The first parameter is a 4 byte Token as public name. 4 byte is required because this string is mapped to a 4 byte integer for speed reason. The second parameter is an object providing the SERVICE CALLBACK interface.
The SERVICE CALLBACK function has one parameter of type MqC*. This parameter refer to the original object (in our case MyServer). If the service object (in our case MyFirstService) is a subclass of the original MqC* object class, the both statements SendSTART() and ctx.sendSTART() are identical.
#include <iostream> #include "ccmsgque.h" using namespace std; using namespace ccmsgque; int MQ_CDECL main(int argc, MQ_CST argv[]) { MqC c; try { c.LinkCreateVC(argc, argv); c.SendSTART(); c.SendEND_AND_WAIT("HLWO", MQ_TIMEOUT_USER); cout << c.ReadC() << endl; } catch (const exception& e) { c.ErrorSet(e); } c.Exit(); }
To call a network visible TCP server listen on PORT 2345 use:
> MyClient --tcp --port 2345 > Hello World
To call a network visible UDP server listen on FILE /path/to/any/file.uds use:
> MyClient --udp --file /path/to/any/file.uds > Hello World
To call a local server started by the client using PIPE communication use:
> MyClient @ MyServer > Hello World
1.5.8