epoll 应用

摘要:
ting.guit*/#包含<#包括<未发布h>服务ret=pthread_create(&ret);event.events=EPOLLIN | EPOLLET;i;事件;i<i++){if((events[i].events&EPOLLERR)||(event;
/*
 * 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;
}

免责声明:文章转载自《epoll 应用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Shader笔记——5.渲染纹理MSSQL无落地文件执行Rootkit-WarSQLKit下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

selenium 定位方式

在使用selenium webdriver进行元素定位时,通常使用findElement或findElements方法结合By类返回的元素句柄来定位元素。其中By类的常用定位方式共八种,现分别介绍如下。 1. By.name() 假设我们要测试的页面源码如下: <button aria-label="Google Search" name="bt...

sysbench的安装及使用

sysbench是一个模块化的、跨平台、多线程基准,主要用于评估测试各种不同系统参数下的数据库负载情况。它主要包括以下几种方式的测试:测试工具 文档顺序: 一、安装 二、测试 1、cpu性能2、磁盘io性能3、调度程序性能4、内存分配及传输速度5、POSIX线程性能6、数据库性能(OLTP基准测试)目前sysbench主要支持 MySQL,pgsql,or...

JPA学习---第九节:JPA中的一对多双向关联与级联操作

一、一对多双向关联与级联操作 1、创建项目,配置文件代码如下: <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http:...

PyCharm创建普通项目配置支持jinja2语法

打开项目的根目录的.idea文件夹中项目名.iml文件(隐藏文件) 打开这个iml文件,在component标签的同级,添加如下代码: <component name="TemplatesService"> <option name="TEMPLATE_CONFIGURATION" value="Jinja2" /> <opt...

10种 分布式ID生成方式(新增MongoDB的ObjectId)

一、为什么要用分布式ID? 在说分布式ID的具体实现之前,我们来简单分析一下为什么用分布式ID?分布式ID应该满足哪些特征? 1、什么是分布式ID? 拿MySQL数据库举个栗子: 在我们业务数据量不大的时候,单库单表完全可以支撑现有业务,数据再大一点搞个MySQL主从同步读写分离也能对付。 但随着数据日渐增长,主从同步也扛不住了,就需要对数据库进行分库分表...

intellij 代码下有小黄波浪线

在同一个工程中或模块中,写了重复的代码。 原来“在IDEA中根据设置的不同,有些代码页,当代码重复比较多时,会出现灰色或黄色的波浪线”,…… 知道原因了,解决就有办法了,设置。 在File->Settings->Editor->Code Style->Inspections->General->Duplicated Co...