如下是提取libuv开发过程中UDP以及TCP的公共基类
#ifndef __CUVNETBASE__H_
#define __CUVNETBASE__H_
#include "UvBase.h"
#include "UvMutex.h"
#include <string>
class CUvNetBase : public CUvBase{
public:
CUvNetBase();
virtual ~CUvNetBase();
public:
int SetNetParam(const char* pIp, unsigned short iPort);
protected:
unsigned short musPort;
std::string mstrIp;
struct sockaddr_in mstLocalAddr;
};
#endif
#include "UvNetBase.h"
CUvNetBase::CUvNetBase()
{
mstrIp = "";
musPort = 0;
memset(&mstLocalAddr, 0, sizeof(mstLocalAddr));
}
CUvNetBase::~CUvNetBase()
{
}
int CUvNetBase::SetNetParam(const char* pIp, unsigned short iPort) {
if (nullptr != pIp && strlen(pIp) > 0) {
mstrIp = pIp;
}
musPort = iPort;
return uv_ip4_addr(mstrIp.c_str(), iPort, &mstLocalAddr);
}