Esempio n. 1
0
void socketTransfertClient::SocketProc()
{
	char buffer[8192];
	WSABUF wsaBuffer;
	DWORD dwFlag = 0;
	DWORD dwReceviedBytes = 0;

	while (m_isConnected)
	{
		FD_ZERO(&m_readSet);
		FD_SET(m_clientSocket, &m_readSet);

		if (select(0, &m_readSet, nullptr, nullptr, nullptr) == SOCKET_ERROR)
		{
			return;
		}

		if (FD_ISSET(m_clientSocket, &m_readSet))
		{
			FD_CLR(m_clientSocket, &m_readSet);
			wsaBuffer.buf = buffer;
			wsaBuffer.len = 8192;

			if (WSARecv(m_clientSocket, &wsaBuffer, 1, &dwReceviedBytes, &dwFlag, nullptr, nullptr) == SOCKET_ERROR)
			{
				int error = WSAGetLastError();
				if (error == WSAEWOULDBLOCK)
				{
					continue;
				}
				else if (error == WSAECONNRESET)
				{
					m_isConnected = false;
					MasterDisconnect();
					break;
				}
			}
			else
			{
				if (dwReceviedBytes > 0)
				{
					DeserializeData(wsaBuffer.buf);
				}
				else if (dwReceviedBytes == 0)
				{
					m_isConnected = false;
					MasterDisconnect();
					break;
				}
			}

		}
	}
	free(buffer);
}
Esempio n. 2
0
void EC_DynamicComponent::DeserializeCommon(std::vector<DeserializeData>& deserializedAttributes, AttributeChange::Type change)
{
    // Sort both lists in alphabetical order.
    AttributeVector oldAttributes = NonEmptyAttributes();
    std::stable_sort(oldAttributes.begin(), oldAttributes.end(), &CmpAttributeById);
    std::stable_sort(deserializedAttributes.begin(), deserializedAttributes.end(), &CmpAttributeDataById);

    std::vector<DeserializeData> addAttributes;
    std::vector<DeserializeData> remAttributes;
    AttributeVector::iterator iter1 = oldAttributes.begin();
    std::vector<DeserializeData>::iterator iter2 = deserializedAttributes.begin();

    // Check what attributes we need to add or remove from the dynamic component (done by comparing two list differences).
    while(iter1 != oldAttributes.end() || iter2 != deserializedAttributes.end())
    {
        // No point to continue the iteration if other list is empty. We can just push all new attributes into the dynamic component.
        if(iter1 == oldAttributes.end())
        {
            for(;iter2 != deserializedAttributes.end(); ++iter2)
                addAttributes.push_back(*iter2);
            break;
        }
        // Only old attributes are left and they can be removed from the dynamic component.
        else if(iter2 == deserializedAttributes.end())
        {
            for(;iter1 != oldAttributes.end(); ++iter1)
                remAttributes.push_back(DeserializeData((*iter1)->Id()));
            break;
        }

        // Attribute has already created and we only need to update it's value.
        if((*iter1)->Id() == (*iter2).id_)
        {
            //SetAttribute(QString::fromStdString(iter2->name_), QString::fromStdString(iter2->value_), change);
            for(AttributeVector::const_iterator attr_iter = attributes.begin(); attr_iter != attributes.end(); ++attr_iter)
                if((*attr_iter)->Id() == iter2->id_)
                    (*attr_iter)->FromString(iter2->value_.toStdString(), change);

            ++iter2;
            ++iter1;
        }
        // Found a new attribute that need to be created and added to the component.
        else if((*iter1)->Id() > (*iter2).id_)
        {
            addAttributes.push_back(*iter2);
            ++iter2;
        }
        // Couldn't find the attribute in a new list so it need to be removed from the component.
        else
        {
            remAttributes.push_back(DeserializeData((*iter1)->Id()));
            ++iter1;
        }
    }

    while(!addAttributes.empty())
    {
        DeserializeData attributeData = addAttributes.back();
        IAttribute *attribute = CreateAttribute(attributeData.type_, attributeData.id_);
        if (attribute)
            attribute->FromString(attributeData.value_.toStdString(), change);
        addAttributes.pop_back();
    }
    while(!remAttributes.empty())
    {
        DeserializeData attributeData = remAttributes.back();
        RemoveAttribute(attributeData.id_);
        remAttributes.pop_back();
    }
}