simxInt extApi_getTimeDiffInMs(simxInt lastTime) { simxInt currentTime=extApi_getTimeInMs(); if (currentTime<lastTime) return(currentTime+0x03ffffff-lastTime); return(currentTime-lastTime); }
simxInt extApi_send_sharedMem(simxInt clientID,const simxUChar* data,simxInt dataLength) { simxUChar* buff; simxInt startTime; simxInt off=0; simxInt initDataLength=dataLength; if (dataLength==0) return(0); #ifdef _WIN32 startTime=extApi_getTimeInMs(); buff=(simxUChar*)MapViewOfFile(_mmfConn[clientID],FILE_MAP_ALL_ACCESS,0,0,_mmfSize[clientID]+20); if (buff!=0) { if (buff[0]!=1) { UnmapViewOfFile(buff); return(0); } while (dataLength>0) { /* Wait for previous data to be gone: */ while (buff[5]!=0) { if (extApi_getTimeDiffInMs(startTime)>1000) { UnmapViewOfFile(buff); return(0); } } /* ok, we can send the data: */ if (dataLength<=_mmfSize[clientID]) { /* we can send the data in one shot: */ memcpy(buff+20,data+off,dataLength); ((int*)(buff+6))[0]=dataLength; ((int*)(buff+6))[1]=20; ((int*)(buff+6))[2]=initDataLength; dataLength=0; } else { /* just send a smaller part first: */ memcpy(buff+20,data+off,_mmfSize[clientID]); ((int*)(buff+6))[0]=_mmfSize[clientID]; ((int*)(buff+6))[1]=20; ((int*)(buff+6))[2]=initDataLength; dataLength-=(_mmfSize[clientID]); off+=(_mmfSize[clientID]); } buff[5]=1; /* client has something to send! */ } UnmapViewOfFile(buff); return(initDataLength); } return(0); #elif defined (__linux) || defined (__APPLE__) return(0); #endif }
simxUChar* extApi_recv_sharedMem(simxInt clientID,simxInt* dataLength) { simxUChar* buff; simxInt startT; simxInt l=0; simxInt off=0; simxInt retDataOff=0; simxUChar* retData=0; simxInt totalLength=-1; #ifdef _WIN32 buff=(simxUChar*)MapViewOfFile(_mmfConn[clientID],FILE_MAP_ALL_ACCESS,0,0,_mmfSize[clientID]+20); if (buff!=0) { if (buff[0]!=1) { /* we are not connected anymore */ UnmapViewOfFile(buff); return(0); } startT=extApi_getTimeInMs(); while (retDataOff!=totalLength) { /* Wait for data: */ while (buff[5]!=2) { if (extApi_getTimeDiffInMs(startT)>1000) { UnmapViewOfFile(buff); return(0); } } /* ok, data is there! */ /* Read the data with correct length: */ l=((int*)(buff+6))[0]; off=((int*)(buff+6))[1]; totalLength=((int*)(buff+6))[2]; if (retData==0) retData=extApi_allocateBuffer(totalLength); memcpy(retData+retDataOff,buff+off,l); retDataOff=retDataOff+l; /* Tell the other side we have read that part and additional parts could be sent (if present): */ buff[5]=0; } UnmapViewOfFile(buff); dataLength[0]=retDataOff; return(retData); } return(0); #elif defined (__linux) || defined (__APPLE__) return(0); #endif }
int main(int argc,char* argv[]) { int clientID=simxStart((simxChar*)"127.0.0.1",19999,true,true,2000,5); if (clientID!=-1) { printf("Connected to remote API server\n"); // Now try to retrieve data in a blocking fashion (i.e. a service call): int objectCount; int* objectHandles; int ret=simxGetObjects(clientID,sim_handle_all,&objectCount,&objectHandles,simx_opmode_blocking); if (ret==simx_return_ok) printf("Number of objects in the scene: %d\n",objectCount); else printf("Remote API function call returned with error code: %d\n",ret); extApi_sleepMs(2000); // Now retrieve streaming data (i.e. in a non-blocking fashion): int startTime=extApi_getTimeInMs(); int mouseX; simxGetIntegerParameter(clientID,sim_intparam_mouse_x,&mouseX,simx_opmode_streaming); // Initialize streaming while (extApi_getTimeDiffInMs(startTime) < 5000) { ret=simxGetIntegerParameter(clientID,sim_intparam_mouse_x,&mouseX,simx_opmode_buffer); // Try to retrieve the streamed data if (ret==simx_return_ok) // After initialization of streaming, it will take a few ms before the first value arrives, so check the return code printf("Mouse position x: %d\n",mouseX); // Mouse position x is actualized when the cursor is over V-REP's window } // Now send some data to V-REP in a non-blocking fashion: simxAddStatusbarMessage(clientID,"Hello V-REP!",simx_opmode_oneshot); // Before closing the connection to V-REP, make sure that the last command sent out had time to arrive. You can guarantee this with (for example): int pingTime; simxGetPingTime(clientID,&pingTime); // Now close the connection to V-REP: simxFinish(clientID); } return(0); }
simxVoid extApi_initRand() { srand(extApi_getTimeInMs()); }