Exemple #1
0
int
SingleThreadedAcceptSocket(int listenSocketNum)
{
    struct sockaddr_in       sin;
    int desc = -1;

    // Wait for the socket to become available on the other side.
    do
    {
#ifdef HAVE_SOCKLEN_T
        socklen_t len;
#else
        int len;
#endif
        len = sizeof(struct sockaddr);
        desc = accept(listenSocketNum, (struct sockaddr *)&sin, &len);
#if defined(_WIN32)
        const char *mName = "SharedDaemon, SingleThreadedAcceptSocket: ";
        if(desc == INVALID_SOCKET)
            LogWindowsSocketError(mName, "accept");
#endif
    }
    while (desc == -1);

    return desc;
}
int
SingleThreadedAcceptSocket(int listenSocketNum)
{
    struct sockaddr_in       sin;
    int desc = -1;

    // Wait for the socket to become available on the other side.
    struct timeval timeout;
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;

    if (setsockopt (listenSocketNum, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
                sizeof(timeout)) < 0)
        printf("setsockopt failed\n");

    if (setsockopt (listenSocketNum, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
                sizeof(timeout)) < 0)
        printf("setsockopt failed\n");

    //do
    {
#ifdef HAVE_SOCKLEN_T
        socklen_t len;
#else
        int len;
#endif
        len = sizeof(struct sockaddr);
        desc = accept(listenSocketNum, (struct sockaddr *)&sin, &len);
#if defined(_WIN32)
        const char *mName = "SharedDaemon, SingleThreadedAcceptSocket: ";
        if(desc == INVALID_SOCKET)
            LogWindowsSocketError(mName, "accept");
#endif
    }
    //while (desc == -1);

    if(desc == -1)
    {
        std::cerr << "Client failed to connect in time" << std::endl;
        EXCEPTION0(CouldNotConnectException);
    }

    return desc;
}
int
SocketConnection::Fill()
{
    if(destFormat.Format == TypeRepresentation::ASCIIFORMAT)
    {
        std::string xmlString = "";

        char tmp[1001]; //leave 1 for null termination//

        int amountRead = 0;
        do
        {

#if defined(_WIN32)
            int amountRead = recv(descriptor, (char FAR *)tmp, 1000, 0);
            if(amountRead == SOCKET_ERROR)
            {
                LogWindowsSocketError("SocketConnection", "Fill");
                if(WSAGetLastError() == WSAEWOULDBLOCK)
                    return -1;
            }
#else
            amountRead = recv(descriptor, (void *)tmp, 1000, 0);
#endif

            if(amountRead > 0)
            {
                zeroesRead = 0;
                tmp[amountRead] = 0;
                xmlString += tmp;
            }

            ++zeroesRead;

            // If we have had a certain number of zero length reads in a row,
            // assume the connection died.
            if(zeroesRead > 100)
            {
                 EXCEPTION0(LostConnectionException);
            }
        }while(amountRead == 1000); //if it gets entire list..
        if(xmlString.size() > 0)
        {
            JSONNode node;
            node.Parse(xmlString);

            //std::cout << node.ToString() << std::endl;

            int guido = node["id"].GetInt();

            JSONNode contents = node["contents"];
//            JSONNode metadata = node["typeinfo"];

            /// With the information I have I could probably
            /// just use JSONNode to convert completely..
            /// but that would leave MapNode incomplete..

            MapNode mapnode(contents,false);

            //std::cout << mapnode.ToXML(false) << std::endl;
            //std::cout << metadata["data"] << std::endl;

            buffer.clear();
            return Write(guido,&mapnode); //,&metadata["data"]
        }

        return 0;
    }

    unsigned char tmp[1000];
#if defined(_WIN32)
    int amountRead = recv(descriptor, (char FAR *)tmp, 1000, 0);
    if(amountRead == SOCKET_ERROR)
    {
        LogWindowsSocketError("SocketConnection", "Fill");
        if(WSAGetLastError() == WSAEWOULDBLOCK)
            return -1;
    }
#else
    int amountRead = recv(descriptor, (void *)tmp, 1000, 0);
#endif

    if(amountRead > 0)
    {
        zeroesRead = 0;

        // Add the new bytes to the buffer.
        for(int i = 0; i < amountRead; ++i)
            buffer.push_back(tmp[i]);
    }
    else
        ++zeroesRead;

    // If we have had a certain number of zero length reads in a row,
    // assume the connection died.
    if(zeroesRead > 100)
    {
         EXCEPTION0(LostConnectionException);
    }

    return amountRead;
}