Esempio n. 1
0
void LogsProcessor::threadProc()
{
    while (!needStop())
    {
        {
            CSectionLock _lock(m_cs);
            if (!m_msgs.empty())
            {
                int last = m_msgs.size() - 1;
                msg m = m_msgs[last];
                m_msgs.pop_back();
                m_msgs.swap(m_towrite);
                m_msgs.push_back(m);
            }
        }
        if (m_towrite.empty())
        {
            closeReqLogs();
            Sleep(250);
            continue;
        }
        saveAll();
        closeReqLogs();
    }
    
    {
        CSectionLock _lock(m_cs);
        if (!m_msgs.empty())    
            m_msgs.swap(m_towrite);
    }
    saveAll();    
    closeReqLogs();
}
Esempio n. 2
0
bool CUploader::Upload(UploadTask* task) {

	if (!m_CurrentEngine) {
		Error(true, "Cannot proceed: m_CurrentEngine is NULL!");
		return false;
	}
	std::string FileName;


	if ( task->getType() == "file" ) {
		FileName = ((FileUploadTask*)task)->getFileName();
		if ( FileName.empty() ) {
			Error(true, "Empty filename!");
			return false;
		}

		if ( ! IuCoreUtils::FileExists (FileName) ) {
			Error(true, "File \""+FileName+"\" doesn't exist!");
			return false;
		}
	}
	m_PrInfo.IsUploading = false;
	m_PrInfo.Total = 0;
	m_PrInfo.Uploaded = 0;
	m_FileName = FileName;
	m_bShouldStop = false;
	if (onConfigureNetworkClient)
		onConfigureNetworkClient(&m_NetworkClient);
	m_CurrentEngine->setNetworkClient(&m_NetworkClient);
	m_CurrentEngine->onDebugMessage.bind(this, &CUploader::DebugMessage);
	m_CurrentEngine->onNeedStop.bind(this, &CUploader::needStop);
	m_CurrentEngine->onStatusChanged.bind(this, &CUploader::SetStatus);
	m_CurrentEngine->onErrorMessage.bind(this, &CUploader::ErrorMessage);

	m_CurrentEngine->setThumbnailWidth(m_nThumbWidth);

	CIUUploadParams uparams;
	uparams.thumbWidth = m_nThumbWidth;
	m_NetworkClient.setProgressCallback(pluginProgressFunc, (void*)this);
	int EngineRes = 0;
	int i = 0;
	do
	{
		if (needStop())
		{
			Cleanup();
			return false;
		}
		EngineRes = m_CurrentEngine->doUpload(task, uparams);
		if ( EngineRes == -1 ) {
			Cleanup();
			return false;
		}
		i++;
		if (needStop())
		{
			Cleanup();
			return false;
		}
		if (!EngineRes && i != m_CurrentEngine->RetryLimit())
		{
			Error(false, "", etRepeating, i);
		}
	}
	while (!EngineRes && i < m_CurrentEngine->RetryLimit());

	if (!EngineRes)
	{
		Error(true, "", etRetriesLimitReached);
		Cleanup();
		return false;
	}

	m_ImageUrl = (uparams.DirectUrl);

	m_ThumbUrl = (uparams.ThumbUrl);

	m_DownloadUrl =  (uparams.ViewUrl);
	return true;
}
Esempio n. 3
0
void NetworkConnection::threadProc()
{
    SOCKET sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock == INVALID_SOCKET)
    {
        sendEvent(NE_ERROR_CONNECT);
        return;
    }

    class autoclose {
        SOCKET socket;
    public:
        autoclose(SOCKET s) : socket(s) {}
        ~autoclose() {
            if (socket != INVALID_SOCKET) { shutdown(socket, 2); closesocket(socket); }
            socket = INVALID_SOCKET; 
        }
    };

    autoclose ac(sock);

    // connecting
    sockaddr_in peer;
    memset(&peer, 0, sizeof(sockaddr_in));
    peer.sin_family = AF_INET;
    peer.sin_port = htons(m_connection.port);

    const std::string& a = m_connection.address;
    if (strspn(a.c_str(), "0123456789.") != a.length())
    {
        struct hostent *hp = gethostbyname(a.c_str());
        if (!hp)
        {
            sendEvent(NE_ERROR_CONNECT);
            return;
        }
        memcpy((char*)&peer.sin_addr, hp->h_addr, sizeof(peer.sin_addr));
    }
    else
    {
        peer.sin_addr.s_addr = inet_addr(m_connection.address.c_str());
    }

    // keep alive option
    DWORD optval = 1;
    if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)(&optval), sizeof(DWORD)))
    {
        sendEvent(NE_ERROR_CONNECT);
        return;
    }
    tcp_keepalive alive;
    alive.onoff = 1;
	alive.keepalivetime = 5000;    // <- время между посылками keep-alive (мс)
	alive.keepaliveinterval = 500; // <- время между посылками при отсутсвии ответа (мс)
    DWORD nSize = 0;
    if  (WSAIoctl(sock, SIO_KEEPALIVE_VALS, &alive, sizeof(alive), NULL, 0, &nSize,NULL,NULL) == SOCKET_ERROR)
    {
        sendEvent(NE_ERROR_CONNECT);
        return;
    }

    sendEvent(NE_CONNECTING);
    if (::connect(sock, (sockaddr*)&peer, sizeof(peer)) == SOCKET_ERROR)
    {
        sendEvent(NE_ERROR_CONNECT);
        return;
    }

    {
        CSectionLock lock(m_cs_connect);
        m_connected = true;
    }
    sendEvent(NE_CONNECT);

    fd_set set;

    timeval tv;
    tv.tv_sec = 0;
    tv.tv_usec = 100;

    while (true)
    {
        if (needStop())
        {
            sendEvent(NE_DISCONNECT);
            break;
        }

        FD_ZERO(&set);
        FD_SET(sock, &set);
        int n = ::select(0, &set, NULL, NULL, &tv);
        if (n == SOCKET_ERROR)
        {
            {
                CSectionLock lock(m_cs_connect);
                m_connected = false;
            }
            sendEvent(NE_ERROR);
            break;
        }

        if (FD_ISSET(sock, &set))
        {
             // receive new data
             int received = ::recv(sock, m_recive_buffer.getData(), m_recive_buffer.getSize(), 0);
             if (received == SOCKET_ERROR)
             {
                 if (WSAGetLastError() == WSAEWOULDBLOCK) {}    // нет данных
                 else
                 {
                    sendEvent(NE_DISCONNECT);
                    break;
                 }
             }
             if (received > 0)
             {
                 CSectionLock lock(m_cs_receive);
                 //OUTPUT_BYTES(m_recive_buffer.getData(), received, received, "received from net");
                 m_receive_data.write(m_recive_buffer.getData(), received);
             }
             if (received > 0)
                 sendEvent(NE_NEWDATA);
             if (received == 0)
             {
                sendEvent(NE_DISCONNECT);
                break;
             }
        }

        // send data
        {
            CSectionLock lock(m_cs_send);
            if (m_send_data.getSize() > 0)
            {
                int sended = ::send(sock, (const char*)m_send_data.getData(), m_send_data.getSize(), 0);
                if (sended == SOCKET_ERROR)
                {
                    sendEvent(NE_DISCONNECT);
                    break;
                }
                //OUTPUT_BYTES(m_send_data.getData(), sended, 32, "sended to net");
                m_send_data.truncate(sended);
            }
        }
    }
}