Example #1
0
void HTTPProc(clientParams* cParams)
{
	assembleBuffer(cParams);
	cParams->parser->parseRequest(cParams->task);
	cParams->task->runner->run(cParams->task);
	releaseClient(cParams);
}
Example #2
0
/////////////////////////////////////////////////////////////////
/// Writes data to the socket.
///
/// Clients can call this method to write data to the socket.
///\param sockfd Socket file descripter
///\param flaWri Communication flag to write to the socket stream.
///\param nDblWri Number of double values to write.
///\param nIntWri Number of integer values to write.
///\param nBooWri Number of boolean values to write.
///\param curSimTim Current simulation time in seconds.
///\param dblValWri Double values to write.
///\param intValWri Integer values to write.
///\param boolValWri Boolean values to write.
///\sa int establishclientsocket(uint16_t *portNo)
///\return The exit value of \c send, or a negative value if an error occured.
int writetosocket(const int *sockfd,
                  const int *flaWri,
                  const int *nDblWri, const int *nIntWri, const int *nBooWri,
                  double *curSimTim,
                  double dblValWri[], int intValWri[], int booValWri[])
{
    int retVal;
    // buffer used to exchange data
    char *buffer;
    int bufLen = REQUIRED_WRITE_LENGTH;

#ifdef NDEBUG
    if (f1 == NULL) // open file
        f1 = fopen ("utilSocket.log", "w");
    if (f1 == NULL) {
        fprintf(stderr, "Cannot open file %s\n", "utilSocket.log");
        return -1;
    }
#endif

    /////////////////////////////////////////////////////
    // make sure that the socketFD is valid
    if (*sockfd < 0 ) {
        fprintf(stderr, "Error: Called write to socket with negative socket number.\n");
        fprintf(stderr, "       sockfd : %d\n",  *sockfd);
#ifdef NDEBUG
        fprintf(f1, "Error: Called write to socket with negative socket number.\n");
        fprintf(f1, "       sockfd : %d\n",  *sockfd);
        fflush(f1);
#endif
        return -1; // return a negative value in case of an error
    }

    /////////////////////////////////////////////////////
    // allocate storage for buffer
#ifdef NDEBUG
    fprintf(f1, "Assembling buffer.\n", *sockfd);
#endif
    buffer = malloc(bufLen);
    if (buffer == NULL) {
        perror("malloc failed in writetosocket.");
#ifdef NDEBUG
        fprintf(f1, "malloc failed in writetosocket.\n");
#endif
        return -1;
    }
    //////////////////////////////////////////////////////
    // copy arguments to buffer
    retVal = assembleBuffer(*flaWri, *nDblWri, *nIntWri, *nBooWri,
                            *curSimTim,
                            dblValWri, intValWri, booValWri,
                            &buffer, &bufLen);

    if (retVal != 0 ) {
        fprintf(stderr, "Error: Failed to allocate memory for buffer before writing to socket.\n");
        fprintf(stderr, "       retVal : %d\n",  retVal);
        fprintf(stderr, "       Message: %s\n",  strerror(errno));
#ifdef NDEBUG
        fprintf(f1, "Error: Failed to allocate memory for buffer before writing to socket.\n");
        fprintf(f1, "       retVal : %d\n",  retVal);
        fprintf(f1, "       Message: %s\n",  strerror(errno));
        fflush(f1);
#endif
        free(buffer);
        return -1; // return a negative value in case of an error
    }
    //////////////////////////////////////////////////////
    // write to socket
#ifdef NDEBUG
    fprintf(f1, "Write to socket with fd = %d\n", *sockfd);
    fprintf(f1, "Buffer        = %s\n", buffer);
#endif

#ifdef _MSC_VER
    retVal = send(*sockfd,buffer,strlen(buffer), 0);
#else
    retVal = write(*sockfd,buffer,strlen(buffer));
#endif

#ifdef NDEBUG
    if (retVal >= 0)
        fprintf(f1, "Wrote %d characters to socket.\n",  retVal);
    else
        fprintf(f1, "Error writing to socket: Return value = %d.\n",  retVal);
#endif
    if (retVal < 0) {
#ifdef NDEBUG
#ifdef _MSC_VER
        fprintf(f1, "Error writing to socket: WSAGetLastError = %d\n", WSAGetLastError());
#else
        fprintf(f1, "Error writing to socket: %s\n",  strerror(errno));
#endif
        fflush(f1);
#endif
    }
    free(buffer);
    return retVal;
}