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\n", ret);
        exit(1);
    }
}
void * _start(void *)
{
    service->startLoop();
    printf("bittube_service startLoop \n");
    return 0;
}
int main()
{
    sp < IBinder > b = defaultServiceManager()->getService(
            String16("bittube_service"));
    if (b == NULL) {
        printf("bittube_service binder null\n");
        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\n");
                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;
}

转载于:https://www.cnblogs.com/wxmdevelop/p/4686361.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值