#include <cstdio>
#include <netinet/in.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h> #define MAX_LINE 16384 struct fd_state
{
char buffer[MAX_LINE];
size_t buffer_used;
size_t n_written;
size_t write_upto;
struct event*read_event;
struct event*write_event;
}; void run(void);
void do_accept(evutil_socket_t listener, short event, void*arg);
void readcb(struct bufferevent* bev, void * ctx);
void errorcb(struct bufferevent* bev, short error, void * ctx); char rot13_char(char c)
{
if ((c >= 'a' && c <= 'm ') || (c >= 'A ' && c <= 'M '))
return c + ;
else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
return c - ;
else
return c;
} int main(int c, char**v)
{
run();
return ;
} void run(void)
{
evutil_socket_t listener;
struct sockaddr_in sin;
struct event_base*base;
struct event*listener_event;
base = event_base_new();
if (!base)
return; sin.sin_family = AF_INET; sin.sin_addr.s_addr = ;
sin.sin_port = htons();
listener = socket(AF_INET, SOCK_STREAM, );
evutil_make_socket_nonblocking(listener);
/*#ifndef WIN32
{
int one = 1;
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
}
#endif */
if (bind(listener, (struct sockaddr*) &sin, sizeof(sin)) < )
{
perror("bind"); return;
}
if (listen(listener, ) < )
{
perror("listen");
return;
}
listener_event = event_new(base, listener, EV_READ | EV_PERSIST, do_accept, (void*)base); event_add(listener_event, NULL);
event_base_dispatch(base);
} void do_accept(evutil_socket_t listener, short event, void*arg)
{
struct event_base*base = (event_base*)(arg);
struct sockaddr_storage ss;
socklen_t slen = sizeof(ss);
int fd = accept(listener, (struct sockaddr*) &ss, &slen);
if (fd < )
{
perror("accept");
}
else if (fd > FD_SETSIZE)
{
close(fd);
}
else
{
struct bufferevent* bev;
evutil_make_socket_nonblocking(fd);
bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(bev, readcb, NULL, errorcb, NULL);
bufferevent_setwatermark(bev, EV_READ, , MAX_LINE);
bufferevent_enable(bev, EV_READ | EV_WRITE);
}
}
void readcb(struct bufferevent* bev, void * ctx)
{
struct evbuffer* input, *output;
char* line;
size_t n;
int i;
input = bufferevent_get_input(bev);
output = bufferevent_get_output(bev);
while ((line = evbuffer_readln(input, &n, EVBUFFER_EOL_LF)))
{
printf("str = %s \n", line); for (i = ; i < n; ++i)
line[i] = rot13_char(line[i]); evbuffer_add(output, line, n);
evbuffer_add(output, "\n", );
free(line);
} if (evbuffer_get_length(input) >= MAX_LINE)
{
char buf[];
while (evbuffer_get_length(input))
{
int n = evbuffer_remove(input, buf, sizeof(buf));
for (i = ; i < n; ++i)
buf[i] = rot13_char(buf[i]); evbuffer_add(output, buf, n);
}
evbuffer_add(output, "\n", );
}
}
void errorcb(struct bufferevent* bev, short error, void * ctx)
{
if (error & BEV_EVENT_EOF)
{
/* connection has been closed, do any clean up here*/
}
else if (error & BEV_EVENT_ERROR)
{
/* check errno to see what error occurred*/
}
else if (error & BEV_EVENT_TIMEOUT)
{
/* must be a timeout event handle, handle it*/
}
bufferevent_free(bev);
}

libevent::bufferevent的更多相关文章

  1. libevent+bufferevent总结

    libevent+bufferevent总结 1 学习参考网址 libevent学习网址:http://blog.csdn.net/feitianxuxue/article/details/93725 ...

  2. 一步一步解剖Libevent源代码 - 0

    本系列文章将在<Libevent源码深度解剖>的基础上,结合Libevent-2.0.22代码,更新了其中的一些定义和说明,以及加上了bufferevent部分.   一.Libevent ...

  3. [转载]Parsing X.509 Certificates with OpenSSL and C

    Parsing X.509 Certificates with OpenSSL and C Zakir Durumeric | October 13, 2013 While OpenSSL has b ...

  4. 处理大并发之五 使用libevent利器bufferevent

    转自:http://blog.csdn.net/feitianxuxue/article/details/9386843 处理大并发之五 使用libevent利器bufferevent 首先来翻译一段 ...

  5. libevent源码分析:bufferevent

    struct bufferevent定义在文件bufferevent_struct.h中. /** Shared implementation of a bufferevent. This type ...

  6. (转)Libevent(4)— Bufferevent

    转自:http://name5566.com/4215.html 参考文献列表:http://www.wangafu.net/~nickm/libevent-book/ 此文编写的时候,使用到的 Li ...

  7. Libevent学习笔记(五) 根据例子学习bufferevent

    libevent中提供了一个Hello-world.c 的例子,从这个例子可以学习libevent是如何使用bufferevent的. 这个例子在Sample中 这个例子之前讲解过,这次主要看下buf ...

  8. Libevent学习笔记(四) bufferevent 的 concepts and basics

    Bufferevents and evbuffers Every bufferevent has an input buffer and an output buffer. These are of ...

  9. libevent学习七(bufferevent)

    1. 每个bufferevent 都拥有类型为struct evbuffer的input buffer和out buffer,分别供数据读取和数据写入使用. 2.读取和写入数据是通过编写和设置对应的回 ...

随机推荐

  1. TestNG(六) 忽略测试

    package com.course.testng.suite; import org.testng.annotations.Test; public class IgnoreTest { @Test ...

  2. Android远程服务AIDL开发过程中容易遇见的两个问题

    问题 一 JavaBinder: Uncaught remote exception! (Exceptions are not yet supported across processes.) jav ...

  3. linux切换jdk

    一.安装openjdk yum search openjdk yum install java-1.8.0-openjdk-devel-debug.x86_64 二.查询java版本 alternat ...

  4. 记一次处理mysql数据库无故锁表的经历

    某日,生产环境上的用户表突然无故锁表,原以为只是偶发的bug.所以第一时间想到的解决方案简单粗暴:重启数据库(service mysqld restart).问题得以解决. 10min后,该表再次锁表 ...

  5. Hadoop 文件系统命令行基础

    Hadoop 命令行最常用指令篇:  1.ls (list directory) Usage: hadoop fs -ls [R] Option: -R => 递归显示 2.mkdir (mak ...

  6. 转载:alpha测试和beta测试的区别;黑盒测试和白盒测试的区别;

    alpha测试版,有点相当于内部测试,一般开发人员在场   ,是由用户做测试,但开发人员在场,一般是请用户到开发现场去测试 beta测试版,完全交给用户,由用户做测试,返回测试报告,相当于发行前的一个 ...

  7. ELK7.3实战安装配置文档

    整体架构   一:环境准备 1.环境规划准备 192.168.43.16 jdk,elasticsearch-master ,logstash,kibana 192.168.43.17 jdk,ela ...

  8. hadoop之mapreduce详解(进阶篇)

    上篇文章hadoop之mapreduce详解(基础篇)我们了解了mapreduce的执行过程和shuffle过程,本篇文章主要从mapreduce的组件和输入输出方面进行阐述. 一.mapreduce ...

  9. FreeSql 导航属性的联级保存功能

    写在前面 FreeSql 一个款 .net 平台下支持 .net framework 4.5+..net core 2.1+ 的开源 ORM.单元测试超过3100+,正在不断吸引新的开发者,生命不息开 ...

  10. JS/Jquery关系

    1. JS / JQuery介绍 Jquery是JS库,何为JS库,即把常用的js方法进行封装,封装到单独的JS文件中,要用的时候直接调用即可: 2. JS / JQuery对象 1. 定义 (1) ...