以linux系统为例,建议从BaseDataEndPoint创建一个子类,同时实现EpollProxy接口,比如SerialPort_Linux,在打开设备后把handle绑定到epoll上即可
classSDK_EXT_CLASSSerialPort_Linux:publicBaseDataEndPoint,publicEpollProxy
{
...
};
voidSerialPort_Linux::OnCreate()
{
__super::OnCreate();
CStringEx filePath="/dev/ttyS0";
mHandle= _open(filePath, O_RDWR | O_NOCTTY | O_NONBLOCK);
if(mHandle < 0)
{
LogW("fail open %s",filePath.c_str());
return;
}
{
SetComSpeed(mHandle,mBaudRate);
}
SOCKET s = (SOCKET)mHandle;
unsignedlonghandle = (unsignedlong)mLooper->GetLooperHandle();
intret = -1;
#ifdef __APPLE__
ASSERT(FALSE);
#else
structepoll_event evt = { 0 };
evt.events= EPOLLIN
//|EPOLLOUT //如果加上会一直触发此事件
|EPOLLRDHUP
|EPOLLERR
;
evt.data.ptr= (EpollProxy*)this;
ret= epoll_ctl((int)handle, EPOLL_CTL_ADD, s, &evt);
#endif
LogV("serial bind ret=%d", ret);
ASSERT(ret == 0);
}
Windows下的做法也类似
这个部分后面会封装的更完善一点,对各OS提供一个抽象层,方便用户扩展。