c++企业级接口框架(二)

c++企业级接口框架(二)

SocketProtocol.h

#ifndef _SOCKET_PROTOCOL_H_
#define _SOCKET_PROTOCOL_H_

#include <stdint.h>

namespace NS_SocketProtocol {
    /**
    说明:接口*/
    class SocketProtocol {
    public:
        virtual bool cltSocketInit () = 0;
        virtual bool cltSocketSend (const uint8_t *BufIn, uint32_t iBuflenIn) = 0;
        virtual bool cltSocketRecv (uint8_t *BufOut, uint32_t *iBuflenOut) = 0;
        virtual bool cltSocketDestory () = 0;
    };
}

#endif


SocketImp_1.h

#ifndef _SOCKET_IMP_1_H_
#define _SOCKET_IMP_1_H_

#include "SocketProtocol.h"

namespace NS_SocketProtocol {
    class SocketImp_1 :public SocketProtocol {
        uint8_t *m_pBuffer;
        uint32_t m_iLen;
    public:
        SocketImp_1 ();
        ~SocketImp_1 ();
        bool cltSocketInit ();
        bool cltSocketSend (const uint8_t *BufIn, uint32_t iBuflenIn);
        bool cltSocketRecv (uint8_t *BufOut, uint32_t *iBuflenOut);
        bool cltSocketDestory ();
    };
}

#endif


SocketImp_1.cpp

#include "SocketImp_1.h"
#include <string>

namespace NS_SocketProtocol {
    SocketImp_1::SocketImp_1 () 
        :
    m_pBuffer (NULL),
        m_iLen (0){
            m_pBuffer = (uint8_t *)malloc (100*sizeof (uint8_t));
            memset (m_pBuffer, 0, 100*sizeof (uint8_t));
    }

    SocketImp_1::~SocketImp_1 (){

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

    bool SocketImp_1::cltSocketInit () {
        memset (m_pBuffer, 0, 100*sizeof (uint8_t));
        m_iLen = 0;
        return true;
    }

    bool SocketImp_1::cltSocketSend (const uint8_t *BufIn, uint32_t iBuflenIn) {

        if (BufIn == NULL ||iBuflenIn <= 0) {
            return false;
        }

        memcpy (m_pBuffer, BufIn, iBuflenIn);
        m_iLen = iBuflenIn;

        return true;
    }

    bool SocketImp_1::cltSocketRecv (uint8_t *BufOut, uint32_t *iBuflenOut) {

        if (m_pBuffer == NULL ||m_iLen <= 0) {
            return false;
        }

        *iBuflenOut = m_iLen;
        memcpy (BufOut, m_pBuffer, m_iLen);

        return true;
    }

    bool SocketImp_1::cltSocketDestory () {
        free (m_pBuffer);
        m_pBuffer = NULL;
        return true;
    }
}


SocketImp_2.h

#ifndef _SOCKET_IMP_2_H_
#define _SOCKET_IMP_2_H_

#include "SocketProtocol.h"

namespace NS_SocketProtocol {
    class SocketImp_2 :public SocketProtocol {
        uint8_t *m_pBuffer;
        uint32_t m_iLen;
    public:
        SocketImp_2 ();
        ~SocketImp_2 ();
        bool cltSocketInit ();
        bool cltSocketSend (const uint8_t *BufIn, uint32_t iBuflenIn);
        bool cltSocketRecv (uint8_t *BufOut, uint32_t *iBuflenOut);
        bool cltSocketDestory ();
    };
}

#endif


SocketImp_2.cpp

#include "SocketImp_2.h"
#include <string>

namespace NS_SocketProtocol {
    SocketImp_2::SocketImp_2 () 
        :
    m_pBuffer (NULL),
        m_iLen (0){
            m_pBuffer = (uint8_t *)malloc (100*sizeof (uint8_t));
            memset (m_pBuffer, 0, 100*sizeof (uint8_t));
    }

    SocketImp_2::~SocketImp_2 (){

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

    bool SocketImp_2::cltSocketInit () {
        memset (m_pBuffer, 0, 100*sizeof (uint8_t));
        m_iLen = 0;
        return true;
    }

    bool SocketImp_2::cltSocketSend (const uint8_t *BufIn, uint32_t iBuflenIn) {

        if (BufIn == NULL ||iBuflenIn <= 0) {
            return false;
        }

        memcpy (m_pBuffer, BufIn, iBuflenIn);
        m_iLen = iBuflenIn;

        return true;
    }

    bool SocketImp_2::cltSocketRecv (uint8_t *BufOut, uint32_t *iBuflenOut) {

        if (m_pBuffer == NULL ||m_iLen <= 0) {
            return false;
        }

        *iBuflenOut = m_iLen;
        memcpy (BufOut, m_pBuffer, m_iLen);

        return true;
    }

    bool SocketImp_2::cltSocketDestory () {
        free (m_pBuffer);
        m_pBuffer = NULL;
        return true;
    }
}


main.cpp

#include <iostream>
#include <stdint.h>
#include "SocketProtocol.h"
#include "SocketImp_1.h"
#include "SocketImp_2.h"

using namespace std;

namespace OP {
    using namespace NS_SocketProtocol;
    /**
    说明:业务框架*/
    class OP_1 {
        SocketProtocol *m_Sp;
    public:

        /**
        说明:强关联*/
        bool setSp (const SocketProtocol *objSp) {
            m_Sp = const_cast <SocketProtocol *>(objSp);
            return true;
        }

        /**
        说明:主业务1*/
        bool MainOP_1 (const uint8_t *pBufIn, int iBuflenIn, uint8_t *pBufOut, uint32_t *pLenOut) {

            /*声明变量*/
            bool isSuccess = false;

            isSuccess = m_Sp->cltSocketInit ();
            isSuccess = m_Sp->cltSocketSend (pBufIn, iBuflenIn);
            isSuccess = m_Sp->cltSocketRecv (pBufOut, pLenOut);
            isSuccess = m_Sp->cltSocketDestory ();

            return isSuccess;
        }

        /**
        说明:弱关联*/
        bool MainOP_2 (const SocketProtocol *objSpIn, const uint8_t *pBufIn, uint32_t iBuflenIn, uint8_t *pBufOut, uint32_t *pLenOut) {

            /*声明变量*/
            bool isSuccess = false;

            isSuccess = const_cast <SocketProtocol *> (objSpIn)->cltSocketInit ();
            isSuccess = const_cast <SocketProtocol *> (objSpIn)->cltSocketSend (pBufIn, iBuflenIn);
            isSuccess = const_cast <SocketProtocol *> (objSpIn)->cltSocketRecv (pBufOut, pLenOut);
            isSuccess = const_cast <SocketProtocol *> (objSpIn)->cltSocketDestory ();

            return isSuccess;
        }

    };
}

int main () {

    /*声明变量*/
    OP::OP_1 objOP_1;
    //SocketProtocol *objSocket = new SocketImp_1 ();
    NS_SocketProtocol::SocketProtocol *objSocket_1 = new NS_SocketProtocol::SocketImp_2 ();
    NS_SocketProtocol::SocketProtocol *objSocket_2 = new NS_SocketProtocol::SocketImp_2 ();
    uint8_t pBufIn[100] = "nihao! shijie!";
    uint8_t pBufOut[100] = {};
    uint32_t iBufLenOut = 0;

    objOP_1.setSp (objSocket_1);  //注入
    objOP_1.MainOP_1 (pBufIn, 100, pBufOut, &iBufLenOut);

    objOP_1.MainOP_2 (objSocket_2, pBufIn, 100, pBufOut, &iBufLenOut);

    cout << pBufOut << endl;
    cout << iBufLenOut << endl;

    system ("pause");
}



  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值