• epoll 应用


    /*
     * test_bittube.cpp
     *
     *  Created on: 2015年7月13日
     *      Author: ting.guit
     */
    #include <binder/IPCThreadState.h>
    #include <binder/ProcessState.h>
    #include <binder/IServiceManager.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/epoll.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <errno.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include "IBittubeService.h"
    #include <gui/BitTube.h>
    using namespace android;
    sp<IBittubeService> service;
    typedef void *Thr_Fun(void *);
    void create_thread(Thr_Fun *fun ){
        pthread_t pthread_id;
        int ret = 0;
        ret = pthread_create(&pthread_id, NULL, fun, NULL);
        if( ret ) {
            printf("create thread failed %d ", ret);
            exit(1);
        }
    }
    void * _start(void *)
    {
        service->startLoop();
        printf("bittube_service startLoop ");
        return 0;
    }
    int main()
    {
        sp < IBinder > b = defaultServiceManager()->getService(
                String16("bittube_service"));
        if (b == NULL) {
            printf("bittube_service binder null ");
            exit(1);
        }
        service = interface_cast < IBittubeService > (b);
        int sfd = service->getFd();
        ALOGD("test-------%d",sfd);
        Parcel p;
       // p.writeFileDescriptor(sfd);
        //BitTube *bit = new BitTube(p);
    //    char buff[128] = {0};
    //
    //    service->startLoop();
    //
    //    while(1) {
    //        ::recv(sfd,buff,sizeof(buff),MSG_DONTWAIT);
    //        ALOGD("test-------%s",buff);
    //    }
        //BitTube::recvBuffer(bit,buff,sizeof(buff));
       //return 0;
        struct epoll_event event;
        struct epoll_event* events;
        int efd = epoll_create1(0);
        if (efd == -1) {
            perror("epoll_create");
            abort();
        }
        event.data.fd = sfd;
        event.events = EPOLLIN | EPOLLET;
        int  s = epoll_ctl(efd, EPOLL_CTL_ADD, sfd, &event);
        if (s == -1) {
            perror("epoll_ctl");
            abort();
        }
    #define MAXEVENTS 64
        /* Buffer where events are returned */
        events = (epoll_event*)calloc(MAXEVENTS, sizeof event);
        create_thread(_start);
        int  done;
        /* The event loop */
        while (1) {
            int n, i;
            n = epoll_wait(efd, events, MAXEVENTS, -1);
            ALOGD("test-------%d",n);
            for (i = 0; i < n; i++) {
                if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP)
                        || (!(events[i].events & EPOLLIN))) {
                    /* An error has occured on this fd, or the socket is not
                     ready for reading (why were we notified then?) */
                    fprintf(stderr, "epoll error ");
                    close(events[i].data.fd);
                    continue;
                } else if(sfd == events[i].data.fd)
                {
                    ssize_t count;
                    char buf[256] = {0};
                    count = read(events[i].data.fd, buf, sizeof buf);
                    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;
                        }
                        break;
                    } else if (count == 0) {
                        /* End of file. The remote has closed the
                         connection. */
                        done = 1;
                        break;
                    }
                    ALOGD("test-------%s,%d",buf,count);
                }
            }
            sleep(1);
        }
        free(events);
        close(sfd);
          return EXIT_SUCCESS;
    }
  • 相关阅读:
    pandas 和 matplotlib 的设置
    Django图(菜鸟教程)
    使用 pyperclip 实现复制粘贴
    Pycharm 使用 doctest 进行判断程序是否运行正常
    jieba 运行结果不显示 Building prefix dict from the default dictionary ...
    浮点数以 .0 结尾如何转换为整数
    IOS时间转时间戳出现Invalid Date的问题
    PHP小技巧
    CSS小技巧
    树状数组
  • 原文地址:https://www.cnblogs.com/wxmdevelop/p/4686361.html
Copyright © 2020-2023  润新知