libuv网络公共库的封装

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lightjia/article/details/84324157

如下是提取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);
}

猜你喜欢

转载自blog.csdn.net/lightjia/article/details/84324157
今日推荐