Chinaunix首页 | 论坛 | 博客
  • 博客访问: 544488
  • 博文数量: 150
  • 博客积分: 5010
  • 博客等级: 大校
  • 技术积分: 1861
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-17 00:19
文章分类

全部博文(150)

文章存档

2011年(1)

2009年(14)

2008年(135)

我的朋友

分类: LINUX

2009-03-03 16:35:18

背景是一个线程要去取消在它执行之前的线程,让另一个线程不再执行。这个控制思路也是梁总想出来的。



/*==================================================================================================
    FUNCTION: ExecutionThreadHandler()
    
    DESCRIPTION: This handler is responsible for handling the socket conneting operation.
    
    ARGUMENTS PASSED:
     client_fd    
                        
    RETURN VALUE:
     void
    
    IMPORTANT NOTES:
     None
==================================================================================================*/

void ExecutionThreadHandler(int client_fd)
{
   UINT8     rev_buff[20];
   int         res_header=0;
   int         res_data=0;
   UINT8     *data_buff=NULL;
   UINT8     *process_buffer=NULL;
   int         i;
   static TC_TCMD_T     tcmd_get;
   TC_TCMD_T *             tcmd = &tcmd_get;
   

   TCMD_TRACE("ExecutionThreadHandler \n");
   while( !g_tcmd_vars.exitFlag){
     TCMD_TRACE("-----------------------Borqs_TCMD_channel_api begin\n");
    
     res_header= tc_recv_socket(client_fd,rev_buff,sizeof(TC_CMD_REQ_HDR_T));
     if(res_header == -1 ){         
          TCMD_TRACE("The receive error\n");
         break;
     }
     else if(res_header == 0 ){         
          TCMD_TRACE("Unconnect or receive length is 0\n");//notice

         break;
     }
     TCMD_TRACE("----tc_recv_socket----------header length---%d\n",res_header);
    
     //obtain length of data area

     memcpy(&(tcmd_get.cmd_header), rev_buff, sizeof(TC_CMD_REQ_HDR_T));
     TCMD_TRACE("----tc_recv_socket----------header opcode---%x\n",tcmd->cmd_header.opcode);
     TCMD_TRACE("----tc_recv_socket----------cmd_header.length ---%d\n",tcmd->cmd_header.length);
     //receive data area

     if(tcmd->cmd_header.length>0){
     data_buff = (UINT8*)malloc( tcmd->cmd_header.length );
        if( data_buff == NULL){
         TCMD_TRACE( "Alloc data_buff memory failed\n" );
         break;
        }
        res_data= tc_recv_socket(client_fd,data_buff,tcmd->cmd_header.length);
     if(res_data ==-1 ){         
          TCMD_TRACE("The receive data length error\n");
         break;
        }
     else if(res_data == 0 ){         
          TCMD_TRACE("Unconnect while receive TCMD data \n");//notice

         break;
     }
     TCMD_TRACE("----tc_recv_socket----------data length---%d\n",res_data);
     }
    
     //obtain the char array of opcode

     process_buffer = (UINT8*)malloc( res_data+res_header );
     if( process_buffer == NULL){
         TCMD_TRACE( "Alloc process_buffer memory failed\n" );
         break;
     }
     memcpy(process_buffer,rev_buff,res_header);
     memcpy(process_buffer+res_header,data_buff,res_data);
     if(res_header >0 ){
       TCMD_TRACE("The receive command length is :%d \n",res_data+res_header);
       TCMD_TRACE("The client fd is %d: \n",client_fd);
   
       Borqs_TCMD_channel_api(process_buffer,res_data+res_header,client_fd);
     TCMD_TRACE("-----------------------Borqs_TCMD_channel_api over\n");
     }
   }
   
   for (i = 0; i < g_tcmd_vars.connect_num; i++)
   {
        if (g_tcmd_vars.connected_socks[i] == client_fd)
        {                 
         TCMD_TRACE("++++++++++++++++++++++++++++ExecutionThreadHandler process sockets\n");
         TCMD_TRACE("g_tcmd_vars.connect_num=%d+++++++++++++++++++++client_fd=%d+++++++++++++++++++++++++++++\n",g_tcmd_vars.connect_num,client_fd);
         TCMD_TRACE("g_tcmd_vars.connected_socks[%d]=%d++++++++++++++++++++++++++++++++++++++++++++++++++%d\n",i,g_tcmd_vars.connected_socks[i]);
    
         g_tcmd_vars.connected_socks[i] =g_tcmd_vars.connected_socks[(g_tcmd_vars.connect_num)--];
         TCMD_TRACE("After ExecutionThreadHandler g_tcmd_vars.connect_num=%d++++++++++++++++++++++++++++++++++++++++++++++++++\n",g_tcmd_vars.connect_num);
         TCMD_TRACE("After Response g_tcmd_vars.connected_sockes[%d]=%d++++++++++++++++++++++++++++++++++++++++++++++++++\n",i,g_tcmd_vars.connected_socks[i]);
         break;
         }
                
    }

    /* reset thread_id array */
    for (i = 0; i < g_tcmd_vars.thread_num; i++)
    {
        g_tcmd_vars.thread_id[i]=0;
                
    }
    g_tcmd_vars.thread_num=0;
    
    TCMD_TRACE("-----------------------ExecutionThreadHandler over\n");
    close(client_fd);
    TCMD_TRACE("-----------------------ExecutionThreadHandler close client_fd \n");
    pthread_exit((void*)0);  

}

1 注意循环是怎样退出的:(通过break 越简单的越不易忘)
2   Borqs_TCMD_channel_api(process_buffer,res_data+res_header,client_fd);
是同步的,如果它执行不完是不会到达下一次循环的起点:res_header= tc_recv_socket(client_fd,rev_buff,sizeof(TC_CMD_REQ_HDR_T));从而接收数据。这一个我还给梁总说,能接收,是梁总问,如果api函数不返回呢??从而就不能接收下一个opcode的过来数据。
3:pthread_detach()函数让api函数中的一个线程去异步了。
4修改了:
M      engine/TC_CtrlNet.c----------异步的修改
M      engine/TC_CtrlThread.c-------自己线程的
M      engine/TC_Main.c
M      engine/TC_HandlerTable.c
M      lib/Driver/arch/Tavor/TC_Empty.c
M      lib/Driver/arch/Tavor/TC_KeyMonitor.c
M      api/TC_Tcmd.h
M      api/API_test_main.c
M      inc/TC_Handler.h
M      inc/TC_Tcmd.h----数据结构修改:
    +       /*for cancel thread*/
    +       pthread_t                thread_id[DEFAULT_NUM];//max is 50
    +       INT32                    thread_num;
文件:tcmd.rar
大小:347KB
下载:下载
文件:test_client.rar(发三个命令,中间没有接收,最后sleep 20s)
大小:5KB
下载:下载()

/*================================================================================
Module Name: test_api_main.c

General Description: Test tcmd api lib.

==================================================================================
                      borqs Confidential Proprietary
                  Template ID and version: TMP_LFC_50085 Version 1.1
                (c) Copyright Motorola 2004, All Rights Reserved

Revision History:
                            Modification Tracking
Author (core ID) Date Number Description of Changes
------------------------ ------------ ---------- -------------------------
Yadong Zhu 06/12/2008 Created

Portability: Indicate if this module is portable to other compilers or
platforms. If not, indicate specific reasons why is it not portable.

==================================================================================
                                 INCLUDE FILES
================================================================================*/


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "TC_Net.h"
#include <time.h>

/*================================================================================
                                LOCAL CONSTANTS
================================================================================*/


/*================================================================================
                   LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
================================================================================*/


/*================================================================================
                                  LOCAL MACROS
================================================================================*/


/*================================================================================
                            LOCAL FUNCTION PROTOTYPES
================================================================================*/


/*================================================================================
                                LOCAL VARIABLES
================================================================================*/


/*================================================================================
                                GLOBAL VARIABLES
================================================================================*/


/*================================================================================
                            LOCAL POINTER DECLARATIONS
================================================================================*/


/*================================================================================
                                LOCAL FUNCTIONS
================================================================================*/


unsigned char *hexstr_to_hex(int length, const char *hexstr);
/*================================================================================
                               GLOBAL FUNCTIONS
================================================================================*/

#define counter_loop 3
int main(int argc, char **argv)
{
    int ret = -1;
    int socket = -1;
    unsigned char res_code;
    unsigned int res_length;
    unsigned char res_data_buff[300];
    unsigned short op_code = 0;
    unsigned int length = 0;
    unsigned char *data_buff = NULL;
    unsigned int bytes_length;
    int seconds=5;
    time_t old;
    time_t new;
    
    memset(res_data_buff,0,300);
    
    //added bu lizhenqing,so can connect once and send twice

    int loop_counter=0;

    switch(argc)
    {
    case 1:
     printf("%s use <>:\n", argv[0]);
        printf("%s opcode [length command_string]\n", argv[0]);
     exit(0);
        break;
    case 2:
        op_code = atoi(argv[1]);
        break;
    case 3:
        printf("%s opcode length command_string\n", argv[0]);
        exit(0);
        break;
    case 4:
        printf("argv[1] %s\n", argv[1]);
        op_code = atoi(argv[1]);
        if(op_code >= 0xE00)
        {
            printf("no this command\n");
            exit(0);
        }
        length = atoi(argv[2]);
        printf("argv[2] %s\n", argv[2]);
        if(length == 0)
        {
            printf("please input command length\n");
            exit(0);
        }
        printf("argv[3] %s\n", argv[3]);
         data_buff = hexstr_to_hex(length, argv[3]);
         bytes_length = (length % 2) + (length / 2);
        break;
    }

    ret = tc_client_socket(DEFAULTPORT1);
    if(ret == -1)
    {
        printf("start tcmd client failured\n");
        free(data_buff);
        exit(1);
    }
    socket = ret;
  while( loop_counter++ < counter_loop ){
      printf("------------------send loop %d\n",loop_counter);
    switch(argc)
    {
    case 2:
        if(loop_counter==1)
        {
          ret = tc_api_send_socket(socket, op_code, 0, NULL);
        }
        else if(loop_counter==2)
        {
             ret = tc_api_send_socket(socket, 41, 0, NULL);//empty handler

        }
        else if(loop_counter==3)
        {
             ret = tc_api_send_socket(socket, 48, 0, NULL);//baterray

        }
        else
        {
        }
        break;
    case 4:
        ret = tc_api_send_socket(socket, op_code, bytes_length, data_buff);
        break;
    }
    if(ret == -1)
    {
        printf("send tcmd command failured\n");
        free(data_buff);
        exit(1);
    }
    /*
    ret= tc_recv_socket(socket,(void*)res_data_buff,300);
    //ret = TCMD_API_Recv_Command(socket, &res_code, &res_length, (void*)&res_data_buff);

    if(ret == -1)
    {
        printf("receive tcmd command response failured\n");
        free(data_buff);
        exit(1);
    }
    
    printf("=======receive response length %d\n", ret);
    res_data_buff[ret]='\0';
    if(ret > 0)
    {
        int i = 0;
        printf("==============receive response data buff\n");
        printf("==============receive response data buff%s:\n" , res_data_buff);
        for(i = 0; i < ret; i++)
        {
            printf("%x", res_data_buff[i]);
        }
        printf("\n");
    }
   */

 
    //printf("sleep 2 seconds\n");

    /*sleep(1);
     time(&old);
     time(&new);
     while(old+seconds>new){
     printf("sleep 5 seconds\n");
     time(&new);
    }
    */

    
    /*
    sleep(2);
    printf("sleep 3 seconds\n");
    sleep(1);
    sleep(1);
    sleep(1);
    printf("sleep 3seconds\n");
    sleep(1);
    sleep(1);
    sleep(1);
    */

}
    printf("sleep 20 seconds before close socket\n");
    sleep(20);
    free(data_buff);
    printf("=close socket\n");
    close(socket);
    exit(0);
}


unsigned char *hexstr_to_hex(int length, const char *hexstr)
{
    int j, k;
    unsigned char* buff;
    unsigned char* new_buff;
    int bytes_length;

    k = 0;
    bytes_length = (length % 2) + (length / 2);

    buff =(unsigned char*)malloc(length);
    printf("hex str %s\n", hexstr);
    memcpy(buff, hexstr, length);
    printf("buff str %s\n", buff);

    new_buff = malloc(bytes_length);
    memset(new_buff, 0, bytes_length);

    for(j = length - 1; j >= 0; j--)
    {
        if((k % 2) == 0)
        {
            if(buff[j] >= '0' && buff[j] <= '9')
            {
                new_buff[j/2] = ((buff[j] - 48) & 0x0F);
            } else if(buff[j] >= 'a' && buff[j] <= 'f')
            {
                new_buff[j/2] = ((buff[j] - 97 + 10) & 0x0F);
            } else if(buff[j] >= 'A' && buff[j] <= 'F')
            {
                new_buff[j/2] = ((buff[j] - 65 + 10) & 0x0F);
            } else
            {
                printf("must be hex decime\n");
                free(buff);
                free(new_buff);
                return NULL;
            }
            if( j > 0)
            {
                if(buff[j - 1] >= '0' && buff[j - 1] <= '9')
                {
                    new_buff[j/2] |= (((buff[j-1] - 48) << 4) & 0xF0);
                } else if(buff[j - 1] >= 'a' && buff[j - 1] <= 'f')
                {
                    new_buff[j/2] |= (((buff[j-1] - 97 + 10) << 4) & 0xF0);
                } else if(buff[j - 1] >= 'A' && buff[j - 1] <= 'F')
                {
                    new_buff[j/2] |= (((buff[j-1] - 65 + 10) << 4) & 0xF0);
                } else
                {
                    printf("must be hex decime\n");
                    free(buff);
                    free(new_buff);
                    return NULL;
                }
            }
        }
        if((k % 2) == 1)
        {
            k++;
            continue;
        }
        k++;
    }

    printf("%s\n", buff);
    for(k = 0; k < bytes_length; k++)
    {
        printf("%x\n", new_buff[k]);
    }
    return new_buff;
}

阅读(1376) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~