Network shared memory - NetMemSpecifications | |||||||||
|
API
The communcation between the clients and server is done with a structure that contains all the informations necessaries to perform one operation and get response from the server:
struct NetMemCommand { unsigned char type; long offset; long length; long version; unsigned char * data; };The comand types are defines in the same header file - /src/utils/ApiNetMem.h - along with the responses from server and the error codes:
After sending the command to the server, the client will receive a NetMem struct instance that will contains the response - for the read/lock commands the data from the memory page will be in the data member of the struct: //connect to the NetMem server and initialize the client NetworkMemory netMem(host, port); NetMemCommand command; command.offset=2; //index of the page we want to lock ClientMemoryPage *page = netMem.LockPage(&command); //here the page->data will contain the data for the page number 2 from the server //read and modify the page->data ... //upload the new data for the page and unlock it netMem.UnlockPage(page); //or just update the page with an integer for instance; you can update the version also //if the new version is negative, just increase the page version int * data=(int *)malloc(4); *data = 25432; command.offset=1; command.data = (unsigned char *)data; command.version = i; netMem.UpdatePage(&command); Server
The server part contains 3 classes:
ServerSocket - the main class that allocate memory and listen on a port: ServerSocket *server=new ServerSocket(noPages,lengthPage); server->StartThreadSocket(port);In constructor, the server creates a vector of MemoryPage instances that will be read/lock/unlock by the clients. The socket server starts a thread that accepts connection: with every connection, a ClientSocket instance is created and a new thread is created for every client in order to get commands from clients and execute them. The ClientSocket class is responsible of reading client command and respond: a NetMemCommand is filled and send back at client; id the command was read or lock, the page data is also send to the client only if the version of the page on the client is different that the version of the page on the server. If the client disconnects, the ClientSocket corresponding for that client is deleted, and all the locks made by that client are released. Client
After the server is online the client can connect and use the pages of memory, for instance the read
//connect to the NetMem server and initialize the client NetworkMemory netMem(host, port); NetMemCommand command; command.offset=5; //index of the page we want to read netMem.ReadPage(&command); //here the command.data will contain the data for the page number 5 from the server ...There are two more commands that can be used: //connect to the NetMem server and initialize the client NetworkMemory netMem(host, port); NetMemCommand command; netMem.GetParams(&command); //here the command.offset will contain the number of pages //and the command.version the length of each page ... unsigned long version = netMem.GetServerVersion(); //the version contains 4 bytes // the most significant byte contains the API major version // 2-nd byte - API major version // 3-rd byte - server major version // 4-th byte - server minor version Copy constructor
To use this system in a multithread environment, just create a (or several) copy instance and use it in other threads
with the same cache for all the relatives.
//connect to the NetMem server and initialize the client NetworkMemory netMem(host, port); //create a clone that will not allocate memory for their cache, but use the original one NetworkMemory copyNetMem(netMem); //use it ... |