c++企业级接口框架(二)
2016-01-23 19:54
405 查看
c++企业级接口框架(二)
SocketProtocol.h
SocketImp_1.h
SocketImp_1.cpp
SocketImp_2.h
SocketImp_2.cpp
main.cpp
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"); }
相关文章推荐
- 库函数的认识及 C/C++常用 api 及基本库
- 1084. Broken Keyboard (20)
- GCC 下 tranform 调用 toupper, tolower 出错原因及解决
- C++学习笔记(二) bool const 和 #define 结构体内存对齐
- 编程学习笔记之c++相关::模板元的学习
- C++ memcpy()函数用法
- C++ static关键字
- NetBeans找不到C/C++编译器
- C语言:运行中获取宏名字的技巧
- 变形空间和候选消除算法(Candidate-Elimination)C++实现
- C++之路进阶——splay树(序列终结者)
- C语言学习-----------配置环境
- EffectC++(二):构造/析构/赋值运算
- C++实现黄金分割数列(斐波纳奇数列)(非递归)
- LeetCode 1: Number of 1 Bits (C++)
- C语言 百炼成钢12
- 后缀数组
- C语言 hdoj1040
- C语言 hdoj1062
- C语言 hdoj1020