zoukankan      html  css  js  c++  java
  • epoll 实际使用

    void DataHandle::recv() {
        sleep(2);
        _data_router -> readInfoHw(&mInfo);
        ALOGD(SYS_LOG_TAG "readInfoHW: conn=%d, init=%d, numC=%d, MAC=%s",
                mInfo.isBTConnected, mInfo.isBTInitialized, mInfo.numClient, mInfo.BTMACaddr);
        ALOGD(SYS_LOG_TAG "readInfoHW: sentP=%lld, recvP=%lld, dropP=%lld, sentB=%lld, recvB=%lld",
                mInfo.sentPacket, mInfo.recvPacket, mInfo.dropPacket,
                mInfo.sentByte, mInfo.recvByte);
        ALOGD(SYS_LOG_TAG "before begin recv.........................");
        bool mac_st = false;
        if (strlen(mInfo.BTMACaddr) > 0) {
            mac_st = SysWatcher::instance()->setMacAddr(mInfo.BTMACaddr);
        }
        int dfd = _data_router->getReadFd();
        struct epoll_event event;
        struct epoll_event * _events;
        int efd = epoll_create1(0);
        if (efd == -1) {
            perror("epoll_create");
            abort();
        }
        event.data.fd = dfd;
        event.events = EPOLLIN | EPOLLET;
        int s = epoll_ctl(efd, EPOLL_CTL_ADD, dfd, &event);
        if (s == -1) {
            perror("epoll_ctl");
            abort();
        }
        _events = (epoll_event *) calloc(MAXEVENTS, sizeof(event));
        int done;
        string message;
        const int RECV_BUF_DEPTH = 200;
        char buf[RECV_BUF_DEPTH] = { 0 };
        while (1) {

    int n, i; n = epoll_wait(efd, _events, MAXEVENTS, -1); for (i = 0; i < n; i++) { if ((_events[i].events & EPOLLERR) || (_events[i].events & EPOLLHUP) || (!(_events[i].events & EPOLLIN))) { ALOGD(SYS_LOG_TAG " SysWatcher::EPOLLERR||EPOLLHUP||!EPOLLIN "); fprintf(stderr, "epoll error "); close(_events[i].data.fd); continue; } else if (dfd == _events[i].data.fd) { ssize_t count; memset(buf, 0, RECV_BUF_DEPTH); /* count = read(_events[i].data.fd, buf, RECV_BUF_DEPTH); if (count == -1) { // If errno == EAGAIN, that means we have read all data. So go back to the main loop. if (errno != EAGAIN) { perror("read"); done = 1; ALOGD(SYS_LOG_TAG " SysWatcher:: errno != EAGAIN "); } ALOGD(SYS_LOG_TAG " SysWatcher:: errno == EAGAIN "); break; } else if (count == 0) { //��ʱ // End of file. The remote has closed the connection. done = 1; ALOGD(SYS_LOG_TAG "SysWatcher::recv() DISCONNECTED."); break; }*/ message = string(buf); ALOGD(SYS_LOG_TAG " SysWatcher Recive Data from BLEPare data=%s ", buf); bool dis_st = RouterSysWatcher::dispath(message); message = ""; memset(buf, 0, RECV_BUF_DEPTH); if (dis_st) { ALOGD(SYS_LOG_TAG "dispath sucess! dis_ret =%d", dis_st); } else {  
    ALOGD(SYS_LOG_TAG "dispath error! dis_ret =%d", dis_st); continue; } ALOGD(SYS_LOG_TAG " Epoll test, buf = %s, count =%d", buf, count); } } _data_router -> readInfoHw(&mInfo); if (strlen(mInfo.BTMACaddr) > 0 and mac_st == false) { mac_st = SysWatcher::instance()->setMacAddr(mInfo.BTMACaddr); ALOGD(SYS_LOG_TAG "......readInfoHW: conn=%d, init=%d, numC=%d, MAC=%s", mInfo.isBTConnected, mInfo.isBTInitialized, mInfo.numClient, mInfo.BTMACaddr); } } free(_events); _events = 0; close(dfd); }

  • 相关阅读:
    selenium---常用元素等待的三种方法
    selenium---浏览器操作方法
    selenium---xpath轴定位
    requests---通过file_data方法请求yaml数据
    pywinauto客户端自动化---模拟键盘操作
    pywinauto客户端自动化---模拟鼠标操作
    开发摆摊网心理路程
    解决MVC提示未能加载文件或程序集“System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。
    ATH9K驱动支持2MHz,2.5Mhz,1Mhz等工作带宽
    javax.validation 参数验证
  • 原文地址:https://www.cnblogs.com/wxmdevelop/p/4773859.html
Copyright © 2011-2022 走看看