/*
 * 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;
}

epoll 应用的更多相关文章

  1. 从I/O复用谈epoll为什么高效

    上一篇文章中,谈了一些网络编程的基本概念.在现实使用中,用的最多的就是I/O复用了,无非就是select,poll,epoll 很多人提到网络就说epoll,认为epoll效率是最高的.单纯的这么认为 ...

  2. select、poll、epoll之间的区别总结

    select.poll.epoll之间的区别总结 05/05. 2014 select,poll,epoll都是IO多路复用的机制.I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪 ...

  3. (转载) Linux IO模式及 select、poll、epoll详解

    注:本文是对众多博客的学习和总结,可能存在理解错误.请带着怀疑的眼光,同时如果有错误希望能指出. 同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案 ...

  4. linux下select/poll/epoll机制的比较

    select.poll.epoll简介 epoll跟select都能提供多路I/O复用的解决方案.在现在的Linux内核里有都能够支持,其中epoll是Linux所特有,而select则应该是POSI ...

  5. epoll LT/ET 深度剖析

    EPOLL事件的两种模型: Level Triggered (LT) 水平触发 .socket接收缓冲区不为空 有数据可读 读事件一直触发 .socket发送缓冲区不满 可以继续写入数据 写事件一直触 ...

  6. 非阻塞/异步(epoll) openssl

    前段时间在自己的异步网络框架handy中添加openssl的支持,当时在网络上搜索了半天也没有找到很好的例子,后来自己慢慢的摸索,耗费不少时间,终于搞定.因此把相关的资料整理一下,并给出简单的例子,让 ...

  7. select,epoll,poll比较

    介绍和比较 http://www.cnblogs.com/maociping/p/5132583.html 比较 http://www.dataguru.cn/thread-336032-1-1.ht ...

  8. Linux epoll

    一. epoll函数集 epoll主要有三个函数: 1. int epoll_create(int size); 创建一个epoll的句柄,size用来告诉内核这个监听的数目一共有多大.这个参数不同于 ...

  9. linux下epoll实现机制

    linux下epoll实现机制 原作者:陶辉 链接:http://blog.csdn.net/russell_tao/article/details/7160071 先简单回顾下如何使用C库封装的se ...

  10. select、poll、epoll之间的区别总结[整理]

    select,poll,epoll都是IO多路复用的机制.I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作.但select ...

随机推荐

  1. 监控Spark应用方法简介

    监控Spark应用有很多种方法. Web接口每一个SparkContext启动一个web UI用来展示应用相关的一些非常有用的信息,默认在4040端口.这些信息包括: 任务和调度状态的列表RDD大小和 ...

  2. Loadrunner 关联 web_custom_request综合实例

    Loadrunner 关联 web_custom_request综合实例 Loadrunner 关联web_custom_request,针对自带的订票系统的一个综合实例,相信看了本文大家对学习loa ...

  3. CodeForces 300C --数论

    A - A Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  4. 寒冰王座[HDU1248]

    寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. windows8 开发教程 教你制作 多点触控Helper可将任意容器内任意对象进行多点缩放

    http://blog.csdn.net/wangrenzhu2011/article/details/7732907 (转) 实现方法: 对Manipulation进行抽象化 使不同容器可共用多点缩 ...

  6. 【BZOJ】1180: [CROATIAN2009]OTOCI & 2843: 极地旅行社(lct)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1180 今天状态怎么这么不好..................................... ...

  7. Linux_使用Linux之安装jdk 7

    工具/原料 jdk7源码安装压缩包 方法/步骤   卸载OpenJDK rpm -qa | grep java rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1 ...

  8. erlang-jiffy 安装手记

    今天安装 erlang-jiffy 把握逼疯,不过最后还是成功了. 错误避免: rebar只能再英文目录下运行,如果编译jiffy的目录中有中文或其它unicode字符,将会出错 从git relea ...

  9. Java导出Highcharts生成的图表为图片源码

    本文转载自:http://blog.csdn.net/dengsilinming/article/details/7352054 需要的jar包: 需要的js文件:(可以通过http://www.hi ...

  10. salt执行报错一例

    执行报错: 查看服务端日志: 认证有问题 重新认证吧!!! minion端: [root@super66 ~]# cd /etc/salt/[root@super66 salt]# lsminion ...