⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tsknetwork.c

📁 基于DM642的JPEG图象编解码的网络传输程序
💻 C
字号:
#include <std.h>
#include <tsk.h>
#include <stdio.h>
#include <csl.h>
#include <csl_cache.h>
#include <csl_dat.h>
#include <chan.h>
#include <scom.h>
#include <utl.h>
#include <ialg.h>

#include "appmain.h"
#include "appThreads.h"
#include "appBiosObjects.h"

#include "c:\ti\c6000\ndk\inc\netmain.h"

IPN IpDecodeDefault = 0;
IPN IpDecodePeer    = 0;
IPN IpDecodeConnect = 0;
IPN IpEncodePeer    = 0;
int NetCamLocal = 1;

#pragma DATA_SECTION(rxjpg_img,   ".user_data_ext")
#pragma DATA_ALIGN( rxjpg_img,   128)
static unsigned char rxjpg_img[128 * 2000];


void tskNetworkTx()
{
    SOCKET   slisten = INVALID_SOCKET;
    SOCKET   speer   = INVALID_SOCKET;
    struct   sockaddr_in sin1;
    struct   timeval timeout;           // Timeout struct for select
    int      tmp;
    fd_set   ibits;

    int      jpg_size;
    UINT8    *jpg_buf;
    UINT8    *pFileBuffer;

    ScomMessage *pMsgBuf;
    SCOM_Handle hs_Enc2JPG,hs_JPG2Enc;

    hs_Enc2JPG = SCOM_open("ENC2NET");
    hs_JPG2Enc = SCOM_open("NET2ENC");

    if( !hs_Enc2JPG || !hs_JPG2Enc )
    {
        for(;;);
    }


    // Allocate the file environment for this task
    fdOpenSession( TaskSelf() );

    // Create the main TCP listen socket
    slisten = socket(AF_INET, SOCK_STREAMNC, IPPROTO_TCP);
    if( slisten == INVALID_SOCKET )
        goto leave;

    // Set Port = 2000, leaving IP address = Any
    bzero( &sin1, sizeof(struct sockaddr_in) );
    sin1.sin_family = AF_INET;
    sin1.sin_len    = sizeof( sin1 );
    sin1.sin_port   = htons(2000);

    // Bind socket
    if ( bind( slisten, (PSA) &sin1, sizeof(sin1) ) < 0 )
        goto leave;

    // Start listening
    if ( listen( slisten, 1) < 0 )
        goto leave;

    for(;;)
    {
        // Get a JPEG file
        pMsgBuf = SCOM_getMsg( hs_Enc2JPG, SYS_FOREVER );

        jpg_size = pMsgBuf->sizeLinear;
        jpg_buf  = pMsgBuf->bufLinear;

        // Convert the JPG to a file so NETCAM still works
        if( NetCamLocal && jpg_size > 0 && jpg_size < 256000 )
        {
            pFileBuffer = mmBulkAlloc( jpg_size );
            if( pFileBuffer )
            {
                mmCopy( pFileBuffer, jpg_buf, jpg_size );
                efs_destroyfile( "image1.jpg" );
                efs_createfilecb( "image1.jpg", jpg_size, pFileBuffer,
                                (EFSFUN)mmBulkFree, (UINT32)pFileBuffer );
            }
        }

        // Now, also stream the JPEG file to a connected client (if any)

        // Check for a new connection

        // Clear the select flags
        FD_ZERO(&ibits);

        // We examine the listening TCP socket
        FD_SET(slisten, &ibits);

        // Check for a connection, but don't block
        timeout.tv_sec  = 0;
        timeout.tv_usec = 0;
        tmp = fdSelect( 2, &ibits, 0, 0, &timeout );
        if( tmp && FD_ISSET(slisten, &ibits) )
        {
            if( speer != INVALID_SOCKET )
                fdClose( speer );

            tmp = sizeof(sin1);
            speer = accept( slisten, (PSA)&sin1, &tmp );

            // If connected, get peer name
            if( speer != INVALID_SOCKET )
            {
                tmp = sizeof(sin1);

                // Set timeout so we don't get stuck in send
                timeout.tv_sec  = 5;
                timeout.tv_usec = 0;
                setsockopt( speer, SOL_SOCKET, SO_SNDTIMEO,
                            &timeout, sizeof(timeout) );

                getpeername( speer, &sin1, &tmp );
                IpEncodePeer = sin1.sin_addr.s_addr;

                // If this address is not the same as our decode
                // peer, then change decode peer
                if( IpDecodePeer != IpEncodePeer )
                    IpDecodeConnect = IpEncodePeer;
            }
        }

        if( speer != INVALID_SOCKET && jpg_size > 0 && jpg_size < 256000 )
        {
            // Now, send the encoded file

            // Send the file size
            if( send( speer, (UINT8 *)&jpg_size, 4, 0 ) < 0 )
            {
peer_closed:
               fdClose( speer );
               speer = INVALID_SOCKET;
            }
            else
            {
                // Send the file data
                if( send( speer, jpg_buf, jpg_size, 0 ) < 0 )
                    goto peer_closed;
            }
        }

        // We pulled the JPEG file into cache, so clean it out
        OEMCacheClean( jpg_buf, jpg_size );

        // Synch the cache
        OEMCacheCleanSynch();

        // Tell the encode that we're done with the JPEG file
        SCOM_putMsg( hs_JPG2Enc, pMsgBuf );
    }

leave:
    // We only get here on a fatal error - close the sockets
    if( slisten != INVALID_SOCKET )
        fdClose( slisten );

    printf("tskNetwork Fatal Error\n");

    // This task is killed by the system - here, we block
    TaskBlock( TaskSelf() );
}





void tskNetworkRx()
{
    SOCKET   speer  = INVALID_SOCKET;
    struct   sockaddr_in sin1;
    int      size,tmp,readsize;
    struct linger  lgr;
    struct timeval timeout;

    UINT8    *pFileBuffer;

    ScomMessage scomMsg;
    SCOM_Handle hs_JPG2Dec,hs_Dec2JPG;

    hs_JPG2Dec = SCOM_open("NET2DEC");
    hs_Dec2JPG = SCOM_open("DEC2NET");

    if( !hs_JPG2Dec || !hs_Dec2JPG )
    {
        for(;;);
    }

    TSK_sleep(500);

    //
    // This initialization code goes first
    //

    // Allocate the file environment for this task
    fdOpenSession( TaskSelf() );

    // We'll use sin1 to hold the address to which we
    // want to connect. The Port will be 2000. We fill
    // in the IP address later (once we know it).
    bzero( &sin1, sizeof(struct sockaddr_in) );
    sin1.sin_family = AF_INET;
    sin1.sin_len    = sizeof( sin1 );
    sin1.sin_port   = htons(2000);

    OEMCacheClean( rxjpg_img, sizeof(rxjpg_img) );
    OEMCacheCleanSynch();

    for(;;)
    {
        // If we are connected but need to be connected to someone else,
        // disconnect now
        if( speer != INVALID_SOCKET &&
            IpDecodeConnect && (IpDecodePeer != IpDecodeConnect) )
        {
peer_closed:
            fdClose( speer );
            IpDecodePeer = 0;
            speer = INVALID_SOCKET;
        }

        // If we are not connected and don't have a connect
        // request, then set the IP address of the next connect
        // to be the default address.
        if( speer == INVALID_SOCKET && !IpDecodeConnect )
            IpDecodeConnect = IpDecodeDefault;

        // If we aren't connected and we need to be, connect now
        if( speer == INVALID_SOCKET && IpDecodeConnect )
        {
            // Create the main TCP listen socket
            speer = socket(AF_INET, SOCK_STREAMNC, IPPROTO_TCP);
            if( speer == INVALID_SOCKET )
            if( connect( speer, (PSA) &sin1, sizeof(sin1) ) < 0 )
            {
                // printf("Socket failed (%d)\n",fdError());
                goto peer_closed;
            }

            sin1.sin_addr.s_addr = IpDecodeConnect;
            IpDecodeConnect = 0;
            if( connect( speer, (PSA) &sin1, sizeof(sin1) ) < 0 )
            {
                // printf("Connect failed (%d)\n",fdError());
                goto peer_closed;
            }

            // Set linger to do a hard drop when we close
            lgr.l_onoff  = 1;
            lgr.l_linger = 0;
            setsockopt( speer, SOL_SOCKET, SO_LINGER, &lgr, sizeof( lgr ) );

            // Set timeout so we don't get stuck in recv
            timeout.tv_sec  = 5;
            timeout.tv_usec = 0;
            setsockopt( speer, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout) );

            IpDecodePeer = sin1.sin_addr.s_addr;
        }

        // At this point, if we're not connected, set "size" to zero
        // to tell the decode we do not have a data file. Otherwise;
        // read the data from the peer.
        if( speer == INVALID_SOCKET )
            size = 0;
        else
        {
            readsize = 0;
            while( readsize < 4 )
            {
                tmp = recv( speer, ((UINT8 *)&size)+readsize, 4-readsize, 0 );

                if( tmp < 0 )
                {
                    // printf("Socket Read Error (%d)\n",fdError());
                    goto peer_closed;
                }
                if( !tmp )
                {
                    // printf("Peer Disconnected (recv)\n");
                    goto peer_closed;
                }
                readsize += tmp;
            }

            readsize = 0;
            while( readsize < size )
            {
                tmp = recv( speer, rxjpg_img+readsize, size-readsize, 0 );

                if( tmp < 0 )
                {
                    // printf("Socket Read Error (%d)\n",fdError());
                    goto peer_closed;
                }
                if( !tmp )
                {
                    // printf("Peer Disconnected (recv)\n");
                    goto peer_closed;
                }
                readsize += tmp;
            }
        }

        if( size )
        {
            // Convert the JPG to a file so NETCAM still works
            if( !NetCamLocal )
            {
                pFileBuffer = mmBulkAlloc( size );
                if( pFileBuffer )
                {
                    mmCopy( pFileBuffer, rxjpg_img, size );
                    efs_destroyfile( "image1.jpg" );
                    efs_createfilecb( "image1.jpg", size, pFileBuffer,
                                    (EFSFUN)mmBulkFree, (UINT32)pFileBuffer );
                }
            }

            OEMCacheClean( rxjpg_img, size );
            OEMCacheCleanSynch();

            // Give the JPG file so give to the decoder
            scomMsg.sizeLinear = size;
            scomMsg.bufLinear  = rxjpg_img;
            SCOM_putMsg( hs_JPG2Dec, &scomMsg );

            // Make sure the decoder is done
            SCOM_getMsg( hs_Dec2JPG, SYS_FOREVER );
        }
    }
}



























⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -