示例#1
0
/*
 * Begins the jsonRPC server endless loop
 * Never quits unless jsonRpc_stop() is called (todo)
 * Can be run in a separate thread
 */
int jsonRpc_run(jsonRpcServer_t* jrs)
{
	fd_set fd;
	timeval sTimeout;
	sTimeout.tv_sec = 1;
	sTimeout.tv_usec = 0;
	while( true )
	{
		FD_ZERO(&fd);
		// add server accept socket
		FD_SET(jrs->acceptSocket, &fd);
		// add all connected sockets
		for(uint32 i=0; i<jrs->list_connections->objectCount; i++)
		{
			jsonRpcClient_t* client = (jsonRpcClient_t*)simpleList_get(jrs->list_connections, i);
			FD_SET(client->clientSocket, &fd);
		}
		// check for socket events
		sint32 r = select(0, &fd, 0, 0, &sTimeout); // wait one second
		if( r )
		{
			// check for new connections
			if( FD_ISSET(jrs->acceptSocket, &fd) )
			{
				jsonRpcServer_newClient(jrs, accept(jrs->acceptSocket, 0, 0));
			}
			// check for client data received
			for(uint32 i=0; i<jrs->list_connections->objectCount; i++)
			{
				jsonRpcClient_t* client = (jsonRpcClient_t*)simpleList_get(jrs->list_connections, i);
				if( FD_ISSET(client->clientSocket, &fd) )
				{
					if( jsonRpcServer_receiveData(jrs, client) == false )
					{
						// something went wrong inside of _receiveData()
						// make sure the client gets disconnected
						client->disconnected = true;
					}
				}
				if( client->disconnected )
				{
					// delete client
					jsonRpc_deleteClient(client);
					// remove from list
					jrs->list_connections->objects[i] = jrs->list_connections->objects[jrs->list_connections->objectCount-1];
					jrs->list_connections->objectCount--;
				}
			}
		}
		else
			sleep(1);
	}
	return 0;
}
示例#2
0
/*
 * Helper method in case parsing of an array fails and the already parsed data needs to be freed again
 */
void jsonObject_destroyArray(jsonObjectArray_t* jsonObjectArray)
{
	for(uint32 i=0; i<jsonObjectArray->list_values->objectCount; i++)
	{
		jsonObject_t* objValue = (jsonObject_t*)simpleList_get(jsonObjectArray->list_values, i);
		jsonObject_freeObject(objValue);
	}
	simpleList_free(jsonObjectArray->list_values);
	free(jsonObjectArray);
}