示例#1
0
static void evh( wip_event_t *ev, void *ctx) {

    switch( ev->kind) {

    case WIP_CEV_OPEN: {
        wip_debug ("[SAMPLE] Connection established successfully\n");
        break;
    }
    case WIP_CEV_READ: {
        int nread;
        wip_debug ("[SAMPLE] Some data arrived\n");
        nread = wip_read( ev->channel, rcv_buffer + rcv_offset,
                          sizeof( rcv_buffer) - rcv_offset);
        if( nread < 0) {
            wip_debug( "[SAMPLE] read error %i\n", nread);
            return;
        }
        rcv_offset += nread;
        if( rcv_offset == sizeof( rcv_buffer)) {
            wip_debug( "[SAMPLE] Reception capacity exceeded, won't read more\n");
        } else {
            wip_debug( "[SAMPLE] Wrote %i bytes of data from network to rcv_buffer. "
                       "%i bytes remain available in rcv_buffer\n",
                       nread, sizeof( rcv_buffer) - rcv_offset);
        }
        break;
    }
    case WIP_CEV_WRITE: {
        int nwrite;
        wip_debug ("[SAMPLE] Can send more data\n");
        nwrite = wip_write( ev->channel, snd_buffer + snd_offset,
                            sizeof( snd_buffer) - snd_offset);
        if( nwrite < 0) {
            wip_debug( "[SAMPLE] write error %i\n", nwrite);
            return;
        }
        snd_offset += nwrite;
        if( snd_offset == sizeof( snd_buffer)) {
            wip_debug( "[SAMPLE] Everything has been sent, won't send more.\n");
        } else {
            wip_debug( "[SAMPLE] Wrote %i bytes. "
                       "%i bytes left to send in snd_buffer\n",
                       nwrite, sizeof( snd_buffer) - snd_offset);
        }
        break;
    }
    case WIP_CEV_ERROR: {
        wip_debug( "[SAMPLE] Error %i on socket. Closing.\n",
                   ev->content.error.errnum);
        wip_close( ev->channel);
        break;
    }
    case WIP_CEV_PEER_CLOSE: {
        wip_debug( "[SAMPLE] Connection closed by peer\n");
        wip_close( ev->channel);
        break;
    }
    }
}
示例#2
0
static void http_ClientTestDataHandler ( wip_event_t *ev, void *ctx )
{
    ascii tmpbuf [ 256 ];
    ascii Ptr_OnTrace [ 240 ];
    int len, tmplen;
    s32 status;
    http_ClientTestCtx_t *pHttpClientCtx = ( http_ClientTestCtx_t * ) &http_ClientTestCtx;

    switch ( ev->kind )
    {
        case WIP_CEV_OPEN:
        {
            TRACE ( ( NORMAL_TRACE_LEVEL, "http_ClientTestDataHandler: WIP_CEV_OPEN" ) );
            adl_atSendResponse ( ADL_AT_UNS, "http_ClientTestDataHandler: Start\r\n" );
            /* ready for getting response data  */
            pHttpClientCtx->dataLength = 0;
            break;
        }

        case WIP_CEV_READ:
        {
            TRACE ( ( NORMAL_TRACE_LEVEL, "http_ClientTestDataHandler: WIP_CEV_READ" ) );
            /* we must read all available data to trigger WIP_CEV_READ again    */
            tmplen = 0;
            while ( ( len = wip_read ( ev->channel, tmpbuf, sizeof ( tmpbuf ) - 1 ) ) > 0 )
            {
                tmpbuf [ len ] = 0;
                //adl_atSendResponse ( ADL_AT_UNS, tmpbuf );
                tmplen += len;
            }
            TRACE(( NORMAL_TRACE_LEVEL, "http_ClientTestDataHandler: read %d bytes", tmplen ));
            /* compute total length of response */
            pHttpClientCtx->dataLength += tmplen;
            break;
        }

        case WIP_CEV_PEER_CLOSE:
        {
            TRACE ( ( NORMAL_TRACE_LEVEL, "http_ClientTestDataHandler: WIP_CEV_PEER_CLOSE" ) );
            adl_atSendResponse ( ADL_AT_UNS, "\r\nhttp_ClientTestDataHandler: Done\r\n" );

            /* end of data  */
            /* show response information    */
            if ( wip_getOpts ( ev->channel,
                               WIP_COPT_HTTP_STATUS_CODE, &status,
                               WIP_COPT_HTTP_STATUS_REASON, tmpbuf, sizeof ( tmpbuf ),
                               WIP_COPT_END ) == OK )
            {
                ascii sbuf [ 16 ];
                adl_atSendResponse ( ADL_AT_UNS, "http_ClientTestDataHandler: Status=" );
                wm_sprintf ( sbuf, "%d", ( s16 ) status );
                adl_atSendResponse ( ADL_AT_UNS, sbuf );
                adl_atSendResponse ( ADL_AT_UNS, "\r\nhttp_ClientTestDataHandler: Reason=\"" );
                adl_atSendResponse ( ADL_AT_UNS, tmpbuf );
                adl_atSendResponse ( ADL_AT_UNS, "\"\r\n" );
                TRACE ( ( NORMAL_TRACE_LEVEL, "http_ClientTestDataHandler: Status=%d", status ) );
                wm_sprintf ( Ptr_OnTrace, "http_ClientTestDataHandler: Reason=\"%s\"", tmpbuf );
                TRACE ( ( NORMAL_TRACE_LEVEL, Ptr_OnTrace ) );
            }

            if ( wip_getOpts ( ev->channel,
                               WIP_COPT_HTTP_HEADER, "content-type", tmpbuf, sizeof ( tmpbuf ),
                               WIP_COPT_END ) == OK )
            {
                wm_sprintf ( Ptr_OnTrace, "http_ClientTestDataHandler: Content Type=\"%s\"\n", tmpbuf );
                TRACE ( ( NORMAL_TRACE_LEVEL, Ptr_OnTrace ) );
            }
            TRACE ( ( NORMAL_TRACE_LEVEL, "http_ClientTestDataHandler: Response Length=%d bytes", pHttpClientCtx->dataLength ) );

            /* data channel must be closed*/
            wip_close( ev->channel );

            break;
        }

        case WIP_CEV_ERROR:
        {
            TRACE ( ( ERROR_TRACE_LEVEL, "http_ClientTestDataHandler: WIP_CEV_ERROR %d", ev->content.error.errnum ) );
            adl_atSendResponse ( ADL_AT_UNS, "http_ClientTestDataHandler: ERROR\r\n" );
            /* connection to server broken  */
            wip_close( ev->channel);
            break;
        }

        default:
        {
            TRACE ( ( ERROR_TRACE_LEVEL, "http_ClientTestDataHandler: unexpected event: %d", ev->kind ) );
            break;
        }
    }
}