├── .gitignore ├── License ├── Makefile ├── callback.h ├── connection.h ├── cxxflags ├── evcpp.h ├── eventloop.h ├── listener.h ├── test.cc └── timer.h /.gitignore: -------------------------------------------------------------------------------- 1 | test 2 | -------------------------------------------------------------------------------- /License: -------------------------------------------------------------------------------- 1 | // evcpp - A header-only C++ wrapper of libevent2 2 | // Copyright (c) 2010, Shuo Chen. All rights reserved. 3 | // http://github.com/chenshuo/evcpp 4 | // 5 | // Redistribution and use in source and binary forms, with or without 6 | // modification, are permitted provided that the following conditions 7 | // are met: 8 | // 9 | // * Redistributions of source code must retain the above copyright 10 | // notice, this list of conditions and the following disclaimer. 11 | // * Redistributions in binary form must reproduce the above copyright 12 | // notice, this list of conditions and the following disclaimer in the 13 | // documentation and/or other materials provided with the distribution. 14 | // * The name of the author may not be used to endorse or promote 15 | // products derived from this software without specific prior written 16 | // permission. 17 | // 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | 30 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CXXFLAGS=$$(cat cxxflags) 2 | HEADERS=$(wildcard *.h) 3 | 4 | all: test 5 | 6 | test: test.cc $(HEADERS) cxxflags 7 | g++ $(CXXFLAGS) -o $@ $< -levent_core 8 | 9 | clean: 10 | rm test 11 | 12 | .PHONY: all clean 13 | 14 | -------------------------------------------------------------------------------- /callback.h: -------------------------------------------------------------------------------- 1 | // Copyright 2010, Shuo Chen. All rights reserved. 2 | // http://github.com/chenshuo/evcpp 3 | // 4 | // Use of this source code is governed by a BSD-style license 5 | // that can be found in the License file. 6 | 7 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 8 | // 9 | 10 | #ifndef EVCPP_CALLBACK_H 11 | #define EVCPP_CALLBACK_H 12 | 13 | // this is an internal header file, you should include evcpp.h only. 14 | 15 | namespace evcpp 16 | { 17 | typedef std::function Functor; 18 | typedef std::function TimerCallback; 19 | 20 | class TcpConnection; 21 | typedef std::shared_ptr TcpConnectionPtr; 22 | typedef std::function NewConnectionCallback; 23 | } 24 | 25 | #endif // EVCPP_CALLBACK_H 26 | 27 | -------------------------------------------------------------------------------- /connection.h: -------------------------------------------------------------------------------- 1 | // Copyright 2010, Shuo Chen. All rights reserved. 2 | // http://github.com/chenshuo/evcpp 3 | // 4 | // Use of this source code is governed by a BSD-style license 5 | // that can be found in the License file. 6 | 7 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 8 | // 9 | 10 | #ifndef EVCPP_CONNECTION_H 11 | #define EVCPP_CONNECTION_H 12 | 13 | // this is an internal header file, you should include evcpp.h only. 14 | 15 | namespace evcpp 16 | { 17 | class TcpConnection : public std::enable_shared_from_this, 18 | private noncopyable 19 | { 20 | public: 21 | TcpConnection(struct event_base* base, int fd) 22 | : conn_(::bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE)) 23 | { 24 | } 25 | 26 | ~TcpConnection() 27 | { 28 | ::bufferevent_free(conn_); 29 | } 30 | 31 | private: 32 | struct bufferevent* const conn_; 33 | }; 34 | } 35 | 36 | #endif // EVCPP_CONNECTION_H 37 | 38 | 39 | -------------------------------------------------------------------------------- /cxxflags: -------------------------------------------------------------------------------- 1 | -O0 -g -Wall -Wextra -Werror -Wno-unused-parameter -std=c++11 2 | -------------------------------------------------------------------------------- /evcpp.h: -------------------------------------------------------------------------------- 1 | // Copyright 2010, Shuo Chen. All rights reserved. 2 | // http://github.com/chenshuo/evcpp 3 | // 4 | // Use of this source code is governed by a BSD-style license 5 | // that can be found in the License file. 6 | 7 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 8 | // 9 | 10 | #ifndef EVCPP_EVCPP_H 11 | #define EVCPP_EVCPP_H 12 | 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | // #include 20 | #include 21 | #include 22 | 23 | #include 24 | #include 25 | 26 | namespace evcpp 27 | { 28 | class noncopyable 29 | { 30 | protected: 31 | noncopyable() {} 32 | private: 33 | noncopyable(const noncopyable&); 34 | void operator=(const noncopyable&); 35 | }; 36 | } 37 | #include "callback.h" 38 | #include "timer.h" 39 | #include "eventloop.h" 40 | #include "connection.h" 41 | #include "listener.h" 42 | 43 | #endif // EVCPP_EVCPP_H 44 | 45 | -------------------------------------------------------------------------------- /eventloop.h: -------------------------------------------------------------------------------- 1 | // Copyright 2010, Shuo Chen. All rights reserved. 2 | // http://github.com/chenshuo/evcpp 3 | // 4 | // Use of this source code is governed by a BSD-style license 5 | // that can be found in the License file. 6 | 7 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 8 | // 9 | 10 | #ifndef EVCPP_EVENTLOOP_H 11 | #define EVCPP_EVENTLOOP_H 12 | 13 | // this is an internal header file, you should include evcpp.h only. 14 | 15 | namespace evcpp 16 | { 17 | 18 | class EventLoop : noncopyable 19 | { 20 | public: 21 | EventLoop() 22 | : base_(::event_base_new()) 23 | { 24 | assert(base_ != NULL); 25 | } 26 | 27 | ~EventLoop() 28 | { 29 | ::event_base_free(base_); 30 | } 31 | 32 | int loop() 33 | { 34 | return ::event_base_loop(base_, 0); 35 | } 36 | 37 | // void runInLoop(const Functor& cb); 38 | // void queueInLoop(const Functor& cb); 39 | // TimerId runAt(const Timestamp& time, const TimerCallback& cb); 40 | // TimerId runAfter(double delay, const TimerCallback& cb); 41 | // TimerId runEvery(double interval, const TimerCallback& cb); 42 | // void cancel(TimerId timerId); 43 | 44 | struct event_base* eventBase() 45 | { 46 | return base_; 47 | } 48 | 49 | private: 50 | struct event_base* const base_; 51 | // pthread_t 52 | }; 53 | } 54 | 55 | #endif // EVCPP_EVENTLOOP_H 56 | 57 | -------------------------------------------------------------------------------- /listener.h: -------------------------------------------------------------------------------- 1 | // Copyright 2010, Shuo Chen. All rights reserved. 2 | // http://github.com/chenshuo/evcpp 3 | // 4 | // Use of this source code is governed by a BSD-style license 5 | // that can be found in the License file. 6 | 7 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 8 | // 9 | 10 | #ifndef EVCPP_LISTENER_H 11 | #define EVCPP_LISTENER_H 12 | 13 | // this is an internal header file, you should include evcpp.h only. 14 | 15 | namespace evcpp 16 | { 17 | class Listener : noncopyable 18 | { 19 | public: 20 | Listener(EventLoop* loop, int port) 21 | : listener_(evconnlistener_new_bind( 22 | loop->eventBase(), 23 | newConnectionCallback, 24 | this, 25 | LEV_OPT_CLOSE_ON_FREE | LEV_OPT_CLOSE_ON_EXEC | LEV_OPT_REUSEABLE, 26 | -1, // backlog 27 | getListenSock(port), 28 | sizeof(struct sockaddr_in))) 29 | { 30 | assert(listener_ != nullptr); 31 | } 32 | 33 | ~Listener() 34 | { 35 | ::evconnlistener_free(listener_); 36 | } 37 | 38 | void setNewConnectionCallback(const NewConnectionCallback& cb) 39 | { callback_ = cb; } 40 | 41 | // void start() { } 42 | 43 | private: 44 | 45 | void onConnect(evutil_socket_t fd, const struct sockaddr_in* address) 46 | { 47 | static_assert(std::is_same::value, 48 | "evutil_socket_t is int"); 49 | if (callback_) 50 | { 51 | TcpConnectionPtr conn(std::make_shared( 52 | evconnlistener_get_base(listener_), fd)); 53 | callback_(std::move(conn)); 54 | } 55 | else 56 | { 57 | ::close(fd); 58 | } 59 | } 60 | 61 | static struct sockaddr* getListenSock(int port) 62 | { 63 | static __thread struct sockaddr_in sin; 64 | sin.sin_family = AF_INET; 65 | sin.sin_addr.s_addr = INADDR_ANY; 66 | sin.sin_port = htons(port); 67 | return reinterpret_cast(&sin); 68 | } 69 | 70 | static void newConnectionCallback(struct evconnlistener* listener, 71 | evutil_socket_t fd, struct sockaddr* address, int socklen, void* ctx) 72 | { 73 | Listener* self = static_cast(ctx); 74 | assert(self->listener_ == listener); 75 | assert(socklen == sizeof(struct sockaddr_in)); 76 | self->onConnect(fd, reinterpret_cast(address)); 77 | } 78 | 79 | struct evconnlistener* const listener_; 80 | NewConnectionCallback callback_; 81 | }; 82 | } 83 | 84 | #endif // EVCPP_LISTENER_H 85 | 86 | 87 | -------------------------------------------------------------------------------- /test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2010, Shuo Chen. All rights reserved. 2 | // http://github.com/chenshuo/evcpp 3 | // 4 | // Use of this source code is governed by a BSD-style license 5 | // that can be found in the License file. 6 | 7 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 8 | // 9 | 10 | #include "evcpp.h" 11 | 12 | void onConnect(evcpp::TcpConnectionPtr conn) 13 | { 14 | printf("onConnect %zd\n", conn.use_count()); 15 | } 16 | 17 | int main() 18 | { 19 | evcpp::EventLoop loop; 20 | evcpp::Listener listener(&loop, 1234); 21 | listener.setNewConnectionCallback(onConnect); 22 | loop.loop(); 23 | } 24 | -------------------------------------------------------------------------------- /timer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2010, Shuo Chen. All rights reserved. 2 | // http://github.com/chenshuo/evcpp 3 | // 4 | // Use of this source code is governed by a BSD-style license 5 | // that can be found in the License file. 6 | 7 | // Author: Shuo Chen (chenshuo at chenshuo dot com) 8 | // 9 | 10 | #ifndef EVCPP_TIMER_H 11 | #define EVCPP_TIMER_H 12 | 13 | // this is an internal header file, you should include evcpp.h only. 14 | 15 | namespace evcpp 16 | { 17 | class Timestamp 18 | { 19 | public: 20 | Timestamp() 21 | : microseconds_(0) 22 | { 23 | } 24 | 25 | Timestamp(int64_t microsecondsSinceEpoch) 26 | : microseconds_(microsecondsSinceEpoch) 27 | { 28 | } 29 | 30 | static Timestamp now() 31 | { 32 | struct timeval tv; 33 | ::gettimeofday(&tv, NULL); 34 | return Timestamp(tv.tv_sec*1000000 + tv.tv_usec); 35 | } 36 | 37 | private: 38 | int64_t microseconds_; 39 | }; 40 | 41 | class TimerId 42 | { 43 | }; 44 | } 45 | 46 | #endif // EVCPP_TIMER_H 47 | --------------------------------------------------------------------------------