xuejianhui

导航

初解禁:供SDK服务端调用函数--01

#include <signal.h>
#include <sys/types.h>
#include "sdk_server.h"
#include "sdk_xmlhandle.h"

/* 全局变量定义 */
INT                   g_iEpollFd;                   /* epoll描述符 */
TCP_SOCKET_S          g_stTcpSock;                  /* Tcp原始套接字*/
LONG                  g_lUnixFd;                    /* 域套接字*/
MUTEX_S               g_UnixWriteMutex;             /* 域套接字写锁 */
CLIENT_INFO_HEADER_S* g_pstClientInfo;              /* 客户映射链表 */
BOOL                  g_UpgradeFlag;                /* 远程升级开关   0:不允许   1:允许*/
UPGRADE_FILE_INFO_S   g_stFileInfo;                 /* 升级文件信息结构*/
//ULONG               g_ulSDKVersion = 0x01000000;  /* SDK版本号,高16位,25位~32位表示主版本号,17~24位表示子版本号*/
                                                    /* 低16位,9位~16位表示修正版本号,1~8位表示预留版本号*/
VW_SDK_SERVER_INFO_S  g_stServerInfo;               /* 平台信息内部结构*/

/****************************************************************************************************
函 数 名:   InitSdkServer
功能描述:   初始化SDK服务端
返 回 值:   成功返回RESULT_SUCCESS,失败返回RESULT_FAIL
*****************************************************************************************************/
LONG InitSdkServer()
{
    LONG lResult = RESULT_SUCCESS;

    /*初始化远程升级开关,默认关闭*/
    g_UpgradeFlag = 0;

    /*初始化远程升级文件信息结构*/
    memset(&g_stFileInfo, 0x00, sizeof(UPGRADE_FILE_INFO_S));

    /*写入文件从第一个开始*/
    g_stFileInfo.uFileSeq = 1;
    g_iEpollFd = InvalidFd;

    /* 初始化套接字变量 */
    memset(&g_stTcpSock, 0x00, sizeof(TCP_SOCKET_S));
    g_lUnixFd = InvalidFd;

    /* 初始化客户映射链表 */
    g_pstClientInfo = NULL;
    g_pstClientInfo = (CLIENT_INFO_HEADER_S*)malloc(sizeof(CLIENT_INFO_HEADER_S));
    if(NULL == g_pstClientInfo)
    {
        WRITE_LOG(LOG_TYPE_DEBUG, LOG_LEVEL_ERR, g_enDebugFlag, 
                    "Initialization g_pstClientInfo error, pointer is NULL!");
        return RESULT_FAIL;
    }
    memset(g_pstClientInfo, 0x00, sizeof(CLIENT_INFO_HEADER_S));
    memset(&g_stServerInfo, 0x00, sizeof(VW_SDK_SERVER_INFO_S));
    memset(g_ServerPassWord, 0x00, SERVER_PASSWORD_LEN);

    //初始化互斥锁
    lResult = (LONG)pthread_mutex_init(&(g_pstClientInfo->stClientInfoMutex.mutex), NULL);
    if( RESULT_SUCCESS != lResult )
    {
        WRITE_LOG(LOG_TYPE_DEBUG, LOG_LEVEL_ERR, g_enDebugFlag, 
                "Initialization ClientInfo Mutex error, lResult = %d!", lResult);
        free(g_pstClientInfo);
        g_pstClientInfo = NULL;
        return RESULT_FAIL;
    }

    //初始化UnixWrite互斥锁
    memset(&g_UnixWriteMutex, 0x00, sizeof(MUTEX_S));
    lResult = (LONG)pthread_mutex_init( &(g_UnixWriteMutex.mutex), NULL);
    if( RESULT_SUCCESS != lResult )
    {
        WRITE_LOG(LOG_TYPE_DEBUG, LOG_LEVEL_ERR, g_enDebugFlag, 
                "Initialization UnixWrite Mutex error, lResult = %d!", lResult);
        free(g_pstClientInfo);
        g_pstClientInfo = NULL;
        return RESULT_FAIL;
    }

#if 0
    //初始化定时器
    lResult = InitIpcTimer(0);
    if(RESULT_SUCCESS != lResult)
    {
        WRITE_LOG(LOG_TYPE_DEBUG, LOG_LEVEL_ERR, g_enDebugFlag, "InitIpcTimer fail!");
        free(g_pstClientInfo);
        g_pstClientInfo = NULL;
        return RESULT_FAIL;
    }

    //启动定时器
    lResult = RunIpcTimer();
    if(RESULT_SUCCESS != lResult)
    {
        WRITE_LOG(LOG_TYPE_DEBUG, LOG_LEVEL_ERR, g_enDebugFlag, "RunIpcTimer fail!");
        free(g_pstClientInfo);
        g_pstClientInfo = NULL;
        return RESULT_FAIL;
    }
#endif
    WRITE_LOG(LOG_TYPE_DEBUG, LOG_LEVEL_INFO, g_enDebugFlag, "Init SdkServer success!");
    return RESULT_SUCCESS;
}

/* 初始化定时器 */
LONG InitIpcTimer(ULONG ulTimeScale)
{
    if(TRUE == SwitchPoint)
    {
        return FAIL;
    }

    TIMER_DESC_S *pNewTimerDesc = NULL;

    /* 设置定时进程开关 */
    SwitchPoint = FALSE;

    /* 判断时间精度 */
    if(0 == ulTimeScale)
    {
        g_ulTimerScale = DefaultTimerScale;

    }
    else if((ulTimeScale > MaxTimerScale) || (ulTimeScale < MinTimerScale))
    {
        return FAIL;
    }
    else
    {
        g_ulTimerScale = ulTimeScale;
    }

    /* 分配定时器描述链表头 */
    g_pTimerHeader = (TIMER_LIST_HEADER_S *)malloc(sizeof(TIMER_LIST_HEADER_S));
    if(NULL == g_pTimerHeader)
    {
        return FAIL;
    }
    bzero(g_pTimerHeader, sizeof(TIMER_LIST_HEADER_S));
    g_pTimerHeader->itNextTimerId = 0;

    /* 分配第一个TIMER_DESC_S,(没有实际意义,只是为了双向链表更好操作) */
    pNewTimerDesc = (TIMER_DESC_S *)malloc(sizeof(TIMER_DESC_S));
    if(NULL == pNewTimerDesc)
    {
        free(g_pTimerHeader);//释放内存
        g_pTimerHeader = NULL;
        return FAIL;
    }
    bzero(pNewTimerDesc, sizeof(TIMER_DESC_S));
    g_pTimerHeader->pHeader = pNewTimerDesc; //链入链表
    /* 初始化头结构体 */
    pNewTimerDesc->itTimerId = g_pTimerHeader->itNextTimerId++; //分配ID号
    pNewTimerDesc->enStatus = enRunning; //状态
    pNewTimerDesc->enStyle = enRepeated; //定时器类型
    pNewTimerDesc->ulPeriod = 0; //周期
    pNewTimerDesc->ulTimeSurplus = 0; //剩余周期
    pNewTimerDesc->tFunc = NULL; //回调函数
    pNewTimerDesc->ptr = NULL; //参数
    pNewTimerDesc->prev = NULL;
    pNewTimerDesc->next = NULL;

    /* 分配互斥锁内存 */
    g_pTimerHeader->pTimerLock = (pthread_mutex_t *)malloc(
            sizeof(pthread_mutex_t));
    if(NULL == g_pTimerHeader->pTimerLock)
    {
        free(g_pTimerHeader->pHeader);//释放内存
        free(g_pTimerHeader);//释放内存
        g_pTimerHeader = NULL;
        return FAIL;
    }

    /* 初始化互斥锁 */
    if(SUCCESS != pthread_mutex_init(g_pTimerHeader->pTimerLock, NULL))/* 互斥锁初始化 */
    {
        free(g_pTimerHeader->pHeader);//释放内存
        free(g_pTimerHeader->pTimerLock);//释放内存
        free(g_pTimerHeader);//释放内存
        g_pTimerHeader = NULL;
        return FAIL;
    }

    return SUCCESS;
}

/* 定时器开始运行 */
LONG RunIpcTimer(VOID)
{
    /* 查看定时器开关状态(已经打开-失败) */
    if(SwitchPoint == TRUE)
    {
        return FAIL;
    }

    /* 设置定时线程开关 */
    SwitchPoint = TRUE;

    /* 创建定时线程 */
    if(SUCCESS != pthread_create(&g_nTid, NULL, s_TimerPthread, NULL)) //创建定时线程
    {
        SwitchPoint = FALSE;
        return FAIL;
    }

    /* 使线程进入分离模式 */
    pthread_detach(g_nTid);

    return SUCCESS;
}

/****************************************************************************************************
函 数 名:   ExitProcess
功能描述:   退出进程
*****************************************************************************************************/
static VOID ExitProcess(INT uSigNo)
{
    WRITE_LOG(LOG_TYPE_CRITICAL, LOG_LEVEL_NONE, DEBUG_FLAG_OPEN, 
                    "SDK Server catch a singal[%d], now ExitProcess", uSigNo);
    exit(0);
}

/****************************************************************************************************
函 数 名:   ReleaseSdkServer
功能描述:   释放SDK服务端
返 回 值:   成功返回RESULT_SUCCESS,失败返回RESULT_FAIL
*****************************************************************************************************/
LONG ReleaseSdkServer()
{
    WRITE_LOG(LOG_TYPE_DEBUG, LOG_LEVEL_INFO, g_enDebugFlag, "Enter function ReleaseSdkServer.");

    if(RESULT_SUCCESS != pthread_mutex_destroy(&(g_pstClientInfo->stClientInfoMutex.mutex)))
    {
        WRITE_LOG(LOG_TYPE_DEBUG, LOG_LEVEL_ERR, g_enDebugFlag, "destroy stClientInfoMutex fail.");
        return RESULT_FAIL;
    }

    if(RESULT_SUCCESS != pthread_mutex_destroy(&g_UnixWriteMutex.mutex))
    {
        WRITE_LOG(LOG_TYPE_DEBUG, LOG_LEVEL_ERR, g_enDebugFlag, "destroy g_UnixWriteMutex fail.");
        return RESULT_FAIL;
    }

    if(NULL != g_pstClientInfo)
    {
        free(g_pstClientInfo);
        g_pstClientInfo = NULL;
    }

    return RESULT_SUCCESS;
}

posted on 2012-11-01 12:16  xuejianhui  阅读(165)  评论(0编辑  收藏  举报