Пример #1
0
bool MutexImpl::tryLockImpl(long milliseconds)
{
#if defined(POCO_HAVE_MUTEX_TIMEOUT)
    struct timespec abstime;
    struct timeval tv;
    gettimeofday(&tv, NULL);
    abstime.tv_sec  = tv.tv_sec + milliseconds / 1000;
    abstime.tv_nsec = tv.tv_usec*1000 + (milliseconds % 1000)*1000000;
    if (abstime.tv_nsec >= 1000000000)
    {
        abstime.tv_nsec -= 1000000000;
        abstime.tv_sec++;
    }
    int rc = pthread_mutex_timedlock(&_mutex, &abstime);
    if (rc == 0)
        return true;
    else if (rc == ETIMEDOUT)
        return false;
    else
        throw SystemException("cannot lock mutex");
#else
    const int sleepMillis = 5;
    Timestamp now;
    Timestamp::TimeDiff diff(Timestamp::TimeDiff(milliseconds)*1000);
    do
    {
        int rc = pthread_mutex_trylock(&_mutex);
        if (rc == 0)
            return true;
        else if (rc != EBUSY)
            throw SystemException("cannot lock mutex");
#if defined(POCO_VXWORKS)
        struct timespec ts;
        ts.tv_sec = 0;
        ts.tv_nsec = sleepMillis*1000000;
        nanosleep(&ts, NULL);

#else
        struct timeval tv;
        tv.tv_sec  = 0;
        tv.tv_usec = sleepMillis * 1000;
        select(0, NULL, NULL, NULL, &tv);
#endif
    }
    while (!now.isElapsed(diff));
    return false;
#endif
}
Пример #2
0
bool MutexImpl::tryLockImpl(long milliseconds)
{
	const int sleepMillis = 5;
	Timestamp now;
	Timestamp::TimeDiff diff(Timestamp::TimeDiff(milliseconds)*1000);

	do
	{
		try
		{
			if (tryLockImpl())
				return true;
		}
		catch (...)
		{
			throw SystemException("cannot lock mutex");
		}
		Sleep(sleepMillis);
	} while (!now.isElapsed(diff));
	return false;
}
Пример #3
0
void RTMFPServerEdge::run() {
	_serverSocket.connect(_serverAddress);
	sockets.add(_serverSocket,*this);

	_serverConnection.setEndPoint(_serverSocket,_serverAddress);

	NOTE("Wait RTMFP server %s successful connection...",_serverAddress.toString().c_str());

	Timestamp connectAttemptTime;	
	_serverConnection.connect(_publicAddress);

	bool terminate=false;

	while(running()) {
		if(!sockets.process(_timeout)) {
			if(connectAttemptTime.isElapsed(6000000)) {
				_serverConnection.connect(_publicAddress);
				connectAttemptTime.update();
			}
			continue;
		}
		if(_serverConnection.connected()) {
			NOTE("RTMFP server %s successful connection",_serverAddress.toString().c_str());
			RTMFPServer::run();
			_serverConnection.clear();
			if(_serverConnection.connected()) {
				// Exception in RTMFPServer::run OR a volontary stop
				_serverConnection.setEndPoint(_serverSocket,_serverAddress);
				_serverConnection.disconnect();
				break; 
			}
			NOTE("RTMFP server %s connection lost",_serverAddress.toString().c_str());
		}
	}
	_sendingEngine.clear();
	sockets.remove(_serverSocket);
	_serverSocket.close();
}
Пример #4
0
    void testDocument(const std::string& document)
    {
        Timestamp documentStartTimestamp;

        URI uri(_app.getURL());

        Log::debug() << "Starting client for '" << document << "'" << Log::end;

        HTTPClientSession cs(uri.getHost(), uri.getPort());
        HTTPRequest request(HTTPRequest::HTTP_GET, "/ws");
        HTTPResponse response;
        WebSocket ws(cs, request, response);

        Condition cond;
        Mutex mutex;

        Thread thread;
        Output output(ws, cond, mutex);

        thread.start(output);

        sendTextFrame(ws, "loolclient " + GetProtocolVersion());

        if (document[0] == '/')
            sendTextFrame(ws, "load " + document);
        else
            sendTextFrame(ws, "load url=" + document);

        sendTextFrame(ws, "status");

        // Wait for the "status:" message
        mutex.lock();
        cond.wait(mutex);
        mutex.unlock();

        Log::debug() << "Got status, size=" << output._width << "x" << output._height << Log::end;

        int y = 0;
        const int DOCTILESIZE = 5000;

        std::uniform_int_distribution<> dis(0, 20);
        int extra = dis(_g);

        int requestCount = 0;

        // Exercise the server with this document for some minutes
        while (!documentStartTimestamp.isElapsed((20 + extra) * Timespan::SECONDS) && !clientDurationExceeded())
        {
            int x = 0;
            while (!documentStartTimestamp.isElapsed((20 + extra) * Timespan::SECONDS) && !clientDurationExceeded())
            {
                sendTextFrame(ws,
                              "tile part=0 width=256 height=256 "
                              "tileposx=" + std::to_string(x * DOCTILESIZE) + " "
                              "tileposy=" + std::to_string(y * DOCTILESIZE) + " "
                              "tilewidth=" + std::to_string(DOCTILESIZE) + " "
                              "tileheight=" + std::to_string(DOCTILESIZE));
                requestCount++;
                x = ((x + 1) % ((output._width-1)/DOCTILESIZE + 1));
                if (x == 0)
                    break;
            }
            y = ((y + 1) % ((output._height-1)/DOCTILESIZE + 1));
            Thread::sleep(50);
        }
        sendTextFrame(ws, "canceltiles");

        Thread::sleep(10000);

        Log::debug() << "Sent " << requestCount << " tile requests, shutting down client for '" << document << "'" << Log::end;

        ws.shutdown();
        thread.join();
    }