/*
 * 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. Android调用WebService(转)

    Android调用WebService WebService是一种基于SOAP协议的远程调用标准,通过 webservice可以将不同操作系统平台.不同语言.不同技术整合到一块.在Android SD ...

  2. C++的那些事:你真的了解引用吗

    一.引用的本质是什么 说到引用,一般C++的教材中都是这么定义的: 1,引用就是一个对象的别名. 2,引用不是值不占内存空间. 3,引用必须在定义时赋值,将变量与引用绑定. 那你有没有想过,上面的定义 ...

  3. CentOS6.5升级内核到3.10.28 --已验证

    本文适用于CentOS 6.4, CentOS 6.5,估计也适用于其他Linux发行版. 1. 准备工作 确认内核及版本信息 [root@hostname ~]# uname -r 2.6.32-2 ...

  4. DP VK Cup 2012 Qualification Round D. Palindrome pairs

    题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...

  5. 超级楼梯[HDU2041]

    超级楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. BZOJ2690 : 字符串游戏

    离线算法: 先将所有涉及到的串建成字典树,然后用线段树维护dfs序,时间复杂度$O(m\log L)$. 在线算法: 用替罪羊树动态维护Trie树的dfs序即可,时间复杂度$O(L\log L)$. ...

  7. 通过网页的JS代码启动移动APP

    <span style="font-size:18px;"><script> function startAPP(){ window.location = ...

  8. web farm 讨论引出

    关于web farm 有成功的实施的文档没 用它还不如 用nginx,简单易用. Nginx for windows的运行效果咋样 windows  iis无敌 玩nginx就不要用win系统,必须l ...

  9. 【POJ】2104 K-th Number(区间k大+主席树)

    http://poj.org/problem?id=2104 裸题不说.主席树水过. #include <cstdio> #include <iostream> #includ ...

  10. winform学习之-----页面设计-20160523

    1.将默认的Form属性设置为FormBorderStyle:none 2.picturebox均设置为backgroundImage 3.lable设置自动换行,autosize true,设置Ma ...