├── libs ├── actor │ ├── doc │ │ └── README │ ├── src │ │ ├── impl │ │ │ ├── protocol.hpp │ │ │ └── tcp │ │ │ │ └── acceptor.cpp │ │ └── heartbeat.cpp │ ├── example │ │ ├── cluster │ │ │ ├── hash.hpp │ │ │ ├── user.hpp │ │ │ ├── game.hpp │ │ │ ├── master.hpp │ │ │ ├── CMakeLists.txt │ │ │ ├── node.hpp │ │ │ ├── gate.hpp │ │ │ ├── endpoint.hpp │ │ │ ├── conn.hpp │ │ │ ├── app.hpp │ │ │ ├── main.cpp │ │ │ ├── user.cpp │ │ │ └── game.cpp │ │ ├── cluster_client │ │ │ ├── main.cpp │ │ │ ├── client.hpp │ │ │ ├── CMakeLists.txt │ │ │ └── socket.hpp │ │ ├── CMakeLists.txt │ │ ├── asio │ │ │ └── CMakeLists.txt │ │ ├── link │ │ │ ├── CMakeLists.txt │ │ │ └── main.cpp │ │ ├── matching │ │ │ ├── CMakeLists.txt │ │ │ └── main.cpp │ │ ├── pingpong │ │ │ ├── CMakeLists.txt │ │ │ └── main.cpp │ │ └── helloworld │ │ │ ├── CMakeLists.txt │ │ │ └── main.cpp │ ├── user.hpp.in │ ├── test │ │ ├── CMakeLists.txt │ │ ├── test_mixin_pingpong.hpp │ │ ├── test_slice_pingpong.hpp │ │ ├── test_send_recv.hpp │ │ ├── test_mixin.hpp │ │ ├── test_actor_pingpong.hpp │ │ ├── test_coro.hpp │ │ ├── test_remote_link.hpp │ │ ├── test_match.hpp │ │ ├── test_socket.hpp │ │ ├── test_relay.hpp │ │ ├── test_router_link.hpp │ │ ├── test_router.hpp │ │ ├── main.cpp │ │ ├── test_service.hpp │ │ ├── test_stackless_pingpong.hpp │ │ ├── test_socket_broken.hpp │ │ ├── test_router_broken.hpp │ │ ├── test_actor.hpp │ │ ├── test_slice.hpp │ │ ├── test_message.hpp │ │ ├── test_link.hpp │ │ ├── test_response.hpp │ │ ├── test_remote_relay.hpp │ │ ├── test_object_pool.hpp │ │ └── test_stackless.hpp │ └── CMakeLists.txt └── amsg │ └── CMakeLists.txt ├── manual.pdf ├── gce ├── amsg │ ├── README │ └── License.txt ├── actor │ ├── wait.hpp │ ├── actor_fwd.hpp │ ├── detail │ │ ├── mailbox_fwd.hpp │ │ ├── basic_acceptor.hpp │ │ ├── pack.hpp │ │ ├── request.hpp │ │ ├── basic_socket.hpp │ │ ├── exit.hpp │ │ ├── link.hpp │ │ ├── scoped_bool.hpp │ │ ├── heartbeat.hpp │ │ ├── buffer.hpp │ │ ├── spawn.hpp │ │ ├── acceptor.hpp │ │ ├── mailbox.hpp │ │ ├── buffer_ref.hpp │ │ ├── object_pool.hpp │ │ ├── cache_pool.hpp │ │ └── socket.hpp │ ├── impl │ │ ├── protocol.hpp │ │ └── tcp │ │ │ ├── acceptor.hpp │ │ │ └── socket.hpp │ ├── all.hpp │ ├── net_option.hpp │ ├── response.hpp │ ├── service.hpp │ ├── match.hpp │ ├── service_id.hpp │ ├── adaptor.hpp │ ├── atom.hpp │ ├── thread_mapped_actor.hpp │ ├── actor_id.hpp │ └── context.hpp ├── config.hpp ├── detail │ ├── object_access.hpp │ ├── scope.hpp │ ├── unique_ptr.hpp │ ├── ref_count.hpp │ └── freelist.hpp └── integer.hpp ├── user.hpp.in ├── License.txt └── CMakeLists.txt /libs/actor/doc/README: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /manual.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nousxiong/gce/HEAD/manual.pdf -------------------------------------------------------------------------------- /gce/amsg/README: -------------------------------------------------------------------------------- 1 | lordoffox(Ning Ding, lordoffox@gmail.com)'s amsg 0.1.0. 2 | (http://www.cppfans.org/bbs/1593.html) 3 | 4 | Thx lordoffox! 5 | -------------------------------------------------------------------------------- /libs/actor/src/impl/protocol.hpp: -------------------------------------------------------------------------------- 1 | #ifndef GCE_IMPL_PROTOCOL_HPP 2 | #define GCE_IMPL_PROTOCOL_HPP 3 | 4 | #include 5 | 6 | namespace gce 7 | { 8 | namespace msg 9 | { 10 | struct header 11 | { 12 | header() 13 | : size_(0) 14 | , type_(match_nil) 15 | { 16 | } 17 | 18 | boost::uint32_t size_; 19 | match_t type_; 20 | }; 21 | } 22 | } 23 | 24 | GCE_PACK(gce::msg::header, (size_&sfix)(type_)); 25 | 26 | #endif /// GCE_IMPL_PROTOCOL_HPP 27 | -------------------------------------------------------------------------------- /gce/actor/wait.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_WAIT_HPP 11 | #define GCE_ACTOR_WAIT_HPP 12 | 13 | #include 14 | 15 | namespace gce 16 | { 17 | template 18 | inline void wait(Waiter& waiter, duration_t dur) 19 | { 20 | return waiter.wait(dur); 21 | } 22 | } 23 | 24 | #endif /// GCE_ACTOR_WAIT_HPP 25 | -------------------------------------------------------------------------------- /libs/amsg/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is part of the CMake build system for Gce 3 | # 4 | # CMake auto-generated configuration options. 5 | # Do not check in modified versions of this file. 6 | # 7 | # Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 8 | # 9 | # Distributed under the Boost Software License, Version 1.0. (See accompanying 10 | # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 11 | # 12 | # See https://github.com/nousxiong/gce for latest version. 13 | # 14 | 15 | # All headers need install include dependences. 16 | install ( 17 | DIRECTORY ${PROJECT_SOURCE_DIR}/gce/amsg DESTINATION include/gce 18 | PATTERN "impl" EXCLUDE 19 | PATTERN "CVS" EXCLUDE 20 | PATTERN ".svn" EXCLUDE 21 | ) 22 | -------------------------------------------------------------------------------- /gce/actor/actor_fwd.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_ACTOR_FWD_HPP 11 | #define GCE_ACTOR_ACTOR_FWD_HPP 12 | 13 | #include 14 | 15 | namespace gce 16 | { 17 | inline std::size_t default_stacksize() 18 | { 19 | return boost::coroutines::stack_allocator::default_stacksize(); 20 | } 21 | 22 | inline std::size_t minimum_stacksize() 23 | { 24 | return boost::coroutines::stack_allocator::minimum_stacksize(); 25 | } 26 | } 27 | 28 | #endif /// GCE_ACTOR_ACTOR_FWD_HPP 29 | -------------------------------------------------------------------------------- /gce/actor/detail/mailbox_fwd.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_MAILBOX_FWD_HPP 11 | #define GCE_ACTOR_DETAIL_MAILBOX_FWD_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | namespace gce 20 | { 21 | namespace detail 22 | { 23 | typedef boost::variant recv_t; 24 | } 25 | } 26 | 27 | #endif /// GCE_ACTOR_DETAIL_MAILBOX_FWD_HPP 28 | -------------------------------------------------------------------------------- /libs/actor/example/cluster/hash.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_EXAMPLE_CLUSTER_HASH_HPP 11 | #define GCE_ACTOR_EXAMPLE_CLUSTER_HASH_HPP 12 | 13 | #include 14 | 15 | typedef gce::match_t hash_t; 16 | 17 | /// from《The C Programming Language》 18 | inline hash_t hash(char const* str, std::size_t size) 19 | { 20 | hash_t ret = 0; 21 | for (std::size_t i=0; i 15 | #include 16 | 17 | typedef gce::aid_t cid_t; 18 | class user 19 | { 20 | public: 21 | static void run( 22 | gce::actor&, app_ctxid_list_t game_list, 23 | gce::aid_t old_usr_aid, gce::aid_t master, 24 | cid_t cid, std::string username, std::string passwd 25 | ); 26 | }; 27 | 28 | #endif /// GCE_ACTOR_EXAMPLE_CLUSTER_USER_HPP 29 | -------------------------------------------------------------------------------- /gce/config.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// config.hpp 3 | /// 4 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 5 | /// 6 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 7 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 8 | /// 9 | /// See https://github.com/nousxiong/gce for latest version. 10 | /// 11 | 12 | #ifndef GCE_CONFIG_HPP 13 | #define GCE_CONFIG_HPP 14 | 15 | #include 16 | #include 17 | 18 | /// Suppress some vc warnings. 19 | #ifdef BOOST_COMP_MSVC 20 | # pragma warning(disable : 4251 4231 4660 4275 4355 4244 4307) 21 | #endif 22 | 23 | /// Ensure occupy entire cache(s) line. 24 | #define GCE_CACHE_ALIGNED_VAR(type, var) \ 25 | type var; \ 26 | byte_t pad_##var[(sizeof(type)/GCE_CACHE_LINE_SIZE + 1)*GCE_CACHE_LINE_SIZE - sizeof(type)]; 27 | 28 | #endif /// GCE_CONFIG_HPP 29 | -------------------------------------------------------------------------------- /gce/detail/object_access.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_DETAIL_OBJECT_ACCESS_HPP 11 | #define GCE_DETAIL_OBJECT_ACCESS_HPP 12 | 13 | #include 14 | 15 | namespace gce 16 | { 17 | namespace detail 18 | { 19 | class object_access 20 | { 21 | public: 22 | template 23 | inline static T* get_next(T* obj) 24 | { 25 | return obj->next_obj_; 26 | } 27 | 28 | template 29 | inline static void set_next(T* obj, T* next) 30 | { 31 | obj->next_obj_ = next; 32 | } 33 | }; 34 | } 35 | } 36 | 37 | #endif /// GCE_DETAIL_OBJECT_ACCESS_HPP 38 | -------------------------------------------------------------------------------- /gce/actor/impl/protocol.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_IMPL_PROTOCOL_HPP 11 | #define GCE_ACTOR_IMPL_PROTOCOL_HPP 12 | 13 | #include 14 | 15 | namespace gce 16 | { 17 | namespace msg 18 | { 19 | struct header 20 | { 21 | header() 22 | : size_(0) 23 | , type_(match_nil) 24 | , tag_offset_(u32_nil) 25 | { 26 | } 27 | 28 | boost::uint32_t size_; 29 | match_t type_; 30 | boost::uint32_t tag_offset_; 31 | }; 32 | } 33 | } 34 | 35 | GCE_PACK(gce::msg::header, (size_&sfix)(type_)(tag_offset_)); 36 | 37 | #endif /// GCE_ACTOR_IMPL_PROTOCOL_HPP 38 | -------------------------------------------------------------------------------- /libs/actor/example/cluster_client/main.cpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #include "client.hpp" 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | int main(int argc, char* argv[]) 18 | { 19 | try 20 | { 21 | if (argc < 1) 22 | { 23 | std::cerr << "Usage: " << std::endl; 24 | return 0; 25 | } 26 | 27 | client cln(argv[1]); 28 | cln.run(); 29 | } 30 | catch (std::exception& ex) 31 | { 32 | std::cerr << ex.what() << std::endl; 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /libs/actor/example/cluster/game.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_EXAMPLE_CLUSTER_GAME_HPP 11 | #define GCE_ACTOR_EXAMPLE_CLUSTER_GAME_HPP 12 | 13 | #include "app.hpp" 14 | #include 15 | 16 | class game 17 | { 18 | public: 19 | static gce::aid_t start( 20 | gce::actor& sire, 21 | gce::match_t svc_name, 22 | app_ctxid_list_t game_list 23 | ); 24 | 25 | private: 26 | static void run( 27 | gce::actor& self, 28 | gce::match_t svc_name, 29 | app_ctxid_list_t game_list 30 | ); 31 | }; 32 | 33 | #endif /// GCE_ACTOR_EXAMPLE_CLUSTER_GAME_HPP 34 | -------------------------------------------------------------------------------- /gce/actor/all.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_ALL_HPP 11 | #define GCE_ACTOR_ALL_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #endif /// GCE_ACTOR_ALL_HPP 29 | -------------------------------------------------------------------------------- /gce/actor/detail/basic_acceptor.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_BASIC_ACCEPTOR_HPP 11 | #define GCE_ACTOR_DETAIL_BASIC_ACCEPTOR_HPP 12 | 13 | #include 14 | #include 15 | 16 | namespace gce 17 | { 18 | namespace detail 19 | { 20 | class basic_socket; 21 | class basic_acceptor 22 | { 23 | public: 24 | basic_acceptor() {} 25 | virtual ~basic_acceptor() {} 26 | 27 | public: 28 | virtual void bind() = 0; 29 | virtual socket_ptr accept(yield_t) = 0; 30 | virtual void close() = 0; 31 | }; 32 | } 33 | } 34 | 35 | #endif /// GCE_ACTOR_DETAIL_BASIC_ACCEPTOR_HPP 36 | 37 | -------------------------------------------------------------------------------- /user.hpp.in: -------------------------------------------------------------------------------- 1 | /// 2 | /// user.hpp 3 | /// CMake auto-generated configuration options. 4 | /// Do not check in modified versions of this file. 5 | /// 6 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 7 | /// 8 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 9 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 10 | /// 11 | /// See https://github.com/nousxiong/gce for latest version. 12 | /// 13 | 14 | #ifndef GCE_USER_HPP 15 | #define GCE_USER_HPP 16 | 17 | /// The configured options and settings for geth. 18 | #define GCE_VERSION_MAJOR @GCE_VERSION_MAJOR@ 19 | #define GCE_VERSION_MINOR @GCE_VERSION_MINOR@ 20 | 21 | /// User options. 22 | #define GCE_ASIO_ALLOC_HANDLER_SIZE @GCE_ASIO_ALLOC_HANDLER_SIZE@ 23 | 24 | #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) 25 | # define GCE_WINVER @GCE_WINVER@ 26 | #endif 27 | 28 | #endif /// GCE_USER_HPP 29 | -------------------------------------------------------------------------------- /gce/actor/detail/pack.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_PACK_HPP 11 | #define GCE_ACTOR_DETAIL_PACK_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | namespace gce 18 | { 19 | namespace detail 20 | { 21 | struct pack 22 | { 23 | pack() 24 | : is_err_ret_(false) 25 | , cache_queue_index_(size_nil) 26 | , cache_index_(u64_nil) 27 | { 28 | } 29 | 30 | detail::tag_t tag_; 31 | aid_t recver_; 32 | aid_t skt_; 33 | svcid_t svc_; 34 | bool is_err_ret_; 35 | message msg_; 36 | 37 | std::size_t cache_queue_index_; 38 | boost::uint64_t cache_index_; 39 | }; 40 | } 41 | } 42 | 43 | #endif /// GCE_ACTOR_DETAIL_PACK_HPP 44 | -------------------------------------------------------------------------------- /gce/actor/detail/request.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_REQUEST_HPP 11 | #define GCE_ACTOR_DETAIL_REQUEST_HPP 12 | 13 | #include 14 | #include 15 | 16 | namespace gce 17 | { 18 | namespace detail 19 | { 20 | class request_t 21 | { 22 | public: 23 | request_t() : id_(sid_nil) {} 24 | request_t(sid_t id, aid_t aid) : id_(id), aid_(aid) {} 25 | ~request_t() {} 26 | 27 | public: 28 | inline bool valid() const { return id_ != sid_nil; } 29 | inline sid_t get_id() const { return id_; } 30 | inline aid_t get_aid() const { return aid_; } 31 | 32 | private: 33 | sid_t id_; 34 | aid_t aid_; 35 | }; 36 | } 37 | } 38 | 39 | #endif /// GCE_ACTOR_DETAIL_REQUEST_HPP 40 | 41 | -------------------------------------------------------------------------------- /libs/actor/user.hpp.in: -------------------------------------------------------------------------------- 1 | /// 2 | /// user.hpp 3 | /// CMake auto-generated configuration options. 4 | /// Do not check in modified versions of this file. 5 | /// 6 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 7 | /// 8 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 9 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 10 | /// 11 | /// See https://github.com/nousxiong/gce for latest version. 12 | /// 13 | 14 | #ifndef GCE_ACTOR_USER_HPP 15 | #define GCE_ACTOR_USER_HPP 16 | 17 | /// User options. 18 | #define GCE_CACHE_LINE_SIZE @GCE_CACHE_LINE_SIZE@ 19 | #define GCE_FREE_CACHE_SIZE @GCE_FREE_CACHE_SIZE@ 20 | #define GCE_SOCKET_RECV_CACHE_SIZE @GCE_SOCKET_RECV_CACHE_SIZE@ 21 | #define GCE_SOCKET_RECV_MAX_SIZE @GCE_SOCKET_RECV_MAX_SIZE@ 22 | #define GCE_SMALL_MSG_SIZE @GCE_SMALL_MSG_SIZE@ 23 | #define GCE_MSG_MIN_GROW_SIZE @GCE_MSG_MIN_GROW_SIZE@ 24 | #define GCE_DEFAULT_REQUEST_TIMEOUT_SEC @GCE_DEFAULT_REQUEST_TIMEOUT_SEC@ 25 | 26 | #cmakedefine GCE_ACTOR_BUILD_EXAMPLE 27 | #cmakedefine GCE_ACTOR_BUILD_TEST 28 | 29 | #endif /// GCE_ACTOR_USER_HPP 30 | -------------------------------------------------------------------------------- /libs/actor/example/cluster/master.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_EXAMPLE_CLUSTER_MASTER_HPP 11 | #define GCE_ACTOR_EXAMPLE_CLUSTER_MASTER_HPP 12 | 13 | #include "app.hpp" 14 | #include 15 | #include 16 | #include 17 | 18 | class master 19 | { 20 | public: 21 | master( 22 | std::vector const& gate_list, 23 | std::vector const& game_list, 24 | std::vector const& router_list 25 | ); 26 | ~master(); 27 | 28 | public: 29 | gce::aid_t start(gce::actor& sire); 30 | 31 | private: 32 | void run(gce::actor&); 33 | 34 | private: 35 | std::vector const gate_list_; 36 | std::vector const game_list_; 37 | std::vector const router_list_; 38 | }; 39 | 40 | #endif /// GCE_ACTOR_EXAMPLE_CLUSTER_MASTER_HPP 41 | -------------------------------------------------------------------------------- /gce/actor/impl/tcp/acceptor.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_IMPL_TCP_ACCEPTOR_HPP 11 | #define GCE_ACTOR_IMPL_TCP_ACCEPTOR_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | namespace gce 18 | { 19 | namespace detail 20 | { 21 | class basic_socket; 22 | } 23 | namespace tcp 24 | { 25 | class acceptor 26 | : public gce::detail::basic_acceptor 27 | { 28 | public: 29 | acceptor(strand_t&, std::string const&, boost::uint16_t); 30 | ~acceptor(); 31 | 32 | public: 33 | void bind(); 34 | gce::detail::socket_ptr accept(yield_t); 35 | void close(); 36 | 37 | private: 38 | strand_t& snd_; 39 | boost::asio::ip::tcp::acceptor acpr_; 40 | std::string const host_; 41 | boost::uint16_t const port_; 42 | }; 43 | } 44 | } 45 | 46 | #endif /// GCE_ACTOR_IMPL_TCP_ACCEPTOR_HPP 47 | -------------------------------------------------------------------------------- /libs/actor/example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is part of the CMake build system for Gce 3 | # 4 | # CMake auto-generated configuration options. 5 | # Do not check in modified versions of this file. 6 | # 7 | # Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 8 | # 9 | # Distributed under the Boost Software License, Version 1.0. (See accompanying 10 | # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 11 | # 12 | # See https://github.com/nousxiong/gce for latest version. 13 | # 14 | 15 | # Build examples. 16 | set (EXAMPLES_LINK_LIBS gce_actor ${LINK_LIBS}) 17 | 18 | if (GCE_STATIC) 19 | set (GCE_LINK_PROP "${GCE_LINK_PROP} -static -static-libgcc -static-libstdc++") 20 | endif () 21 | 22 | # Gce work with asio. 23 | add_subdirectory (asio) 24 | 25 | # Gce cluster. 26 | add_subdirectory (cluster) 27 | 28 | 29 | 30 | # Gce cluster client. 31 | add_subdirectory (cluster_client) 32 | 33 | 34 | 35 | # Helloworld. 36 | add_subdirectory (helloworld) 37 | 38 | # Link. 39 | add_subdirectory (link) 40 | 41 | # Matching. 42 | add_subdirectory (matching) 43 | 44 | # Two actors pingpong. 45 | add_subdirectory (pingpong) 46 | 47 | -------------------------------------------------------------------------------- /gce/actor/detail/basic_socket.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_BASIC_SOCKET_HPP 11 | #define GCE_ACTOR_DETAIL_BASIC_SOCKET_HPP 12 | 13 | #include 14 | #include 15 | 16 | namespace gce 17 | { 18 | namespace detail 19 | { 20 | class cache_pool; 21 | class basic_socket 22 | { 23 | public: 24 | basic_socket() {} 25 | virtual ~basic_socket() {} 26 | 27 | public: 28 | virtual void init(cache_pool*) = 0; 29 | virtual void send(byte_t const*, std::size_t, byte_t const*, std::size_t) = 0; 30 | virtual std::size_t recv(byte_t*, std::size_t, yield_t) = 0; 31 | virtual void connect(yield_t) = 0; 32 | virtual void close() = 0; 33 | virtual void wait_end(yield_t) = 0; 34 | virtual void reset() = 0; 35 | }; 36 | typedef boost::shared_ptr socket_ptr; 37 | } 38 | } 39 | 40 | #endif /// GCE_ACTOR_DETAIL_BASIC_SOCKET_HPP 41 | -------------------------------------------------------------------------------- /gce/actor/net_option.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_NET_OPTION_HPP 11 | #define GCE_ACTOR_NET_OPTION_HPP 12 | 13 | #include 14 | 15 | namespace gce 16 | { 17 | struct net_option 18 | { 19 | net_option() 20 | : heartbeat_period_(30) 21 | , heartbeat_count_(3) 22 | , init_reconn_period_(3) 23 | , init_reconn_try_(2) 24 | , reconn_period_(10) 25 | , reconn_try_(3) 26 | { 27 | } 28 | 29 | seconds_t heartbeat_period_; 30 | std::size_t heartbeat_count_; 31 | seconds_t init_reconn_period_; /// init conn, between two connects' period 32 | std::size_t init_reconn_try_; /// init conn, how many try to reconnect before give up 33 | seconds_t reconn_period_; /// in one reconn, between two connects' period 34 | std::size_t reconn_try_; /// how many try to reconnect before drop cache msgs 35 | }; 36 | } 37 | 38 | #endif /// GCE_ACTOR_NET_OPTION_HPP 39 | -------------------------------------------------------------------------------- /libs/actor/example/cluster_client/client.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_EXAMPLE_CLUSTER_CLIENT_CLIENT_HPP 11 | #define GCE_ACTOR_EXAMPLE_CLUSTER_CLIENT_CLIENT_HPP 12 | 13 | #include "socket.hpp" 14 | #include 15 | #include 16 | #include 17 | 18 | class client 19 | { 20 | public: 21 | explicit client(std::string); 22 | ~client(); 23 | 24 | public: 25 | void run(); 26 | 27 | private: 28 | void command(std::string cmd, gce::aid_t); 29 | boost::array parse_potocol(std::string); 30 | void pri_run(gce::actor&); 31 | void recv(gce::actor&, tcp_socket&); 32 | gce::attributes get_attrs(); 33 | 34 | private: 35 | boost::array gate_ep_; 36 | gce::context ctx_; 37 | gce::actor base_; 38 | 39 | std::string username_; 40 | }; 41 | 42 | #endif /// GCE_ACTOR_EXAMPLE_CLUSTER_CLIENT_CLIENT_HPP 43 | -------------------------------------------------------------------------------- /libs/actor/example/asio/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is part of the CMake build system for Gce 3 | # 4 | # CMake auto-generated configuration options. 5 | # Do not check in modified versions of this file. 6 | # 7 | # Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 8 | # 9 | # Distributed under the Boost Software License, Version 1.0. (See accompanying 10 | # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 11 | # 12 | # See https://github.com/nousxiong/gce for latest version. 13 | # 14 | 15 | if (WIN32) 16 | if (${CMAKE_GENERATOR} MATCHES "Visual Studio 11 *" OR ${CMAKE_GENERATOR} MATCHES "Visual Studio 12 *") 17 | set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO") 18 | endif () 19 | endif () 20 | 21 | file (GLOB_RECURSE GCE_ACTOR_EXAMPLE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") 22 | file (GLOB_RECURSE GCE_ACTOR_EXAMPLE_FILES ${GCE_ACTOR_EXAMPLE_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp") 23 | add_executable (gce_actor_asio ${GCE_ACTOR_EXAMPLE_FILES}) 24 | 25 | if (GCE_LINK_PROP) 26 | set_target_properties (gce_actor_asio PROPERTIES LINK_FLAGS "${GCE_LINK_PROP}") 27 | endif () 28 | 29 | target_link_libraries (gce_actor_asio ${EXAMPLES_LINK_LIBS}) 30 | install (TARGETS gce_actor_asio RUNTIME DESTINATION bin) 31 | 32 | -------------------------------------------------------------------------------- /libs/actor/example/link/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is part of the CMake build system for Gce 3 | # 4 | # CMake auto-generated configuration options. 5 | # Do not check in modified versions of this file. 6 | # 7 | # Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 8 | # 9 | # Distributed under the Boost Software License, Version 1.0. (See accompanying 10 | # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 11 | # 12 | # See https://github.com/nousxiong/gce for latest version. 13 | # 14 | 15 | if (WIN32) 16 | if (${CMAKE_GENERATOR} MATCHES "Visual Studio 11 *" OR ${CMAKE_GENERATOR} MATCHES "Visual Studio 12 *") 17 | set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO") 18 | endif () 19 | endif () 20 | 21 | file (GLOB_RECURSE GCE_ACTOR_EXAMPLE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") 22 | file (GLOB_RECURSE GCE_ACTOR_EXAMPLE_FILES ${GCE_ACTOR_EXAMPLE_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp") 23 | add_executable (gce_actor_link ${GCE_ACTOR_EXAMPLE_FILES}) 24 | 25 | if (GCE_LINK_PROP) 26 | set_target_properties (gce_actor_link PROPERTIES LINK_FLAGS "${GCE_LINK_PROP}") 27 | endif () 28 | 29 | target_link_libraries (gce_actor_link ${EXAMPLES_LINK_LIBS}) 30 | install (TARGETS gce_actor_link RUNTIME DESTINATION bin) 31 | 32 | -------------------------------------------------------------------------------- /libs/actor/example/link/main.cpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #include 11 | #include 12 | 13 | void quiter(gce::actor& self) 14 | { 15 | /// wait for gce::exit from link actor 16 | gce::recv(self); 17 | } 18 | 19 | void link(gce::actor& self) 20 | { 21 | /// create 10 actor and link with them 22 | for (std::size_t i=0; i<10; ++i) 23 | { 24 | gce::spawn(self, boost::bind(&quiter, _1), gce::linked); 25 | } 26 | 27 | /// quit, will send 10 gce::exit to quiter actors 28 | /// and 1 gce::exit to base actor(in main) 29 | } 30 | 31 | int main() 32 | { 33 | gce::context ctx; 34 | 35 | /// spawn a thread_mapped_actor 36 | gce::actor base = gce::spawn(ctx); 37 | 38 | /// create a link actor and monitor it. 39 | gce::spawn(base, boost::bind(&link, _1), gce::monitored); 40 | 41 | /// wait for gce::exit message 42 | gce::recv(base); 43 | 44 | std::cout << "end" << std::endl; 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /libs/actor/example/cluster/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is part of the CMake build system for Gce 3 | # 4 | # CMake auto-generated configuration options. 5 | # Do not check in modified versions of this file. 6 | # 7 | # Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 8 | # 9 | # Distributed under the Boost Software License, Version 1.0. (See accompanying 10 | # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 11 | # 12 | # See https://github.com/nousxiong/gce for latest version. 13 | # 14 | 15 | if (WIN32) 16 | if (${CMAKE_GENERATOR} MATCHES "Visual Studio 11 *" OR ${CMAKE_GENERATOR} MATCHES "Visual Studio 12 *") 17 | set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO") 18 | endif () 19 | endif () 20 | 21 | file (GLOB_RECURSE GCE_ACTOR_EXAMPLE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") 22 | file (GLOB_RECURSE GCE_ACTOR_EXAMPLE_FILES ${GCE_ACTOR_EXAMPLE_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp") 23 | add_executable (gce_actor_cluster ${GCE_ACTOR_EXAMPLE_FILES}) 24 | 25 | if (GCE_LINK_PROP) 26 | set_target_properties (gce_actor_cluster PROPERTIES LINK_FLAGS "${GCE_LINK_PROP}") 27 | endif () 28 | 29 | target_link_libraries (gce_actor_cluster ${EXAMPLES_LINK_LIBS}) 30 | install (TARGETS gce_actor_cluster RUNTIME DESTINATION bin) 31 | 32 | -------------------------------------------------------------------------------- /libs/actor/example/matching/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is part of the CMake build system for Gce 3 | # 4 | # CMake auto-generated configuration options. 5 | # Do not check in modified versions of this file. 6 | # 7 | # Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 8 | # 9 | # Distributed under the Boost Software License, Version 1.0. (See accompanying 10 | # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 11 | # 12 | # See https://github.com/nousxiong/gce for latest version. 13 | # 14 | 15 | if (WIN32) 16 | if (${CMAKE_GENERATOR} MATCHES "Visual Studio 11 *" OR ${CMAKE_GENERATOR} MATCHES "Visual Studio 12 *") 17 | set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO") 18 | endif () 19 | endif () 20 | 21 | file (GLOB_RECURSE GCE_ACTOR_EXAMPLE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") 22 | file (GLOB_RECURSE GCE_ACTOR_EXAMPLE_FILES ${GCE_ACTOR_EXAMPLE_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp") 23 | add_executable (gce_actor_matching ${GCE_ACTOR_EXAMPLE_FILES}) 24 | 25 | if (GCE_LINK_PROP) 26 | set_target_properties (gce_actor_matching PROPERTIES LINK_FLAGS "${GCE_LINK_PROP}") 27 | endif () 28 | 29 | target_link_libraries (gce_actor_matching ${EXAMPLES_LINK_LIBS}) 30 | install (TARGETS gce_actor_matching RUNTIME DESTINATION bin) 31 | 32 | -------------------------------------------------------------------------------- /libs/actor/example/pingpong/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is part of the CMake build system for Gce 3 | # 4 | # CMake auto-generated configuration options. 5 | # Do not check in modified versions of this file. 6 | # 7 | # Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 8 | # 9 | # Distributed under the Boost Software License, Version 1.0. (See accompanying 10 | # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 11 | # 12 | # See https://github.com/nousxiong/gce for latest version. 13 | # 14 | 15 | if (WIN32) 16 | if (${CMAKE_GENERATOR} MATCHES "Visual Studio 11 *" OR ${CMAKE_GENERATOR} MATCHES "Visual Studio 12 *") 17 | set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO") 18 | endif () 19 | endif () 20 | 21 | file (GLOB_RECURSE GCE_ACTOR_EXAMPLE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") 22 | file (GLOB_RECURSE GCE_ACTOR_EXAMPLE_FILES ${GCE_ACTOR_EXAMPLE_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp") 23 | add_executable (gce_actor_pingpong ${GCE_ACTOR_EXAMPLE_FILES}) 24 | 25 | if (GCE_LINK_PROP) 26 | set_target_properties (gce_actor_pingpong PROPERTIES LINK_FLAGS "${GCE_LINK_PROP}") 27 | endif () 28 | 29 | target_link_libraries (gce_actor_pingpong ${EXAMPLES_LINK_LIBS}) 30 | install (TARGETS gce_actor_pingpong RUNTIME DESTINATION bin) 31 | 32 | -------------------------------------------------------------------------------- /libs/actor/example/helloworld/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is part of the CMake build system for Gce 3 | # 4 | # CMake auto-generated configuration options. 5 | # Do not check in modified versions of this file. 6 | # 7 | # Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 8 | # 9 | # Distributed under the Boost Software License, Version 1.0. (See accompanying 10 | # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 11 | # 12 | # See https://github.com/nousxiong/gce for latest version. 13 | # 14 | 15 | if (WIN32) 16 | if (${CMAKE_GENERATOR} MATCHES "Visual Studio 11 *" OR ${CMAKE_GENERATOR} MATCHES "Visual Studio 12 *") 17 | set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO") 18 | endif () 19 | endif () 20 | 21 | file (GLOB_RECURSE GCE_ACTOR_EXAMPLE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") 22 | file (GLOB_RECURSE GCE_ACTOR_EXAMPLE_FILES ${GCE_ACTOR_EXAMPLE_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp") 23 | add_executable (gce_actor_helloworld ${GCE_ACTOR_EXAMPLE_FILES}) 24 | 25 | if (GCE_LINK_PROP) 26 | set_target_properties (gce_actor_helloworld PROPERTIES LINK_FLAGS "${GCE_LINK_PROP}") 27 | endif () 28 | 29 | target_link_libraries (gce_actor_helloworld ${EXAMPLES_LINK_LIBS}) 30 | install (TARGETS gce_actor_helloworld RUNTIME DESTINATION bin) 31 | 32 | -------------------------------------------------------------------------------- /libs/actor/example/cluster_client/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is part of the CMake build system for Gce 3 | # 4 | # CMake auto-generated configuration options. 5 | # Do not check in modified versions of this file. 6 | # 7 | # Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 8 | # 9 | # Distributed under the Boost Software License, Version 1.0. (See accompanying 10 | # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 11 | # 12 | # See https://github.com/nousxiong/gce for latest version. 13 | # 14 | 15 | if (WIN32) 16 | if (${CMAKE_GENERATOR} MATCHES "Visual Studio 11 *" OR ${CMAKE_GENERATOR} MATCHES "Visual Studio 12 *") 17 | set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO") 18 | endif () 19 | endif () 20 | 21 | file (GLOB_RECURSE GCE_ACTOR_EXAMPLE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") 22 | file (GLOB_RECURSE GCE_ACTOR_EXAMPLE_FILES ${GCE_ACTOR_EXAMPLE_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp") 23 | add_executable (gce_actor_cluster_client ${GCE_ACTOR_EXAMPLE_FILES}) 24 | 25 | if (GCE_LINK_PROP) 26 | set_target_properties (gce_actor_cluster_client PROPERTIES LINK_FLAGS "${GCE_LINK_PROP}") 27 | endif () 28 | 29 | target_link_libraries (gce_actor_cluster_client ${EXAMPLES_LINK_LIBS}) 30 | install (TARGETS gce_actor_cluster_client RUNTIME DESTINATION bin) 31 | 32 | -------------------------------------------------------------------------------- /gce/actor/response.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_RESPONSE_HPP 11 | #define GCE_ACTOR_RESPONSE_HPP 12 | 13 | #include 14 | #include 15 | 16 | namespace gce 17 | { 18 | class response_t 19 | { 20 | public: 21 | response_t() : id_(sid_nil) {} 22 | response_t(sid_t id, aid_t aid) : id_(id), aid_(aid){} 23 | response_t(sid_t id, aid_t aid, aid_t recver) : id_(id), aid_(aid), recver_(recver) {} 24 | response_t(sid_t id, aid_t aid, svcid_t svc) : id_(id), aid_(aid), svc_(svc) {} 25 | ~response_t() {} 26 | 27 | public: 28 | inline bool valid() const { return id_ != sid_nil; } 29 | inline sid_t get_id() const { return id_; } 30 | inline aid_t get_aid() const { return aid_; } 31 | 32 | /// for local use 33 | inline aid_t get_recver() const { return recver_; } 34 | inline svcid_t get_svcid() const { return svc_; } 35 | 36 | private: 37 | sid_t id_; 38 | aid_t aid_; 39 | 40 | /// for local use 41 | aid_t recver_; 42 | svcid_t svc_; 43 | }; 44 | } 45 | 46 | #endif /// GCE_ACTOR_RESPONSE_HPP 47 | -------------------------------------------------------------------------------- /libs/actor/example/cluster/node.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_EXAMPLE_CLUSTER_NODE_HPP 11 | #define GCE_ACTOR_EXAMPLE_CLUSTER_NODE_HPP 12 | 13 | #include "master.hpp" 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | class node 21 | { 22 | public: 23 | node( 24 | gce::attributes attrs, 25 | std::string master_ep, 26 | bool is_master, 27 | gce::ctxid_t master_node_id, 28 | std::vector const& gate_list, 29 | std::vector const& game_list, 30 | std::vector const& router_list 31 | ); 32 | ~node(); 33 | 34 | void wait_end(); 35 | 36 | private: 37 | void run(gce::actor&, gce::svcid_t master); 38 | void handle_signal(gce::actor&); 39 | 40 | private: 41 | gce::context ctx_; 42 | gce::actor base_; 43 | boost::asio::signal_set sig_; 44 | boost::scoped_ptr master_; 45 | }; 46 | 47 | #endif /// GCE_ACTOR_EXAMPLE_CLUSTER_NODE_HPP 48 | -------------------------------------------------------------------------------- /License.txt: -------------------------------------------------------------------------------- 1 | Boost Software License - Version 1.0 - August 17th, 2003 2 | 3 | Permission is hereby granted, free of charge, to any person or organization 4 | obtaining a copy of the software and accompanying documentation covered by 5 | this license (the "Software") to use, reproduce, display, distribute, 6 | execute, and transmit the Software, and to prepare derivative works of the 7 | Software, and to permit third-parties to whom the Software is furnished to 8 | do so, all subject to the following: 9 | 10 | The copyright notices in the Software and this entire statement, including 11 | the above license grant, this restriction and the following disclaimer, 12 | must be included in all copies of the Software, in whole or in part, and 13 | all derivative works of the Software, unless such copies or derivative 14 | works are solely in the form of machine-executable object code generated by 15 | a source language processor. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 20 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 21 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 22 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /libs/actor/example/cluster/gate.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_EXAMPLE_CLUSTER_GATE_HPP 11 | #define GCE_ACTOR_EXAMPLE_CLUSTER_GATE_HPP 12 | 13 | #include "conn.hpp" 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | class gate 20 | { 21 | typedef boost::asio::ip::tcp::acceptor acceptor_t; 22 | 23 | public: 24 | static gce::aid_t start( 25 | gce::actor& sire, 26 | gce::match_t svc_name, 27 | std::string cln_ep, 28 | app_ctxid_list_t game_list 29 | ); 30 | 31 | private: 32 | static void run( 33 | gce::actor& self, 34 | gce::match_t svc_name, 35 | std::string cln_ep, 36 | app_ctxid_list_t game_list 37 | ); 38 | static void accept( 39 | gce::actor& self, 40 | acceptor_t& acpr, 41 | app_ctxid_list_t game_list, 42 | std::vector conn_group_list 43 | ); 44 | static void conn_group( 45 | gce::actor& self, 46 | gce::aid_t ga_id 47 | ); 48 | }; 49 | 50 | #endif /// GCE_ACTOR_EXAMPLE_CLUSTER_GATE_HPP 51 | -------------------------------------------------------------------------------- /libs/actor/example/matching/main.cpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #include 11 | #include 12 | 13 | void echo(gce::actor& self) 14 | { 15 | /// wait for "start" message. 16 | /// if and only if after fetch "start", then others 17 | gce::recv(self, gce::atom("start")); 18 | std::cout << "start!" << std::endl; 19 | 20 | /// handle other messages 21 | gce::message msg; 22 | gce::aid_t sender = self.recv(msg); 23 | std::cout << "recv message: " << gce::atom(msg.get_type()) << std::endl; 24 | 25 | /// reply 26 | gce::send(self, sender); 27 | } 28 | 29 | int main() 30 | { 31 | gce::context ctx; 32 | 33 | /// spawn a thread_mapped_actor 34 | gce::actor base = gce::spawn(ctx); 35 | 36 | gce::aid_t echo_actor = gce::spawn(base, boost::bind(&echo, _1)); 37 | 38 | /// send "hi" message to echo 39 | gce::send(base, echo_actor, gce::atom("hi")); 40 | 41 | /// send "start" message to echo, after "hi" message 42 | gce::send(base, echo_actor, gce::atom("start")); 43 | 44 | /// ... and wait for a response 45 | gce::recv(base); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /gce/amsg/License.txt: -------------------------------------------------------------------------------- 1 | Boost Software License - Version 1.0 - August 17th, 2003 2 | 3 | Permission is hereby granted, free of charge, to any person or organization 4 | obtaining a copy of the software and accompanying documentation covered by 5 | this license (the "Software") to use, reproduce, display, distribute, 6 | execute, and transmit the Software, and to prepare derivative works of the 7 | Software, and to permit third-parties to whom the Software is furnished to 8 | do so, all subject to the following: 9 | 10 | The copyright notices in the Software and this entire statement, including 11 | the above license grant, this restriction and the following disclaimer, 12 | must be included in all copies of the Software, in whole or in part, and 13 | all derivative works of the Software, unless such copies or derivative 14 | works are solely in the form of machine-executable object code generated by 15 | a source language processor. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 20 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 21 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 22 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 | DEALINGS IN THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /libs/actor/test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is part of the CMake build system for Gce 3 | # 4 | # CMake auto-generated configuration options. 5 | # Do not check in modified versions of this file. 6 | # 7 | # Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 8 | # 9 | # Distributed under the Boost Software License, Version 1.0. (See accompanying 10 | # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 11 | # 12 | # See https://github.com/nousxiong/gce for latest version. 13 | # 14 | 15 | if (WIN32) 16 | if (${CMAKE_GENERATOR} MATCHES "Visual Studio 11 *" OR ${CMAKE_GENERATOR} MATCHES "Visual Studio 12 *") 17 | set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SAFESEH:NO") 18 | endif () 19 | endif () 20 | 21 | # Build tests. 22 | set (TESTS_LINK_LIBS gce_actor ${LINK_LIBS}) 23 | file (GLOB_RECURSE GCE_ACTOR_UNIT_TEST_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") 24 | file (GLOB_RECURSE GCE_ACTOR_UNIT_TEST_FILES ${GCE_ACTOR_UNIT_TEST_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp") 25 | add_executable (gce_actor_ut ${GCE_ACTOR_UNIT_TEST_FILES}) 26 | 27 | if (GCE_STATIC) 28 | set (GCE_LINK_PROP "${GCE_LINK_PROP} -static -static-libgcc -static-libstdc++") 29 | endif () 30 | if (GCE_LINK_PROP) 31 | set_target_properties (gce_actor_ut PROPERTIES LINK_FLAGS "${GCE_LINK_PROP}") 32 | endif () 33 | 34 | target_link_libraries (gce_actor_ut ${TESTS_LINK_LIBS}) 35 | install (TARGETS gce_actor_ut RUNTIME DESTINATION bin) 36 | -------------------------------------------------------------------------------- /gce/actor/detail/exit.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_EXIT_HPP 11 | #define GCE_ACTOR_DETAIL_EXIT_HPP 12 | 13 | #include 14 | #include 15 | 16 | namespace gce 17 | { 18 | namespace detail 19 | { 20 | class exit_t 21 | { 22 | public: 23 | exit_t() : ec_(exit_normal) {} 24 | exit_t(exit_code_t ec, aid_t aid) : ec_(ec), aid_(aid) {} 25 | ~exit_t() {} 26 | 27 | public: 28 | inline exit_code_t get_code() const { return ec_; } 29 | inline aid_t get_aid() const { return aid_; } 30 | 31 | private: 32 | exit_code_t ec_; 33 | aid_t aid_; 34 | }; 35 | 36 | class fwd_exit_t 37 | { 38 | public: 39 | fwd_exit_t() : ec_(exit_normal) {} 40 | fwd_exit_t(exit_code_t ec, aid_t aid, aid_t skt) 41 | : ec_(ec) 42 | , aid_(aid) 43 | , skt_(skt) 44 | { 45 | } 46 | 47 | ~fwd_exit_t() 48 | { 49 | } 50 | 51 | public: 52 | inline exit_code_t get_code() const { return ec_; } 53 | inline aid_t get_aid() const { return aid_; } 54 | inline aid_t get_skt() const { return skt_; } 55 | 56 | private: 57 | exit_code_t ec_; 58 | aid_t aid_; 59 | aid_t skt_; 60 | }; 61 | } 62 | } 63 | 64 | #endif /// GCE_ACTOR_DETAIL_EXIT_HPP 65 | -------------------------------------------------------------------------------- /gce/actor/detail/link.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_LINK_HPP 11 | #define GCE_ACTOR_DETAIL_LINK_HPP 12 | 13 | #include 14 | #include 15 | 16 | namespace gce 17 | { 18 | namespace detail 19 | { 20 | class link_t 21 | { 22 | public: 23 | link_t() : type_(no_link) {} 24 | link_t(link_type type, aid_t aid) : type_(type), aid_(aid) {} 25 | ~link_t() {} 26 | 27 | public: 28 | inline link_type get_type() const { return type_; } 29 | inline aid_t get_aid() const { return aid_; } 30 | 31 | private: 32 | link_type type_; 33 | aid_t aid_; 34 | }; 35 | 36 | class fwd_link_t 37 | { 38 | public: 39 | fwd_link_t() : type_(no_link) {} 40 | fwd_link_t(link_type type, aid_t aid, aid_t skt) 41 | : type_(type) 42 | , aid_(aid) 43 | , skt_(skt) 44 | { 45 | } 46 | 47 | ~fwd_link_t() 48 | { 49 | } 50 | 51 | public: 52 | inline link_type get_type() const { return type_; } 53 | inline aid_t get_aid() const { return aid_; } 54 | inline aid_t get_skt() const { return skt_; } 55 | 56 | private: 57 | link_type type_; 58 | aid_t aid_; 59 | aid_t skt_; 60 | }; 61 | } 62 | } 63 | 64 | #endif /// GCE_ACTOR_DETAIL_LINK_HPP 65 | 66 | 67 | -------------------------------------------------------------------------------- /gce/actor/detail/scoped_bool.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_SCOPED_BOOL_HPP 11 | #define GCE_ACTOR_DETAIL_SCOPED_BOOL_HPP 12 | 13 | #include 14 | 15 | namespace gce 16 | { 17 | namespace detail 18 | { 19 | template 20 | struct scoped_bool 21 | { 22 | explicit scoped_bool(Bool& flag, Bool val = true) 23 | : flag_(flag) 24 | { 25 | flag_ = val; 26 | } 27 | 28 | ~scoped_bool() 29 | { 30 | flag_ = !flag_; 31 | } 32 | 33 | Bool& flag_; 34 | }; 35 | 36 | template <> 37 | struct scoped_bool 38 | { 39 | explicit scoped_bool( 40 | boost::atomic_bool& flag, 41 | bool val = true, 42 | boost::memory_order begin_order = boost::memory_order_release, 43 | boost::memory_order end_order = boost::memory_order_release 44 | ) 45 | : flag_(flag) 46 | , val_(!val) 47 | , end_order_(end_order) 48 | { 49 | flag_.store(val, begin_order); 50 | } 51 | 52 | ~scoped_bool() 53 | { 54 | flag_.store(val_, end_order_); 55 | } 56 | 57 | boost::atomic_bool& flag_; 58 | bool val_; 59 | boost::memory_order end_order_; 60 | }; 61 | } 62 | } 63 | 64 | #endif /// GCE_ACTOR_DETAIL_SCOPED_BOOL_HPP 65 | -------------------------------------------------------------------------------- /gce/detail/scope.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_DETAIL_SCOPE_HPP 11 | #define GCE_DETAIL_SCOPE_HPP 12 | 13 | #include 14 | #include 15 | 16 | namespace gce 17 | { 18 | namespace detail 19 | { 20 | class scope 21 | { 22 | scope(scope const&); 23 | scope& operator=(scope const&); 24 | 25 | void operator==(scope const&) const; 26 | void operator!=(scope const&) const; 27 | 28 | public: 29 | typedef boost::function cb_t; 30 | 31 | public: 32 | scope() 33 | { 34 | } 35 | 36 | template 37 | explicit scope(F f) 38 | : cb_(f) 39 | { 40 | } 41 | 42 | template 43 | explicit scope(F f, A a) 44 | : cb_(f, a) 45 | { 46 | } 47 | 48 | ~scope() 49 | { 50 | if (cb_) 51 | { 52 | cb_(); 53 | } 54 | } 55 | 56 | public: 57 | void reset() 58 | { 59 | cb_.clear(); 60 | } 61 | 62 | void reset(cb_t const& cb) 63 | { 64 | cb_ = cb; 65 | } 66 | 67 | operator bool() const 68 | { 69 | return cb_ != 0; 70 | } 71 | 72 | bool operator!() const 73 | { 74 | return cb_ == 0; 75 | } 76 | 77 | private: 78 | cb_t cb_; 79 | }; 80 | } 81 | } 82 | 83 | #endif /// GCE_DETAIL_SCOPE_HPP 84 | -------------------------------------------------------------------------------- /gce/integer.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// integer.hpp 3 | /// 4 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 5 | /// 6 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 7 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 8 | /// 9 | /// See https://github.com/nousxiong/gce for latest version. 10 | /// 11 | 12 | #ifndef GCE_INTEGER_HPP 13 | #define GCE_INTEGER_HPP 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | namespace gce 22 | { 23 | typedef boost::mpl::if_< 24 | boost::is_same, boost::mpl::int_<4> >, 25 | boost::uint32_t, boost::uint64_t 26 | >::type uintptr_t; 27 | 28 | typedef unsigned char byte_t; 29 | 30 | static byte_t const byte_nil = static_cast(-1); 31 | static boost::int8_t const i8_nil = static_cast(-1); 32 | static boost::int16_t const i16_nil = static_cast(-1); 33 | static boost::int32_t const i32_nil = static_cast(-1); 34 | static boost::int64_t const i64_nil = static_cast(-1); 35 | static boost::uint8_t const u8_nil = static_cast(-1); 36 | static boost::uint16_t const u16_nil = static_cast(-1); 37 | static boost::uint32_t const u32_nil = static_cast(-1); 38 | static boost::uint64_t const u64_nil = static_cast(-1); 39 | static std::size_t const size_nil = static_cast(-1); 40 | } 41 | 42 | #endif /// GCE_INTEGER_HPP 43 | -------------------------------------------------------------------------------- /libs/actor/example/helloworld/main.cpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | void mirror(gce::actor& self) 16 | { 17 | /// wait for messages 18 | gce::message msg; 19 | gce::aid_t aid = self.recv(msg); 20 | std::string what; 21 | msg >> what; 22 | 23 | /// prints "Hello World!" 24 | std::cout << what << std::endl; 25 | 26 | /// replies "!dlroW olleH" 27 | gce::message m; 28 | m << std::string(what.rbegin(), what.rend()); 29 | self.send(aid, m); 30 | } 31 | 32 | int main() 33 | { 34 | /// everything begin here 35 | gce::context ctx; 36 | 37 | /// spawn a thread_mapped_actor 38 | gce::actor hello_world = gce::spawn(ctx); 39 | 40 | /// create a new actor that calls ’mirror(gce::actor&)’, using coroutine-base actor 41 | gce::aid_t mirror_actor = gce::spawn(hello_world, boost::bind(&mirror, _1)); 42 | 43 | /// send "Hello World!" to mirror 44 | gce::message m; 45 | m << std::string("Hello World!"); 46 | hello_world.send(mirror_actor, m); 47 | 48 | /// ... and wait for a response 49 | gce::message msg; 50 | gce::aid_t aid = hello_world.recv(msg); 51 | BOOST_ASSERT(aid == mirror_actor); 52 | std::string reply_str; 53 | msg >> reply_str; 54 | 55 | /// prints "!dlroW olleH" 56 | std::cout << reply_str << std::endl; 57 | 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /gce/actor/detail/heartbeat.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_HEARTBEAT_HPP 11 | #define GCE_ACTOR_DETAIL_HEARTBEAT_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | namespace gce 18 | { 19 | namespace detail 20 | { 21 | class cache_pool; 22 | class heartbeat 23 | { 24 | typedef boost::function timeout_func_t; 25 | 26 | public: 27 | explicit heartbeat(strand_t&); 28 | ~heartbeat(); 29 | 30 | public: 31 | template 32 | void init( 33 | seconds_t period, std::size_t max_count, 34 | F f, F t = timeout_func_t() 35 | ) 36 | { 37 | clear(); 38 | period_ = period; 39 | max_count_ = max_count; 40 | if (max_count_ == 0) 41 | { 42 | max_count_ = 1; 43 | } 44 | timeout_ = f; 45 | tick_ = t; 46 | } 47 | 48 | void start(); 49 | void stop(); 50 | void beat(); 51 | void wait_end(yield_t); 52 | 53 | void clear(); 54 | 55 | private: 56 | void start_timer(); 57 | void handle_timeout(errcode_t const&); 58 | 59 | private: 60 | strand_t& snd_; 61 | timer_t tmr_; 62 | timer_t sync_; 63 | seconds_t period_; 64 | std::size_t max_count_; 65 | std::size_t curr_count_; 66 | 67 | timeout_func_t timeout_; 68 | timeout_func_t tick_; 69 | bool stopped_; 70 | std::size_t waiting_; 71 | }; 72 | } 73 | } 74 | 75 | #endif /// GCE_ACTOR_DETAIL_HEARTBEAT_HPP 76 | -------------------------------------------------------------------------------- /libs/actor/test/test_mixin_pingpong.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class mixin_pingpong_ut 13 | { 14 | static std::size_t const msg_size = 10000; 15 | public: 16 | static void run() 17 | { 18 | std::cout << "mixin_pingpong_ut begin." << std::endl; 19 | test(); 20 | std::cout << "mixin_pingpong_ut end." << std::endl; 21 | } 22 | 23 | private: 24 | static void pong_actor(actor pong) 25 | { 26 | message msg; 27 | while (true) 28 | { 29 | aid_t sender = pong.recv(msg); 30 | if (msg.get_type() == 2) 31 | { 32 | break; 33 | } 34 | else 35 | { 36 | pong.send(sender, msg); 37 | } 38 | } 39 | } 40 | 41 | static void test() 42 | { 43 | try 44 | { 45 | attributes attrs; 46 | attrs.thread_num_ = 2; 47 | context ctx(attrs); 48 | 49 | actor ping = spawn(ctx); 50 | actor pong = spawn(ctx); 51 | aid_t pong_id = pong.get_aid(); 52 | boost::thread thr( 53 | boost::bind( 54 | &mixin_pingpong_ut::pong_actor, pong 55 | ) 56 | ); 57 | 58 | boost::timer::auto_cpu_timer t; 59 | message m(1); 60 | for (std::size_t i=0; i& self) 25 | { 26 | message msg; 27 | while (true) 28 | { 29 | aid_t sender = self.recv(msg); 30 | if (msg.get_type() == 2) 31 | { 32 | break; 33 | } 34 | else 35 | { 36 | self.send(sender, msg); 37 | } 38 | } 39 | } 40 | 41 | static void test() 42 | { 43 | try 44 | { 45 | attributes attrs; 46 | attrs.thread_num_ = 2; 47 | context ctx(attrs); 48 | actor base = spawn(ctx); 49 | 50 | actor ping = spawn(base); 51 | 52 | aid_t pong_id = 53 | spawn( 54 | base, 55 | boost::bind( 56 | &slice_pingpong_ut::pong_actor, _1 57 | ) 58 | ); 59 | 60 | boost::timer::auto_cpu_timer t; 61 | message m(1); 62 | for (std::size_t i=0; i 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | namespace gce 20 | { 21 | namespace detail 22 | { 23 | class buffer 24 | : public ref_count 25 | { 26 | public: 27 | buffer() 28 | : ref_count(boost::bind(&buffer::free, this)) 29 | , data_(0) 30 | , size_(0) 31 | { 32 | } 33 | 34 | explicit buffer(std::size_t size) 35 | : ref_count(boost::bind(&buffer::free, this)) 36 | , data_((byte_t*)std::malloc(size)) 37 | , size_(size) 38 | { 39 | if (size > 0 && !data_) 40 | { 41 | throw std::bad_alloc(); 42 | } 43 | } 44 | 45 | ~buffer() 46 | { 47 | if (data_) 48 | { 49 | std::free(data_); 50 | } 51 | } 52 | 53 | public: 54 | inline byte_t* data() { return data_; } 55 | inline std::size_t size() const { return size_; } 56 | 57 | inline void resize(std::size_t size) 58 | { 59 | if (size > size_) 60 | { 61 | void* p = std::realloc(data_, size); 62 | if (!p) 63 | { 64 | throw std::bad_alloc(); 65 | } 66 | data_ = (byte_t*)p; 67 | size_ = size; 68 | } 69 | } 70 | 71 | void free() 72 | { 73 | delete this; 74 | } 75 | 76 | private: 77 | byte_t* data_; 78 | std::size_t size_; 79 | }; 80 | 81 | typedef boost::intrusive_ptr buffer_ptr; 82 | } 83 | } 84 | 85 | #endif /// GCE_ACTOR_DETAIL_BUFFER_HPP 86 | -------------------------------------------------------------------------------- /libs/actor/example/pingpong/main.cpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | void pingpong(gce::actor& self) 16 | { 17 | try 18 | { 19 | gce::aid_t partner; 20 | gce::aid_t host = gce::recv(self, gce::atom("prepare"), partner); 21 | 22 | int count_down; 23 | while (true) 24 | { 25 | gce::recv(self, gce::atom("pingpong"), count_down); 26 | if (count_down == 0) 27 | { 28 | break; 29 | } 30 | --count_down; 31 | gce::send(self, partner, gce::atom("pingpong"), count_down); 32 | } 33 | gce::send(self, host, gce::atom("bye")); 34 | } 35 | catch (std::exception& ex) 36 | { 37 | std::cerr << ex.what() << std::endl; 38 | } 39 | } 40 | 41 | int main(int argc, char* argv[]) 42 | { 43 | try 44 | { 45 | int const count_down = 46 | (argc > 1 && std::atoi(argv[1]) > 0) ? std::atoi(argv[1]) : 10000; 47 | 48 | gce::context ctx; 49 | gce::actor base = gce::spawn(ctx); 50 | gce::aid_t ping = gce::spawn(base, boost::bind(&pingpong, _1)); 51 | gce::aid_t pong = gce::spawn(base, boost::bind(&pingpong, _1)); 52 | 53 | gce::send(base, ping, gce::atom("prepare"), pong); 54 | gce::send(base, pong, gce::atom("prepare"), ping); 55 | 56 | gce::send(base, ping, gce::atom("pingpong"), count_down); 57 | gce::recv(base, gce::atom("bye")); 58 | } 59 | catch (std::exception& ex) 60 | { 61 | std::cerr << ex.what() << std::endl; 62 | } 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /libs/actor/test/test_send_recv.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class send_recv_ut 13 | { 14 | public: 15 | static void run() 16 | { 17 | std::cout << "send_recv_ut begin." << std::endl; 18 | test_base(); 19 | std::cout << "send_recv_ut end." << std::endl; 20 | } 21 | 22 | public: 23 | static void ping_pong(actor& self) 24 | { 25 | try 26 | { 27 | aid_t host, partner; 28 | host = recv(self, atom("prepare"), partner); 29 | 30 | int count_down; 31 | while (true) 32 | { 33 | recv(self, atom("ping_pong"), count_down); 34 | if (count_down == 0) 35 | { 36 | break; 37 | } 38 | --count_down; 39 | send(self, partner, atom("ping_pong"), count_down); 40 | } 41 | send(self, host); 42 | } 43 | catch (std::exception& ex) 44 | { 45 | std::cerr << ex.what() << std::endl; 46 | } 47 | } 48 | 49 | static void test_base() 50 | { 51 | try 52 | { 53 | context ctx; 54 | actor base = spawn(ctx); 55 | 56 | int const count_down = 10000; 57 | aid_t ping = spawn(base, boost::bind(&send_recv_ut::ping_pong, _1)); 58 | aid_t pong = spawn(base, boost::bind(&send_recv_ut::ping_pong, _1)); 59 | 60 | send(base, ping, atom("prepare"), pong); 61 | send(base, pong, atom("prepare"), ping); 62 | 63 | send(base, ping, atom("ping_pong"), count_down); 64 | recv(base); 65 | } 66 | catch (std::exception& ex) 67 | { 68 | std::cerr << ex.what() << std::endl; 69 | } 70 | } 71 | }; 72 | } 73 | -------------------------------------------------------------------------------- /gce/actor/impl/tcp/socket.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_IMPL_TCP_SOCKET_HPP 11 | #define GCE_ACTOR_IMPL_TCP_SOCKET_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | namespace gce 19 | { 20 | namespace tcp 21 | { 22 | class socket 23 | : public gce::detail::basic_socket 24 | { 25 | public: 26 | explicit socket(io_service_t&); 27 | socket(io_service_t&, std::string const&, std::string const&); 28 | ~socket(); 29 | 30 | public: 31 | void init(gce::detail::cache_pool* user); 32 | void send(byte_t const*, std::size_t, byte_t const*, std::size_t); 33 | std::size_t recv(byte_t*, std::size_t, yield_t); 34 | void connect(yield_t); 35 | void close(); 36 | void wait_end(yield_t); 37 | void reset(); 38 | 39 | inline boost::asio::ip::tcp::socket& get_socket() { return sock_; } 40 | 41 | private: 42 | void close_socket(); 43 | void begin_send(); 44 | void end_send(errcode_t const&); 45 | 46 | private: 47 | gce::detail::cache_pool* user_; 48 | boost::asio::ip::tcp::resolver reso_; 49 | boost::asio::ip::tcp::socket sock_; 50 | std::string const host_; 51 | std::string const port_; 52 | 53 | bool closed_; 54 | bool reconn_; 55 | bool sending_; 56 | std::size_t sending_buffer_; 57 | std::size_t standby_buffer_; 58 | boost::array send_buffer_; 59 | 60 | timer_t sync_; 61 | bool waiting_end_; 62 | }; 63 | typedef boost::shared_ptr socket_ptr; 64 | } 65 | } 66 | 67 | #endif /// GCE_ACTOR_IMPL_TCP_SOCKET_HPP 68 | -------------------------------------------------------------------------------- /libs/actor/test/test_mixin.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class mixin_ut 13 | { 14 | public: 15 | static void run() 16 | { 17 | std::cout << "mixin_ut begin." << std::endl; 18 | test_common(); 19 | std::cout << "mixin_ut end." << std::endl; 20 | } 21 | 22 | private: 23 | static void my_child(actor& self) 24 | { 25 | aid_t aid = recv(self); 26 | reply(self, aid); 27 | } 28 | 29 | static void my_thr(context& ctx) 30 | { 31 | actor a = spawn(ctx); 32 | std::size_t size = 10; 33 | std::vector res_list(size); 34 | for (std::size_t i=0; i 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | namespace gce 20 | { 21 | namespace detail 22 | { 23 | template 24 | inline void register_service(Actor& a, match_t name) 25 | { 26 | cache_pool* user = a.get_cache_pool(); 27 | context& ctx = user->get_context(); 28 | user->register_service(name, a.get_aid()); 29 | ctx.register_service(name, a.get_aid(), user->get_index()); 30 | } 31 | 32 | template 33 | inline void deregister_service(Actor& a, match_t name) 34 | { 35 | cache_pool* user = a.get_cache_pool(); 36 | context& ctx = user->get_context(); 37 | user->deregister_service(name, a.get_aid()); 38 | ctx.deregister_service(name, a.get_aid(), user->get_index()); 39 | } 40 | } 41 | 42 | inline void register_service(actor& self, match_t name) 43 | { 44 | detail::register_service(self, name); 45 | } 46 | 47 | inline void deregister_service(actor& self, match_t name) 48 | { 49 | detail::deregister_service(self, name); 50 | } 51 | 52 | inline void register_service(actor& self, match_t name) 53 | { 54 | detail::register_service(self, name); 55 | } 56 | 57 | inline void deregister_service(actor& self, match_t name) 58 | { 59 | detail::deregister_service(self, name); 60 | } 61 | 62 | inline gce::svcid_t make_svcid(ctxid_t ctxid, match_t name) 63 | { 64 | return svcid_t(ctxid, name); 65 | } 66 | } 67 | 68 | #endif /// GCE_ACTOR_SERVICE_HPP 69 | -------------------------------------------------------------------------------- /libs/actor/example/cluster/endpoint.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_EXAMPLE_CLUSTER_ENDPOINT_HPP 11 | #define GCE_ACTOR_EXAMPLE_CLUSTER_ENDPOINT_HPP 12 | 13 | #include 14 | 15 | class endpoint 16 | { 17 | public: 18 | endpoint() 19 | : group_index_(gce::u32_nil) 20 | , sid_(gce::u32_nil) 21 | { 22 | } 23 | 24 | endpoint(boost::uint32_t group_index, boost::uint32_t sid) 25 | : group_index_(group_index) 26 | , sid_(sid) 27 | { 28 | } 29 | 30 | ~endpoint() 31 | { 32 | } 33 | 34 | public: 35 | inline bool valid() const 36 | { 37 | return sid_ != gce::u32_nil; 38 | } 39 | 40 | inline bool operator==(endpoint const& rhs) const 41 | { 42 | return sid_ == rhs.sid_; 43 | } 44 | 45 | inline bool operator!=(endpoint const& rhs) const 46 | { 47 | return sid_ != rhs.sid_; 48 | } 49 | 50 | inline bool operator<(endpoint const& rhs) const 51 | { 52 | return sid_ < rhs.sid_; 53 | } 54 | 55 | inline boost::uint32_t get_group_index() const 56 | { 57 | return group_index_; 58 | } 59 | 60 | inline boost::uint32_t get_session_id() const 61 | { 62 | return sid_; 63 | } 64 | 65 | private: 66 | boost::uint32_t group_index_; 67 | boost::uint32_t sid_; 68 | }; 69 | 70 | inline gce::message& operator<<(gce::message& m, endpoint const& src) 71 | { 72 | m << src.get_group_index() << src.get_session_id(); 73 | return m; 74 | } 75 | 76 | inline gce::message& operator>>(gce::message& msg, endpoint& des) 77 | { 78 | boost::uint32_t group_index; 79 | boost::uint32_t sid; 80 | msg >> group_index >> sid; 81 | des = endpoint(group_index, sid); 82 | return msg; 83 | } 84 | 85 | #endif /// GCE_ACTOR_EXAMPLE_CLUSTER_ENDPOINT_HPP 86 | -------------------------------------------------------------------------------- /libs/actor/test/test_actor_pingpong.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class actor_pingpong_ut 13 | { 14 | static std::size_t const msg_size = 100000; 15 | public: 16 | static void run() 17 | { 18 | std::cout << "actor_pingpong_ut begin." << std::endl; 19 | test(); 20 | std::cout << "actor_pingpong_ut end." << std::endl; 21 | } 22 | 23 | private: 24 | static void my_child(actor& self, aid_t sire, aid_t base_id) 25 | { 26 | message msg; 27 | while (true) 28 | { 29 | self.recv(msg); 30 | if (msg.get_type() == 2) 31 | { 32 | break; 33 | } 34 | else 35 | { 36 | self.send(sire, msg); 37 | } 38 | } 39 | self.send(base_id, msg); 40 | } 41 | 42 | static void my_actor(actor& self, aid_t base_id) 43 | { 44 | aid_t aid = 45 | spawn( 46 | self, 47 | boost::bind( 48 | &actor_pingpong_ut::my_child, 49 | _1, self.get_aid(), base_id 50 | ) 51 | ); 52 | 53 | message m(1); 54 | for (std::size_t i=0; i base = spawn(ctx); 68 | 69 | aid_t base_id = base.get_aid(); 70 | aid_t aid = 71 | spawn( 72 | base, 73 | boost::bind( 74 | &actor_pingpong_ut::my_actor, _1, 75 | base_id 76 | ) 77 | ); 78 | 79 | boost::timer::auto_cpu_timer t; 80 | recv(base); 81 | } 82 | catch (std::exception& ex) 83 | { 84 | std::cerr << ex.what() << std::endl; 85 | } 86 | } 87 | }; 88 | } 89 | 90 | -------------------------------------------------------------------------------- /libs/actor/test/test_coro.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class coro_ut 13 | { 14 | public: 15 | static void run() 16 | { 17 | std::cout << "coro_ut begin." << std::endl; 18 | test_common(); 19 | std::cout << "coro_ut end." << std::endl; 20 | } 21 | 22 | private: 23 | static void my_actor(actor& self, aid_t base) 24 | { 25 | std::size_t loop_num = 10; 26 | yield_t yield = self.get_yield(); 27 | timer_t tmr(self.get_cache_pool()->get_context().get_io_service()); 28 | 29 | for (std::size_t i=0; i base = spawn(ctx); 46 | 47 | for (std::size_t a=0; a<5; ++a) 48 | { 49 | for (std::size_t i=0; i 14 | 15 | namespace gce 16 | { 17 | struct match 18 | { 19 | match() 20 | : timeout_(infin) 21 | { 22 | } 23 | 24 | explicit match(duration_t tmo) 25 | : timeout_(tmo) 26 | { 27 | } 28 | 29 | explicit match(match_list_t match_list, duration_t tmo = infin) 30 | : timeout_(tmo) 31 | , match_list_(match_list) 32 | { 33 | } 34 | 35 | explicit match(match_t type1, duration_t tmo = infin) 36 | : timeout_(tmo) 37 | { 38 | match_list_.push_back(type1); 39 | } 40 | 41 | match(match_t type1, match_t type2, duration_t tmo = infin) 42 | : timeout_(tmo) 43 | { 44 | match_list_.push_back(type1); 45 | match_list_.push_back(type2); 46 | } 47 | 48 | match(match_t type1, match_t type2, match_t type3, duration_t tmo = infin) 49 | : timeout_(tmo) 50 | { 51 | match_list_.push_back(type1); 52 | match_list_.push_back(type2); 53 | match_list_.push_back(type3); 54 | } 55 | 56 | match(match_t type1, match_t type2, match_t type3, match_t type4, duration_t tmo = infin) 57 | : timeout_(tmo) 58 | { 59 | match_list_.push_back(type1); 60 | match_list_.push_back(type2); 61 | match_list_.push_back(type3); 62 | match_list_.push_back(type4); 63 | } 64 | 65 | match(match_t type1, match_t type2, match_t type3, match_t type4, match_t type5, duration_t tmo = infin) 66 | : timeout_(tmo) 67 | { 68 | match_list_.push_back(type1); 69 | match_list_.push_back(type2); 70 | match_list_.push_back(type3); 71 | match_list_.push_back(type4); 72 | match_list_.push_back(type5); 73 | } 74 | 75 | void clear() 76 | { 77 | timeout_ = infin; 78 | match_list_.clear(); 79 | } 80 | 81 | duration_t timeout_; 82 | match_list_t match_list_; 83 | }; 84 | } 85 | 86 | #endif /// GCE_ACTOR_MATCH_HPP 87 | 88 | -------------------------------------------------------------------------------- /libs/actor/test/test_remote_link.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class remote_link_ut 13 | { 14 | public: 15 | static void run() 16 | { 17 | std::cout << "remote_link_ut begin." << std::endl; 18 | test_base(); 19 | std::cout << "remote_link_ut end." << std::endl; 20 | } 21 | 22 | public: 23 | static void test_base() 24 | { 25 | try 26 | { 27 | std::size_t quiter_num = 100; 28 | 29 | attributes attrs; 30 | attrs.id_ = atom("one"); 31 | context ctx1(attrs); 32 | attrs.id_ = atom("two"); 33 | context ctx2(attrs); 34 | 35 | actor base1 = spawn(ctx1); 36 | actor base2 = spawn(ctx2); 37 | 38 | gce::bind(base2, "tcp://127.0.0.1:14923"); 39 | 40 | net_option opt; 41 | opt.reconn_period_ = seconds_t(1); 42 | connect(base1, atom("two"), "tcp://127.0.0.1:14923", false, opt); 43 | 44 | std::vector quiter_list(quiter_num); 45 | for (std::size_t i=0; i& self) 76 | { 77 | try 78 | { 79 | recv(self); 80 | } 81 | catch (std::exception& ex) 82 | { 83 | std::cerr << "quiter except: " << ex.what() << std::endl; 84 | } 85 | } 86 | }; 87 | } 88 | -------------------------------------------------------------------------------- /libs/actor/test/test_match.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class match_ut 13 | { 14 | public: 15 | static void run() 16 | { 17 | std::cout << "match_ut begin." << std::endl; 18 | test_common(); 19 | std::cout << "match_ut end." << std::endl; 20 | } 21 | 22 | static void my_actor_child(actor& self) 23 | { 24 | aid_t aid = recv(self, 3); 25 | reply(self, aid); 26 | } 27 | 28 | static void my_actor(actor& self, aid_t base_id) 29 | { 30 | std::size_t size = 1; 31 | std::vector res_list(size); 32 | for (std::size_t i=0; i a = spawn(ctx); 48 | for (std::size_t i=0; i<2; ++i) 49 | { 50 | spawn(a, boost::bind(&match_ut::my_actor, _1, base_id)); 51 | } 52 | } 53 | 54 | static void test_common() 55 | { 56 | try 57 | { 58 | std::size_t user_thr_num = 5; 59 | std::size_t my_actor_size = 10; 60 | attributes attrs; 61 | context ctx(attrs); 62 | actor base = spawn(ctx); 63 | 64 | aid_t base_id = base.get_aid(); 65 | 66 | boost::thread_group thrs; 67 | for (std::size_t i=0; i base1 = spawn(ctx1); 36 | actor base2 = spawn(ctx2); 37 | 38 | gce::bind(base2, "tcp://127.0.0.1:14923"); 39 | 40 | aid_t echo_aid = 41 | spawn( 42 | base2, 43 | boost::bind( 44 | &socket_ut::echo, _1 45 | ), 46 | monitored 47 | ); 48 | 49 | net_option opt; 50 | opt.reconn_period_ = seconds_t(1); 51 | connect(base1, atom("two"), "tcp://127.0.0.1:14923", false, opt); 52 | 53 | for (std::size_t i=0; i& self) 69 | { 70 | try 71 | { 72 | while (true) 73 | { 74 | message msg; 75 | aid_t sender = self.recv(msg); 76 | match_t type = msg.get_type(); 77 | if (type == atom("echo")) 78 | { 79 | self.send(sender, msg); 80 | } 81 | else 82 | { 83 | break; 84 | } 85 | } 86 | } 87 | catch (std::exception& ex) 88 | { 89 | std::cerr << "echo except: " << ex.what() << std::endl; 90 | } 91 | } 92 | }; 93 | } 94 | -------------------------------------------------------------------------------- /libs/actor/test/test_relay.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class relay_ut 13 | { 14 | public: 15 | static void run() 16 | { 17 | std::cout << "relay_ut begin." << std::endl; 18 | test_common(); 19 | std::cout << "relay_ut end." << std::endl; 20 | } 21 | 22 | private: 23 | static void my_actor(actor& self, aid_t last_id) 24 | { 25 | message msg; 26 | aid_t sender = self.recv(msg); 27 | if (last_id) 28 | { 29 | self.relay(last_id, msg); 30 | } 31 | else 32 | { 33 | message m(atom("hello")); 34 | self.reply(sender, m); 35 | } 36 | } 37 | 38 | static void root(actor& self) 39 | { 40 | std::size_t free_actor_num = 20; 41 | 42 | aid_t last_id; 43 | aid_t first_id; 44 | for (std::size_t i=0; i base = spawn(ctx); 75 | 76 | for (std::size_t i=0; i 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | namespace gce 17 | { 18 | namespace tcp 19 | { 20 | ///---------------------------------------------------------------------------- 21 | acceptor::acceptor( 22 | strand_t& snd, 23 | std::string const& host, 24 | boost::uint16_t port 25 | ) 26 | : snd_(snd) 27 | , acpr_(snd_.get_io_service()) 28 | , host_(host) 29 | , port_(port) 30 | { 31 | } 32 | ///---------------------------------------------------------------------------- 33 | acceptor::~acceptor() 34 | { 35 | } 36 | ///---------------------------------------------------------------------------- 37 | void acceptor::bind() 38 | { 39 | boost::asio::ip::address addr; 40 | addr.from_string(host_); 41 | boost::asio::ip::tcp::endpoint ep(addr, port_); 42 | acpr_.open(ep.protocol()); 43 | 44 | acpr_.set_option(boost::asio::socket_base::reuse_address(true)); 45 | acpr_.bind(ep); 46 | 47 | acpr_.set_option(boost::asio::socket_base::receive_buffer_size(640000)); 48 | acpr_.set_option(boost::asio::socket_base::send_buffer_size(640000)); 49 | 50 | acpr_.listen(1024); 51 | 52 | acpr_.set_option(boost::asio::ip::tcp::no_delay(true)); 53 | acpr_.set_option(boost::asio::socket_base::keep_alive(true)); 54 | acpr_.set_option(boost::asio::socket_base::enable_connection_aborted(true)); 55 | } 56 | ///---------------------------------------------------------------------------- 57 | gce::detail::socket_ptr acceptor::accept(yield_t yield) 58 | { 59 | gce::tcp::socket_ptr skt(new socket(snd_.get_io_service())); 60 | acpr_.async_accept(skt->get_socket(), yield); 61 | return skt; 62 | } 63 | ///---------------------------------------------------------------------------- 64 | void acceptor::close() 65 | { 66 | errcode_t ignore_ec; 67 | acpr_.close(ignore_ec); 68 | } 69 | ///---------------------------------------------------------------------------- 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /gce/actor/service_id.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_SERVICE_ID_HPP 11 | #define GCE_ACTOR_SERVICE_ID_HPP 12 | 13 | #include 14 | 15 | namespace gce 16 | { 17 | class service_id 18 | { 19 | public: 20 | service_id() 21 | : ctxid_(ctxid_nil) 22 | , name_(match_nil) 23 | { 24 | } 25 | 26 | explicit service_id(match_t name) 27 | : ctxid_(ctxid_nil) 28 | , name_(name) 29 | { 30 | } 31 | 32 | service_id(ctxid_t ctxid, match_t name) 33 | : ctxid_(ctxid) 34 | , name_(name) 35 | { 36 | } 37 | 38 | ~service_id() 39 | { 40 | } 41 | 42 | public: 43 | inline operator bool() const 44 | { 45 | return name_ != match_nil; 46 | } 47 | 48 | inline bool operator!() const 49 | { 50 | return name_ == match_nil; 51 | } 52 | 53 | inline bool operator==(service_id const& rhs) const 54 | { 55 | return 56 | ctxid_ == rhs.ctxid_ && 57 | name_ == rhs.name_; 58 | } 59 | 60 | inline bool operator!=(service_id const& rhs) const 61 | { 62 | return !(*this == rhs); 63 | } 64 | 65 | inline bool operator<(service_id const& rhs) const 66 | { 67 | if (ctxid_ < rhs.ctxid_) 68 | { 69 | return true; 70 | } 71 | else if (ctxid_ > rhs.ctxid_) 72 | { 73 | return false; 74 | } 75 | 76 | if (name_ < rhs.name_) 77 | { 78 | return true; 79 | } 80 | else if (name_ > rhs.name_) 81 | { 82 | return false; 83 | } 84 | 85 | return false; 86 | } 87 | 88 | ctxid_t ctxid_; 89 | match_t name_; 90 | }; 91 | 92 | typedef service_id svcid_t; 93 | } 94 | 95 | template 96 | std::basic_ostream& operator<<( 97 | std::basic_ostream& strm, gce::svcid_t const& svc 98 | ) 99 | { 100 | strm << "<" << svc.ctxid_ << "." << svc.name_ << ">"; 101 | return strm; 102 | } 103 | 104 | GCE_PACK(gce::svcid_t, (ctxid_)(name_)); 105 | 106 | #endif /// GCE_ACTOR_SERVICE_ID_HPP 107 | 108 | -------------------------------------------------------------------------------- /gce/actor/adaptor.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_ADAPTOR_HPP 11 | #define GCE_ACTOR_ADAPTOR_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | 17 | namespace gce 18 | { 19 | class adaptor 20 | { 21 | public: 22 | adaptor(actor& a, errcode_t& ec, std::size_t& bytes_transferred) 23 | : a_(a) 24 | , ec_(ec) 25 | , bytes_transferred_(&bytes_transferred) 26 | , itr_(0) 27 | , signal_number_(0) 28 | { 29 | } 30 | 31 | adaptor(actor& a, errcode_t& ec, boost::asio::ip::tcp::resolver::iterator& itr) 32 | : a_(a) 33 | , ec_(ec) 34 | , bytes_transferred_(0) 35 | , itr_(&itr) 36 | , signal_number_(0) 37 | { 38 | } 39 | 40 | adaptor(actor& a, errcode_t& ec) 41 | : a_(a) 42 | , ec_(ec) 43 | , bytes_transferred_(0) 44 | , itr_(0) 45 | , signal_number_(0) 46 | { 47 | } 48 | 49 | adaptor(actor& a, errcode_t& ec, int& signal_number) 50 | : a_(a) 51 | , ec_(ec) 52 | , bytes_transferred_(0) 53 | , itr_(0) 54 | , signal_number_(&signal_number) 55 | { 56 | } 57 | 58 | public: 59 | void operator()(errcode_t const& ec, std::size_t bytes_transferred) 60 | { 61 | BOOST_ASSERT(bytes_transferred_); 62 | ec_ = ec; 63 | *bytes_transferred_ = bytes_transferred; 64 | a_.resume(); 65 | } 66 | 67 | void operator()(errcode_t const& ec, boost::asio::ip::tcp::resolver::iterator itr) 68 | { 69 | BOOST_ASSERT(itr_); 70 | ec_ = ec; 71 | *itr_ = itr; 72 | a_.resume(); 73 | } 74 | 75 | void operator()(errcode_t const& ec) 76 | { 77 | ec_ = ec; 78 | a_.resume(); 79 | } 80 | 81 | void operator()(errcode_t const& ec, int signal_number) 82 | { 83 | BOOST_ASSERT(signal_number_); 84 | ec_ = ec; 85 | *signal_number_ = signal_number; 86 | a_.resume(); 87 | } 88 | 89 | private: 90 | actor a_; 91 | errcode_t& ec_; 92 | std::size_t* bytes_transferred_; 93 | boost::asio::ip::tcp::resolver::iterator* itr_; 94 | int* signal_number_; 95 | }; 96 | } 97 | 98 | #endif /// GCE_ACTOR_ADAPTOR_HPP 99 | -------------------------------------------------------------------------------- /gce/actor/detail/spawn.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_SPAWN_HPP 11 | #define GCE_ACTOR_DETAIL_SPAWN_HPP 12 | 13 | #include 14 | #include 15 | 16 | namespace gce 17 | { 18 | namespace detail 19 | { 20 | class spawn_t 21 | { 22 | public: 23 | spawn_t() 24 | : type_(spw_nil) 25 | , func_(match_nil) 26 | , ctxid_(ctxid_nil) 27 | , stack_size_(0) 28 | { 29 | } 30 | 31 | spawn_t( 32 | spawn_type type, match_t func, match_t ctxid, 33 | std::size_t stack_size, sid_t sid, aid_t aid 34 | ) 35 | : type_(type) 36 | , func_(func) 37 | , ctxid_(ctxid) 38 | , stack_size_(stack_size) 39 | , sid_(sid) 40 | , aid_(aid) 41 | { 42 | } 43 | 44 | ~spawn_t() 45 | { 46 | } 47 | 48 | public: 49 | inline spawn_type get_type() const { return type_; } 50 | inline match_t get_func() const { return func_; } 51 | inline match_t get_ctxid() const { return ctxid_; } 52 | inline std::size_t get_stack_size() const { return stack_size_; } 53 | inline sid_t get_id() const { return sid_; } 54 | inline aid_t get_aid() const { return aid_; } 55 | 56 | private: 57 | spawn_type type_; 58 | match_t func_; 59 | match_t ctxid_; 60 | std::size_t stack_size_; 61 | sid_t sid_; 62 | aid_t aid_; 63 | }; 64 | 65 | enum spawn_error 66 | { 67 | spawn_ok = 0, 68 | spawn_no_socket, 69 | spawn_func_not_found, 70 | }; 71 | 72 | class spawn_ret_t 73 | { 74 | public: 75 | spawn_ret_t() 76 | : err_(spawn_ok) 77 | , sid_(sid_nil) 78 | { 79 | } 80 | 81 | spawn_ret_t(spawn_error err, sid_t sid, aid_t aid) 82 | : err_(err) 83 | , sid_(sid) 84 | , aid_(aid) 85 | { 86 | } 87 | 88 | ~spawn_ret_t() 89 | { 90 | } 91 | 92 | public: 93 | inline spawn_error get_error() const { return err_; } 94 | inline sid_t get_id() const { return sid_; } 95 | inline aid_t get_aid() const { return aid_; } 96 | 97 | private: 98 | spawn_error err_; 99 | sid_t sid_; 100 | aid_t aid_; 101 | }; 102 | } 103 | } 104 | 105 | #endif /// GCE_ACTOR_DETAIL_SPAWN_HPP 106 | 107 | -------------------------------------------------------------------------------- /libs/actor/test/test_router_link.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class router_link_ut 13 | { 14 | public: 15 | static void run() 16 | { 17 | std::cout << "router_link_ut begin." << std::endl; 18 | test_base(); 19 | std::cout << "router_link_ut end." << std::endl; 20 | } 21 | 22 | public: 23 | static void test_base() 24 | { 25 | try 26 | { 27 | std::size_t quiter_num = 100; 28 | 29 | attributes attrs; 30 | attrs.id_ = atom("router"); 31 | context ctx(attrs); 32 | attrs.id_ = atom("one"); 33 | context ctx1(attrs); 34 | attrs.id_ = atom("two"); 35 | context ctx2(attrs); 36 | 37 | actor base = spawn(ctx); 38 | actor base1 = spawn(ctx1); 39 | actor base2 = spawn(ctx2); 40 | 41 | gce::bind(base, "tcp://127.0.0.1:14923", true); 42 | 43 | net_option opt; 44 | opt.reconn_period_ = seconds_t(1); 45 | connect(base1, atom("router"), "tcp://127.0.0.1:14923", true, opt); 46 | connect(base2, atom("router"), "tcp://127.0.0.1:14923", true, opt); 47 | wait(base2, boost::chrono::milliseconds(100)); 48 | 49 | std::vector quiter_list(quiter_num); 50 | for (std::size_t i=0; i& self) 81 | { 82 | try 83 | { 84 | recv(self); 85 | } 86 | catch (std::exception& ex) 87 | { 88 | std::cerr << "quiter except: " << ex.what() << std::endl; 89 | } 90 | } 91 | }; 92 | } 93 | 94 | -------------------------------------------------------------------------------- /gce/actor/detail/acceptor.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_ACCEPTOR_HPP 11 | #define GCE_ACTOR_DETAIL_ACCEPTOR_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | namespace gce 26 | { 27 | class thread_mapped_actor; 28 | class context; 29 | namespace detail 30 | { 31 | class cache_pool; 32 | class basic_acceptor; 33 | 34 | class acceptor 35 | : public object_pool::object 36 | , public basic_actor 37 | { 38 | typedef basic_actor base_type; 39 | 40 | enum status 41 | { 42 | ready = 0, 43 | on, 44 | off, 45 | }; 46 | 47 | public: 48 | explicit acceptor(cache_pool*); 49 | ~acceptor(); 50 | 51 | public: 52 | void init(net_option); 53 | void bind( 54 | aid_t sire, remote_func_list_t const&, 55 | std::string const&, bool is_router 56 | ); 57 | 58 | public: 59 | void stop(); 60 | void on_free(); 61 | void on_recv(pack&, base_type::send_hint); 62 | inline void send(aid_t recver, message const& m) 63 | { 64 | base_type::pri_send(recver, m); 65 | } 66 | 67 | void link(aid_t) {} 68 | void monitor(aid_t) {} 69 | 70 | private: 71 | void run(aid_t sire, std::string const&, yield_t); 72 | void spawn_socket(cache_pool*, socket_ptr); 73 | basic_acceptor* make_acceptor(std::string const&); 74 | void handle_recv(pack&); 75 | 76 | private: 77 | void close(); 78 | void free_self(exit_code_t, std::string const&, yield_t); 79 | 80 | private: 81 | /// Ensure start from a new cache line. 82 | byte_t pad0_[GCE_CACHE_LINE_SIZE]; 83 | 84 | GCE_CACHE_ALIGNED_VAR(status, stat_) 85 | GCE_CACHE_ALIGNED_VAR(net_option, opt_) 86 | 87 | /// thread local vals 88 | boost::scoped_ptr acpr_; 89 | std::map remote_func_list_; 90 | bool is_router_; 91 | }; 92 | } 93 | } 94 | 95 | #endif /// GCE_ACTOR_DETAIL_ACCEPTOR_HPP 96 | 97 | -------------------------------------------------------------------------------- /gce/actor/detail/mailbox.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_MAILBOX_HPP 11 | #define GCE_ACTOR_DETAIL_MAILBOX_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | namespace gce 25 | { 26 | namespace detail 27 | { 28 | class mailbox 29 | { 30 | typedef std::pair recv_pair_t; 31 | typedef std::pair res_msg_pair_t; 32 | typedef std::map res_msg_list_t; 33 | 34 | public: 35 | explicit mailbox(std::size_t); 36 | ~mailbox(); 37 | 38 | void clear(); 39 | 40 | public: 41 | bool pop(recv_t&, message&, match_list_t const&); 42 | bool pop(response_t&, message&); 43 | bool pop(aid_t, request_t&); 44 | 45 | void push(aid_t, message const&); 46 | void push(exit_t, message const&); 47 | void push(request_t, message const&); 48 | bool push(response_t, message const&); 49 | 50 | private: 51 | void add_match_msg(recv_t const&, aid_t sender, message const&); 52 | bool fetch_match_msg(match_t, recv_t&, message&); 53 | 54 | private: 55 | typedef std::list recv_queue_t; 56 | typedef recv_queue_t::iterator recv_itr; 57 | recv_queue_t recv_que_; 58 | 59 | typedef std::list match_queue_t; 60 | typedef match_queue_t::iterator match_itr; 61 | std::vector cache_match_list_; 62 | 63 | typedef std::map match_queue_list_t; 64 | match_queue_list_t match_queue_list_; 65 | 66 | res_msg_list_t res_msg_list_; 67 | 68 | typedef std::deque req_queue_t; 69 | typedef std::map wait_reply_list_t; 70 | wait_reply_list_t wait_reply_list_; 71 | 72 | typedef std::map exit_list_t; 73 | typedef std::map > svc_exit_list_t; 74 | exit_list_t exit_list_; 75 | svc_exit_list_t svc_exit_list_; 76 | 77 | req_queue_t dummy_; 78 | match_queue_t dummy2_; 79 | }; 80 | } 81 | } 82 | 83 | #endif /// GCE_ACTOR_DETAIL_MAILBOX_HPP 84 | -------------------------------------------------------------------------------- /libs/actor/test/test_router.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class router_ut 13 | { 14 | public: 15 | static void run() 16 | { 17 | std::cout << "router_ut begin." << std::endl; 18 | test_base(); 19 | std::cout << "router_ut end." << std::endl; 20 | } 21 | 22 | public: 23 | static void test_base() 24 | { 25 | try 26 | { 27 | std::size_t echo_num = 100; 28 | 29 | attributes attrs; 30 | attrs.id_ = atom("router"); 31 | context ctx(attrs); 32 | attrs.id_ = atom("one"); 33 | context ctx1(attrs); 34 | attrs.id_ = atom("two"); 35 | context ctx2(attrs); 36 | 37 | actor base = spawn(ctx); 38 | actor base1 = spawn(ctx1); 39 | actor base2 = spawn(ctx2); 40 | 41 | gce::bind(base, "tcp://127.0.0.1:14923", true); 42 | 43 | aid_t echo_aid = 44 | spawn( 45 | base2, 46 | boost::bind( 47 | &socket_ut::echo, _1 48 | ), 49 | monitored 50 | ); 51 | 52 | net_option opt; 53 | opt.reconn_period_ = seconds_t(1); 54 | connect(base1, atom("router"), "tcp://127.0.0.1:14923", true, opt); 55 | connect(base2, atom("router"), "tcp://127.0.0.1:14923", true, opt); 56 | wait(base2, boost::chrono::milliseconds(100)); 57 | 58 | for (std::size_t i=0; i& self) 74 | { 75 | try 76 | { 77 | while (true) 78 | { 79 | message msg; 80 | aid_t sender = self.recv(msg); 81 | match_t type = msg.get_type(); 82 | if (type == atom("echo")) 83 | { 84 | self.send(sender, msg); 85 | } 86 | else 87 | { 88 | break; 89 | } 90 | } 91 | } 92 | catch (std::exception& ex) 93 | { 94 | std::cerr << "echo except: " << ex.what() << std::endl; 95 | } 96 | } 97 | }; 98 | } 99 | -------------------------------------------------------------------------------- /libs/actor/test/main.cpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include 26 | #include "test_object_pool.hpp" 27 | #include "test_coro.hpp" 28 | #include "test_actor.hpp" 29 | #include "test_response.hpp" 30 | #include "test_stackless.hpp" 31 | #include "test_mixin.hpp" 32 | #include "test_slice.hpp" 33 | #include "test_actor_pingpong.hpp" 34 | #include "test_stackless_pingpong.hpp" 35 | #include "test_mixin_pingpong.hpp" 36 | #include "test_slice_pingpong.hpp" 37 | #include "test_match.hpp" 38 | #include "test_link.hpp" 39 | #include "test_message.hpp" 40 | #include "test_relay.hpp" 41 | #include "test_socket.hpp" 42 | #include "test_socket_broken.hpp" 43 | #include "test_remote_link.hpp" 44 | #include "test_router.hpp" 45 | #include "test_router_link.hpp" 46 | #include "test_router_broken.hpp" 47 | #include "test_remote.hpp" 48 | #include "test_remote_relay.hpp" 49 | #include "test_send_recv.hpp" 50 | #include "test_service.hpp" 51 | 52 | int main() 53 | { 54 | try 55 | { 56 | gce::object_pool_ut::run(); 57 | gce::coro_ut::run(); 58 | gce::send_recv_ut::run(); 59 | gce::actor_ut::run(); 60 | gce::response_ut::run(); 61 | gce::stackless_ut::run(); 62 | gce::mixin_ut::run(); 63 | gce::slice_ut::run(); 64 | gce::actor_pingpong_ut::run(); 65 | gce::stackless_pingpong_ut::run(); 66 | gce::mixin_pingpong_ut::run(); 67 | gce::slice_pingpong_ut::run(); 68 | gce::match_ut::run(); 69 | gce::link_ut::run(); 70 | gce::relay_ut::run(); 71 | gce::message_ut::run(); 72 | gce::socket_ut::run(); 73 | gce::socket_broken_ut::run(); 74 | gce::remote_ut::run(); 75 | gce::remote_link_ut::run(); 76 | gce::router_ut::run(); 77 | gce::router_link_ut::run(); 78 | gce::router_broken_ut::run(); 79 | gce::remote_relay_ut::run(); 80 | gce::service_ut::run(); 81 | } 82 | catch (std::exception& ex) 83 | { 84 | std::cerr << ex.what() << std::endl; 85 | } 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /gce/actor/detail/buffer_ref.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_BUFFER_REF_HPP 11 | #define GCE_ACTOR_DETAIL_BUFFER_REF_HPP 12 | 13 | #include 14 | 15 | namespace gce 16 | { 17 | namespace detail 18 | { 19 | class buffer_ref 20 | { 21 | buffer_ref(buffer_ref const&); 22 | buffer_ref& operator=(buffer_ref const& rhs); 23 | 24 | public: 25 | buffer_ref() 26 | : buf_(0) 27 | , size_(0) 28 | , write_size_(0) 29 | , read_size_(0) 30 | { 31 | } 32 | 33 | buffer_ref(byte_t* buf, std::size_t size) 34 | : buf_(buf) 35 | , size_(size) 36 | , write_size_(0) 37 | , read_size_(0) 38 | { 39 | } 40 | 41 | ~buffer_ref() 42 | { 43 | } 44 | 45 | public: 46 | inline std::size_t remain_write_size() const { return size_ - write_size_; } 47 | inline std::size_t remain_read_size() const { return write_size_ - read_size_; } 48 | inline std::size_t write_size() const { return write_size_; } 49 | inline std::size_t read_size() const { return read_size_; } 50 | inline byte_t* get_write_data() { return buf_ + write_size_; } 51 | inline byte_t* get_read_data() { return buf_ + read_size_; } 52 | inline byte_t const* data() const { return buf_; } 53 | inline std::size_t size() const { return size_; } 54 | 55 | inline void clear() 56 | { 57 | write_size_ = 0; 58 | read_size_ = 0; 59 | } 60 | 61 | inline void reset(byte_t* buf, std::size_t size) 62 | { 63 | buf_ = buf; 64 | size_ = size; 65 | 66 | if (read_size_ > size_) 67 | { 68 | throw std::runtime_error("read buffer overflow"); 69 | } 70 | 71 | if (write_size_ > size_) 72 | { 73 | throw std::runtime_error("write buffer overflow"); 74 | } 75 | } 76 | 77 | inline void read(std::size_t size) 78 | { 79 | if (read_size_ + size > write_size_) 80 | { 81 | throw std::runtime_error("read buffer overflow"); 82 | } 83 | 84 | read_size_ += size; 85 | } 86 | 87 | inline void write(std::size_t size) 88 | { 89 | if (write_size_ + size > size_) 90 | { 91 | throw std::runtime_error("write buffer overflow"); 92 | } 93 | 94 | write_size_ += size; 95 | } 96 | 97 | private: 98 | byte_t* buf_; 99 | std::size_t size_; 100 | std::size_t write_size_; 101 | std::size_t read_size_; 102 | }; 103 | } 104 | } 105 | 106 | #endif /// GCE_ACTOR_DETAIL_BUFFER_REF_HPP 107 | -------------------------------------------------------------------------------- /libs/actor/example/cluster/conn.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_EXAMPLE_CLUSTER_CONN_HPP 11 | #define GCE_ACTOR_EXAMPLE_CLUSTER_CONN_HPP 12 | 13 | #include "app.hpp" 14 | #include "socket.hpp" 15 | #include 16 | 17 | typedef boost::shared_ptr socket_ptr; 18 | class conn 19 | : public boost::enable_shared_from_this 20 | { 21 | enum status 22 | { 23 | conned = 0, 24 | online 25 | }; 26 | typedef boost::shared_ptr ptr_t; 27 | 28 | public: 29 | conn(socket_ptr skt, gce::aid_t group_aid, app_ctxid_list_t game_list); 30 | ~conn(); 31 | 32 | public: 33 | static gce::aid_t spawn( 34 | gce::actor& sire, socket_ptr skt, 35 | gce::aid_t group_aid, app_ctxid_list_t game_list 36 | ); 37 | 38 | public: 39 | void run(gce::actor&); 40 | 41 | private: 42 | class timeout 43 | { 44 | public: 45 | void run(gce::actor&); 46 | 47 | private: 48 | boost::chrono::seconds curr_tmo_; 49 | std::size_t max_count_; 50 | std::size_t curr_count_; 51 | 52 | gce::message msg_; 53 | bool running_; 54 | gce::aid_t sender_; 55 | gce::errcode_t ec_; 56 | }; 57 | 58 | class recv 59 | { 60 | public: 61 | recv(socket_ptr skt, app_ctxid_list_t game_list); 62 | 63 | public: 64 | void run( 65 | gce::actor&, 66 | gce::aid_t tmo_aid, gce::aid_t conn_aid 67 | ); 68 | 69 | private: 70 | socket_ptr skt_; 71 | app_ctxid_list_t game_list_; 72 | 73 | gce::aid_t tmo_aid_; 74 | gce::aid_t conn_aid_; 75 | gce::svcid_t game_svcid_; 76 | gce::aid_t usr_aid_; 77 | status stat_; 78 | 79 | gce::message msg_; 80 | gce::errcode_t ec_; 81 | std::size_t bytes_transferred_; 82 | std::string errmsg_; 83 | std::string username_; 84 | gce::match_t type_; 85 | }; 86 | 87 | private: 88 | socket_ptr skt_; 89 | gce::aid_t group_aid_; 90 | app_ctxid_list_t game_list_; 91 | 92 | timeout tmo_; 93 | recv rcv_; 94 | 95 | /// local stack 96 | gce::message msg_; 97 | gce::aid_t tmo_aid_; 98 | gce::aid_t rcv_aid_; 99 | bool running_; 100 | gce::aid_t sender_; 101 | gce::errcode_t ec_; 102 | std::size_t bytes_transferred_; 103 | gce::match_t type_; 104 | gce::aid_t tmp_aid_; 105 | int exit_num_; 106 | }; 107 | 108 | #endif /// GCE_ACTOR_EXAMPLE_CLUSTER_CONN_HPP 109 | -------------------------------------------------------------------------------- /gce/actor/detail/object_pool.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_OBJECT_POOL_HPP 11 | #define GCE_ACTOR_DETAIL_OBJECT_POOL_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | namespace gce 21 | { 22 | namespace detail 23 | { 24 | class cache_pool; 25 | template < 26 | typename T, 27 | typename Args = void, 28 | typename Alloc = std::allocator 29 | > 30 | class object_pool 31 | { 32 | typedef typename detail::freelist pool_t; 33 | typedef typename pool_t::value_type value_type; 34 | typedef typename pool_t::args_t args_t; 35 | typedef typename pool_t::allocator_t allocator_t; 36 | typedef typename pool_t::pointer pointer; 37 | 38 | public: 39 | struct object 40 | { 41 | friend class object_access; 42 | object() : next_obj_(0) {} 43 | virtual ~object() {} 44 | virtual void on_free() {} 45 | 46 | private: 47 | pointer next_obj_; 48 | }; 49 | 50 | public: 51 | explicit object_pool( 52 | cache_pool* owner, 53 | args_t args, 54 | std::size_t cache_size = 32, /// size_nil means always cache object 55 | std::size_t reserve_size = 32, 56 | allocator_t a = allocator_t() 57 | ) 58 | : owner_(owner) 59 | , pool_(args, cache_size, reserve_size, a) 60 | { 61 | } 62 | 63 | explicit object_pool( 64 | cache_pool* owner, 65 | std::size_t cache_size = 32, /// size_nil means always cache object 66 | std::size_t reserve_size = 32, 67 | allocator_t a = allocator_t() 68 | ) 69 | : owner_(owner) 70 | , pool_(cache_size, reserve_size, a) 71 | { 72 | } 73 | 74 | ~object_pool() 75 | { 76 | } 77 | 78 | public: 79 | inline pointer get() 80 | { 81 | BOOST_STATIC_ASSERT((boost::is_base_of::value)); 82 | 83 | return pool_.get(); 84 | } 85 | 86 | inline void free(pointer obj) 87 | { 88 | BOOST_STATIC_ASSERT((boost::is_base_of::value)); 89 | 90 | pool_.free(obj); 91 | } 92 | 93 | inline cache_pool* get_owner() { return owner_; } 94 | 95 | private: 96 | cache_pool* owner_; 97 | pool_t pool_; 98 | }; 99 | } 100 | } 101 | 102 | #endif /// GCE_ACTOR_DETAIL_OBJECT_POOL_HPP 103 | -------------------------------------------------------------------------------- /gce/actor/atom.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_ATOM_HPP 11 | #define GCE_ACTOR_ATOM_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | namespace gce 19 | { 20 | /// Since lordoffox's str2val.h (http://bbs.cppfans.org/forum.php?mod=viewthread&tid=56&extra=page%3D1) 21 | inline boost::uint64_t atom(char const* str) 22 | { 23 | std::size_t len = std::strlen(str); 24 | if (len > 14) 25 | { 26 | return 0; 27 | } 28 | 29 | static char const* const encoding_table = 30 | "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0" 31 | "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0" 32 | "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0" 33 | "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0" 34 | "\x0\x1\x2\x3\x4\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf" 35 | "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x0\x0\x0\x0\x1b" 36 | "\x0\x1\x2\x3\x4\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf" 37 | "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x0\x0\x0\x0\x0" 38 | "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0" 39 | "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0" 40 | "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0" 41 | "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0" 42 | "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0" 43 | "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0" 44 | "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0" 45 | "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0"; 46 | 47 | boost::uint64_t value = 0; 48 | boost::uint32_t encode_value = 0; 49 | for (std::size_t i=0 ; i 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | namespace gce 21 | { 22 | namespace detail 23 | { 24 | /// Unique pointer with deleter(like C++11 std::unique_ptr). 25 | /// All member functions never throws. 26 | template 27 | class unique_ptr 28 | { 29 | typedef boost::function deleter_t; 30 | unique_ptr(unique_ptr const&); 31 | unique_ptr& operator=(unique_ptr const&); 32 | 33 | typedef unique_ptr self_t; 34 | 35 | void operator==(unique_ptr const&) const; 36 | void operator!=(unique_ptr const&) const; 37 | 38 | public: 39 | unique_ptr() 40 | : px_(0) 41 | { 42 | } 43 | 44 | explicit unique_ptr(T* p) 45 | : px_(p) 46 | { 47 | } 48 | 49 | template 50 | unique_ptr(T* p, D d) 51 | : px_(p) 52 | , deleter_(d) 53 | { 54 | } 55 | 56 | ~unique_ptr() 57 | { 58 | if (deleter_) 59 | { 60 | deleter_(px_); 61 | } 62 | else 63 | { 64 | boost::checked_delete(px_); 65 | } 66 | } 67 | 68 | public: 69 | void reset() 70 | { 71 | self_t().swap(*this); 72 | } 73 | 74 | void reset(T* p) 75 | { 76 | BOOST_ASSERT(p == 0 || p != px_); 77 | self_t(p).swap(*this); 78 | } 79 | 80 | template 81 | void reset(T* p, D d) 82 | { 83 | BOOST_ASSERT(p == 0 || p != px_); 84 | self_t(p, d).swap(*this); 85 | } 86 | 87 | T& operator*() const 88 | { 89 | BOOST_ASSERT(px_ != 0); 90 | return *px_; 91 | } 92 | 93 | T* operator->() const 94 | { 95 | BOOST_ASSERT(px_ != 0); 96 | return px_; 97 | } 98 | 99 | T* get() const 100 | { 101 | return px_; 102 | } 103 | 104 | operator bool() const 105 | { 106 | return px_ != 0; 107 | } 108 | 109 | bool operator!() const 110 | { 111 | return px_ == 0; 112 | } 113 | 114 | void swap(unique_ptr& b) 115 | { 116 | std::swap(px_, b.px_); 117 | std::swap(deleter_, b.deleter_); 118 | } 119 | 120 | private: 121 | T* px_; 122 | deleter_t deleter_; 123 | }; 124 | 125 | template 126 | struct empty_deleter 127 | { 128 | void operator()(T*) const {} 129 | }; 130 | } 131 | } 132 | 133 | #endif /// GCE_DETAIL_UNIQUE_PTR_HPP 134 | -------------------------------------------------------------------------------- /libs/actor/example/cluster/app.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_EXAMPLE_CLUSTER_APP_HPP 11 | #define GCE_ACTOR_EXAMPLE_CLUSTER_APP_HPP 12 | 13 | #include "hash.hpp" 14 | #include 15 | #include 16 | #include 17 | 18 | typedef std::vector app_ctxid_list_t; 19 | ///---------------------------------------------------------------------------- 20 | inline gce::svcid_t select_game_app( 21 | app_ctxid_list_t const& game_list, 22 | std::string const& username 23 | ) 24 | { 25 | hash_t h = hash(username.data(), username.size()); 26 | gce::match_t i = h % game_list.size(); 27 | BOOST_ASSERT(i <= game_list.size()); 28 | return game_list[i]; 29 | } 30 | ///---------------------------------------------------------------------------- 31 | struct app_init 32 | { 33 | app_init(gce::ctxid_t node_id, std::string ep, gce::match_t name) 34 | : node_id_(node_id) 35 | , ep_(ep) 36 | , name_(name) 37 | { 38 | } 39 | 40 | gce::ctxid_t node_id_; 41 | std::string ep_; 42 | gce::match_t name_; 43 | }; 44 | ///---------------------------------------------------------------------------- 45 | struct game_ep_info 46 | { 47 | gce::ctxid_t node_id_; 48 | std::string ep_; 49 | }; 50 | GCE_PACK(game_ep_info, (node_id_)(ep_)); 51 | ///---------------------------------------------------------------------------- 52 | struct gate_info 53 | { 54 | gce::match_t svc_name_; 55 | std::string ep_; 56 | app_ctxid_list_t game_list_; 57 | }; 58 | GCE_PACK(gate_info, (svc_name_)(ep_)(game_list_)); 59 | ///---------------------------------------------------------------------------- 60 | struct game_info 61 | { 62 | gce::match_t svc_name_; 63 | std::string ep_; 64 | app_ctxid_list_t game_list_; 65 | }; 66 | GCE_PACK(game_info, (svc_name_)(ep_)(game_list_)); 67 | ///---------------------------------------------------------------------------- 68 | typedef app_init app_init_t; 69 | struct router_init 70 | { 71 | gce::ctxid_t first; 72 | std::string second; 73 | }; 74 | typedef router_init router_init_t; 75 | GCE_PACK(router_init, (first)(second)); 76 | ///---------------------------------------------------------------------------- 77 | struct node_info 78 | { 79 | std::vector gate_list_; 80 | std::vector game_list_; 81 | std::vector router_list_; 82 | }; 83 | GCE_PACK(node_info, (gate_list_)(game_list_)(router_list_)); 84 | ///---------------------------------------------------------------------------- 85 | 86 | #endif /// GCE_ACTOR_EXAMPLE_CLUSTER_APP_HPP 87 | -------------------------------------------------------------------------------- /libs/actor/test/test_service.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #include 11 | 12 | namespace gce 13 | { 14 | class service_ut 15 | { 16 | public: 17 | static void run() 18 | { 19 | std::cout << "service_ut begin." << std::endl; 20 | test_common(); 21 | std::cout << "service_ut end." << std::endl; 22 | } 23 | 24 | private: 25 | static void test_common() 26 | { 27 | try 28 | { 29 | std::size_t echo_num = 100; 30 | 31 | attributes attrs; 32 | attrs.id_ = atom("router"); 33 | context ctx(attrs); 34 | attrs.id_ = atom("one"); 35 | context ctx1(attrs); 36 | attrs.id_ = atom("two"); 37 | context ctx2(attrs); 38 | 39 | actor base = spawn(ctx); 40 | actor base1 = spawn(ctx1); 41 | actor base2 = spawn(ctx2); 42 | 43 | gce::bind(base, "tcp://127.0.0.1:14923", true); 44 | 45 | spawn( 46 | base2, 47 | boost::bind( 48 | &service_ut::echo_service, _1 49 | ), 50 | monitored 51 | ); 52 | svcid_t echo_svc(atom("two"), atom("echo_svc")); 53 | 54 | net_option opt; 55 | opt.reconn_period_ = seconds_t(1); 56 | connect(base1, atom("router"), "tcp://127.0.0.1:14923", true, opt); 57 | connect(base2, atom("router"), "tcp://127.0.0.1:14923", true, opt); 58 | wait(base2, boost::chrono::milliseconds(100)); 59 | 60 | for (std::size_t i=0; i& self) 76 | { 77 | try 78 | { 79 | register_service(self, atom("echo_svc")); 80 | 81 | while (true) 82 | { 83 | message msg; 84 | aid_t sender = self.recv(msg); 85 | match_t type = msg.get_type(); 86 | if (type == atom("echo")) 87 | { 88 | self.send(sender, msg); 89 | } 90 | else 91 | { 92 | break; 93 | } 94 | } 95 | deregister_service(self, atom("echo_svc")); 96 | } 97 | catch (std::exception& ex) 98 | { 99 | std::cerr << "echo except: " << ex.what() << std::endl; 100 | } 101 | } 102 | }; 103 | } 104 | -------------------------------------------------------------------------------- /libs/actor/example/cluster/main.cpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #include "gate.hpp" 11 | #include "game.hpp" 12 | #include "node.hpp" 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | int main(int argc, char* argv[]) 23 | { 24 | try 25 | { 26 | if (argc < 4) 27 | { 28 | std::cerr << 29 | "Usage: " << std::endl; 30 | std::cerr << "// if is_master != 0 " << 31 | " [ ...] \ 32 | [ ...] \ 33 | [ ...]" 34 | << std::endl; 35 | std::cerr << "// else " << 36 | "" 37 | << std::endl; 38 | return 0; 39 | } 40 | 41 | std::vector gate_list; 42 | std::vector game_list; 43 | std::vector router_list; 44 | 45 | gce::ctxid_t node_id = gce::atom(argv[1]); 46 | std::string master_ep = argv[2]; 47 | bool is_master = boost::lexical_cast(argv[3]); 48 | gce::ctxid_t master_node_id = node_id; 49 | if (is_master) 50 | { 51 | std::size_t i = 4; 52 | std::size_t gate_count = 53 | boost::lexical_cast(argv[i]); 54 | ++i; 55 | for (std::size_t j=0; j(argv[i]); 63 | ++i; 64 | for (std::size_t j=0; j(argv[i]); 72 | ++i; 73 | for (std::size_t j=0; j 11 | #include 12 | #include 13 | 14 | namespace gce 15 | { 16 | namespace detail 17 | { 18 | ///---------------------------------------------------------------------------- 19 | heartbeat::heartbeat(strand_t& snd) 20 | : snd_(snd) 21 | , tmr_(snd_.get_io_service()) 22 | , sync_(snd_.get_io_service()) 23 | , max_count_(0) 24 | , curr_count_(0) 25 | , stopped_(false) 26 | , waiting_(0) 27 | { 28 | } 29 | ///---------------------------------------------------------------------------- 30 | heartbeat::~heartbeat() 31 | { 32 | } 33 | ///---------------------------------------------------------------------------- 34 | void heartbeat::start() 35 | { 36 | curr_count_ = max_count_; 37 | stopped_ = false; 38 | start_timer(); 39 | } 40 | ///---------------------------------------------------------------------------- 41 | void heartbeat::stop() 42 | { 43 | if (!stopped_) 44 | { 45 | stopped_ = true; 46 | errcode_t ignore_ec; 47 | tmr_.cancel(ignore_ec); 48 | } 49 | } 50 | ///---------------------------------------------------------------------------- 51 | void heartbeat::beat() 52 | { 53 | if (!stopped_) 54 | { 55 | curr_count_ = max_count_; 56 | } 57 | } 58 | ///---------------------------------------------------------------------------- 59 | void heartbeat::wait_end(yield_t yield) 60 | { 61 | if (waiting_ > 0) 62 | { 63 | errcode_t ec; 64 | sync_.expires_from_now(infin); 65 | sync_.async_wait(yield[ec]); 66 | } 67 | } 68 | ///---------------------------------------------------------------------------- 69 | void heartbeat::clear() 70 | { 71 | timeout_.clear(); 72 | tick_.clear(); 73 | } 74 | ///---------------------------------------------------------------------------- 75 | void heartbeat::start_timer() 76 | { 77 | ++waiting_; 78 | tmr_.expires_from_now(period_); 79 | tmr_.async_wait( 80 | snd_.wrap( 81 | boost::bind( 82 | &heartbeat::handle_timeout, this, 83 | boost::asio::placeholders::error 84 | ) 85 | ) 86 | ); 87 | } 88 | ///---------------------------------------------------------------------------- 89 | void heartbeat::handle_timeout(errcode_t const& errc) 90 | { 91 | --waiting_; 92 | if (!stopped_ && !errc) 93 | { 94 | --curr_count_; 95 | if (tick_) 96 | { 97 | tick_(); 98 | } 99 | 100 | if (curr_count_ == 0) 101 | { 102 | timeout_(); 103 | } 104 | else 105 | { 106 | start_timer(); 107 | } 108 | } 109 | 110 | if (stopped_) 111 | { 112 | errcode_t ignore_ec; 113 | sync_.cancel(ignore_ec); 114 | } 115 | } 116 | ///---------------------------------------------------------------------------- 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /libs/actor/test/test_stackless_pingpong.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class stackless_pingpong_ut 13 | { 14 | static std::size_t const msg_size = 100000; 15 | public: 16 | static void run() 17 | { 18 | std::cout << "stackless_pingpong_ut begin." << std::endl; 19 | test(); 20 | std::cout << "stackless_pingpong_ut end." << std::endl; 21 | } 22 | 23 | private: 24 | class my_child 25 | : public boost::enable_shared_from_this 26 | { 27 | public: 28 | explicit my_child(aid_t base_id) 29 | : base_id_(base_id) 30 | { 31 | } 32 | 33 | ~my_child() 34 | { 35 | } 36 | 37 | public: 38 | void run(actor& self) 39 | { 40 | GCE_REENTER (self) 41 | { 42 | while (true) 43 | { 44 | GCE_YIELD self.recv(sire_, msg_); 45 | if (msg_.get_type() == 2) 46 | { 47 | break; 48 | } 49 | else 50 | { 51 | self.send(sire_, msg_); 52 | } 53 | } 54 | self.send(base_id_, msg_); 55 | } 56 | } 57 | 58 | private: 59 | aid_t base_id_; 60 | aid_t sire_; 61 | message msg_; 62 | }; 63 | 64 | class my_actor 65 | : public boost::enable_shared_from_this 66 | { 67 | public: 68 | explicit my_actor(aid_t base_id) 69 | : base_id_(base_id) 70 | , msg_(1) 71 | { 72 | } 73 | 74 | ~my_actor() 75 | { 76 | } 77 | 78 | public: 79 | void run(actor& self) 80 | { 81 | GCE_REENTER (self) 82 | { 83 | GCE_YIELD spawn( 84 | self, 85 | boost::bind( 86 | &my_child::run, 87 | boost::make_shared(base_id_), 88 | _1 89 | ), 90 | aid_ 91 | ); 92 | 93 | for (i_=0; i_ base = spawn(ctx); 117 | 118 | aid_t base_id = base.get_aid(); 119 | aid_t aid = 120 | spawn( 121 | base, 122 | boost::bind( 123 | &my_actor::run, 124 | boost::make_shared(base_id), 125 | _1 126 | ) 127 | ); 128 | 129 | boost::timer::auto_cpu_timer t; 130 | recv(base); 131 | } 132 | catch (std::exception& ex) 133 | { 134 | std::cerr << ex.what() << std::endl; 135 | } 136 | } 137 | }; 138 | } 139 | 140 | -------------------------------------------------------------------------------- /libs/actor/test/test_socket_broken.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class socket_broken_ut 13 | { 14 | public: 15 | static void run() 16 | { 17 | std::cout << "socket_broken_ut begin." << std::endl; 18 | test_base(); 19 | std::cout << "socket_broken_ut end." << std::endl; 20 | } 21 | 22 | public: 23 | static void test_base() 24 | { 25 | try 26 | { 27 | std::size_t quiter_num = 100; 28 | 29 | attributes attrs; 30 | attrs.id_ = atom("one"); 31 | context ctx(attrs); 32 | actor base = spawn(ctx); 33 | 34 | actor a = spawn(ctx); 35 | gce::bind(base, "tcp://127.0.0.1:14923"); 36 | 37 | std::vector quiter_list(quiter_num); 38 | boost::thread thr( 39 | boost::bind( 40 | &socket_broken_ut::two, 41 | boost::ref(quiter_list), 42 | base.get_aid(), boost::ref(a) 43 | ) 44 | ); 45 | 46 | recv(base); 47 | BOOST_FOREACH(aid_t const& aid, quiter_list) 48 | { 49 | base.link(aid); 50 | } 51 | 52 | thr.join(); 53 | 54 | for (std::size_t i=0; i& quiter_list, 67 | aid_t base_id, actor a 68 | ) 69 | { 70 | try 71 | { 72 | attributes attrs; 73 | attrs.id_ = atom("two"); 74 | context ctx(attrs); 75 | actor base = spawn(ctx); 76 | 77 | wait(base, boost::chrono::milliseconds(100)); 78 | net_option opt; 79 | opt.reconn_period_ = seconds_t(1); 80 | connect(base, atom("one"), "tcp://127.0.0.1:14923", false, opt); 81 | 82 | BOOST_FOREACH(aid_t& aid, quiter_list) 83 | { 84 | aid = 85 | spawn( 86 | base, 87 | boost::bind( 88 | &socket_broken_ut::quiter, _1 89 | ), 90 | monitored 91 | ); 92 | } 93 | 94 | send(a, base_id, atom("notify")); 95 | BOOST_FOREACH(aid_t const& aid, quiter_list) 96 | { 97 | send(base, aid, atom("quit")); 98 | } 99 | 100 | for (std::size_t i=0; i& self) 112 | { 113 | try 114 | { 115 | recv(self); 116 | } 117 | catch (std::exception& ex) 118 | { 119 | std::cerr << "quiter except: " << ex.what() << std::endl; 120 | } 121 | } 122 | }; 123 | } 124 | -------------------------------------------------------------------------------- /libs/actor/test/test_router_broken.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class router_broken_ut 13 | { 14 | public: 15 | static void run() 16 | { 17 | std::cout << "router_broken_ut begin." << std::endl; 18 | test_base(); 19 | std::cout << "router_broken_ut end." << std::endl; 20 | } 21 | 22 | public: 23 | static void test_base() 24 | { 25 | try 26 | { 27 | std::size_t quiter_num = 100; 28 | 29 | attributes attrs; 30 | attrs.id_ = atom("one"); 31 | context ctx1(attrs); 32 | attrs.id_ = atom("two"); 33 | context ctx2(attrs); 34 | 35 | actor base1 = spawn(ctx1); 36 | actor base2 = spawn(ctx2); 37 | 38 | actor a = spawn(ctx1); 39 | 40 | boost::thread thr( 41 | boost::bind( 42 | &router_broken_ut::router, 43 | base1.get_aid(), boost::ref(a) 44 | ) 45 | ); 46 | aid_t mix_id = recv(base1); 47 | 48 | net_option opt; 49 | opt.reconn_period_ = seconds_t(1); 50 | connect(base1, atom("router"), "tcp://127.0.0.1:14923", true, opt); 51 | connect(base2, atom("router"), "tcp://127.0.0.1:14923", true, opt); 52 | wait(base2, boost::chrono::milliseconds(100)); 53 | 54 | std::vector quiter_list(quiter_num); 55 | for (std::size_t i=0; i mix) 89 | { 90 | try 91 | { 92 | attributes attrs; 93 | attrs.id_ = atom("router"); 94 | context ctx(attrs); 95 | actor base = spawn(ctx); 96 | 97 | gce::bind(base, "tcp://127.0.0.1:14923", true); 98 | send(mix, base_id); 99 | recv(mix); 100 | } 101 | catch (std::exception& ex) 102 | { 103 | std::cerr << "router except: " << ex.what() << std::endl; 104 | } 105 | } 106 | 107 | static void quiter(actor& self) 108 | { 109 | try 110 | { 111 | recv(self); 112 | } 113 | catch (std::exception& ex) 114 | { 115 | std::cerr << "quiter except: " << ex.what() << std::endl; 116 | } 117 | } 118 | }; 119 | } 120 | -------------------------------------------------------------------------------- /libs/actor/test/test_actor.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class actor_ut 13 | { 14 | public: 15 | static void run() 16 | { 17 | std::cout << "actor_ut begin." << std::endl; 18 | test_common(); 19 | std::cout << "actor_ut end." << std::endl; 20 | } 21 | 22 | private: 23 | static void my_child(actor& self) 24 | { 25 | aid_t aid = recv(self); 26 | //wait(self, seconds_t(3)); 27 | reply(self, aid); 28 | } 29 | 30 | static void my_actor(actor& self, aid_t base_id) 31 | { 32 | std::size_t size = 50; 33 | std::vector res_list(size); 34 | for (std::size_t i=0; iget_context().get_io_service()); 45 | yield_t yield = self.get_yield(); 46 | tmr.expires_from_now(boost::chrono::milliseconds(1)); 47 | tmr.async_wait(yield); 48 | 49 | for (std::size_t i=0; i a = spawn(ctx); 69 | for (std::size_t i=0; i<2; ++i) 70 | { 71 | spawn(a, boost::bind(&actor_ut::my_actor, _1, base_id)); 72 | } 73 | } 74 | 75 | static void test_common() 76 | { 77 | try 78 | { 79 | std::size_t free_actor_num = 20; 80 | std::size_t user_thr_num = 0; 81 | std::size_t my_actor_size = free_actor_num + user_thr_num * 2; 82 | attributes attrs; 83 | context ctx(attrs); 84 | actor base = spawn(ctx); 85 | 86 | aid_t base_id = base.get_aid(); 87 | for (std::size_t i=0; i& self, app_ctxid_list_t game_list, 15 | gce::aid_t old_usr_aid, gce::aid_t master, 16 | cid_t cid, std::string username, std::string passwd 17 | ) 18 | { 19 | try 20 | { 21 | if (old_usr_aid) 22 | { 23 | /// verify username and passwd, if valid then kick old session 24 | gce::response_t res = 25 | gce::request(self, old_usr_aid, gce::atom("kick")); 26 | gce::recv(self); 27 | } 28 | else 29 | { 30 | /// check db/cache if username existed 31 | /// if existed, verify username and passwd; or register 32 | } 33 | 34 | /// verify or register ok, link cid; 35 | self.link(cid); 36 | 37 | /// response cln_login ok or error 38 | gce::aid_t sender = gce::recv(self, gce::atom("cln_login")); 39 | gce::reply(self, sender, gce::atom("ok"), std::string()); 40 | 41 | /// loop handle messages 42 | bool running = true; 43 | while (running) 44 | { 45 | gce::message msg; 46 | gce::aid_t sender = self.recv(msg); 47 | gce::match_t type = msg.get_type(); 48 | if (type == gce::exit) 49 | { 50 | running = false; 51 | } 52 | else if (type == gce::atom("kick")) 53 | { 54 | running = false; 55 | gce::reply(self, sender, gce::atom("ok")); 56 | } 57 | else if (type == gce::atom("chat")) 58 | { 59 | gce::message m(gce::atom("fwd_msg")); 60 | m << msg; 61 | self.send(cid, m); 62 | } 63 | else if (type == gce::atom("chat_to")) 64 | { 65 | std::string target; 66 | msg >> target; 67 | if (target != username) 68 | { 69 | /// find game app and send chat msg 70 | gce::svcid_t game_svcid = select_game_app(game_list, target); 71 | self.send(game_svcid, msg); 72 | } 73 | else 74 | { 75 | /// send to self 76 | gce::message m(gce::atom("fwd_msg")); 77 | m << msg; 78 | self.send(cid, m); 79 | } 80 | } 81 | else if (type == gce::atom("cln_logout")) 82 | { 83 | running = false; 84 | } 85 | else 86 | { 87 | std::string errmsg("user::run unexpected message, type: "); 88 | errmsg += gce::atom(type); 89 | throw std::runtime_error(errmsg); 90 | } 91 | } 92 | 93 | std::printf("user quit\n"); 94 | } 95 | catch (std::exception& ex) 96 | { 97 | std::printf("user::run except: %s\n", ex.what()); 98 | } 99 | /// do some clean job before erase self from game_app 100 | gce::send(self, master, gce::atom("rmv_user"), username); 101 | } 102 | ///---------------------------------------------------------------------------- 103 | -------------------------------------------------------------------------------- /libs/actor/test/test_slice.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class slice_ut 13 | { 14 | public: 15 | class my_3d_engine 16 | : public boost::noncopyable 17 | { 18 | public: 19 | typedef boost::chrono::system_clock system_clock_t; 20 | typedef system_clock_t::time_point time_point_t; 21 | typedef boost::chrono::milliseconds milliseconds_t; 22 | typedef boost::chrono::milliseconds::rep fps_rep_t; 23 | 24 | public: 25 | my_3d_engine(context& ctx, int count_down, fps_rep_t fps = 60) 26 | : frame_(1000/fps) 27 | , base_(spawn(ctx)) 28 | , cln_(spawn(base_)) 29 | , stopped_(false) 30 | { 31 | aid_t counter = spawn(base_, boost::bind(&slice_ut::cd, _1)); 32 | send(cln_, counter, atom("cd"), count_down); 33 | } 34 | 35 | ~my_3d_engine() 36 | { 37 | } 38 | 39 | public: 40 | void run() 41 | { 42 | milliseconds_t last_dt = frame_; 43 | while (!stopped_) 44 | { 45 | time_point_t begin_tp = system_clock_t::now(); 46 | update(last_dt); 47 | last_dt = 48 | boost::chrono::duration_cast( 49 | system_clock_t::now() - begin_tp 50 | ); 51 | if (last_dt < frame_) 52 | { 53 | boost::this_thread::sleep_for(frame_ - last_dt); 54 | last_dt = frame_; 55 | } 56 | } 57 | } 58 | 59 | private: 60 | void update(milliseconds_t /*dt*/) 61 | { 62 | int count_down; 63 | aid_t counter = recv(cln_, atom("cd"), count_down); 64 | if (counter) 65 | { 66 | if (count_down > 0) 67 | { 68 | send(cln_, counter, atom("cd"), count_down); 69 | } 70 | else 71 | { 72 | stopped_ = true; 73 | } 74 | } 75 | } 76 | 77 | private: 78 | milliseconds_t const frame_; 79 | actor base_; 80 | actor cln_; 81 | bool stopped_; 82 | }; 83 | 84 | static void cd(actor& self) 85 | { 86 | try 87 | { 88 | int count_down; 89 | while (true) 90 | { 91 | aid_t cln = recv(self, atom("cd"), count_down); 92 | --count_down; 93 | send(self, cln, atom("cd"), count_down); 94 | if (count_down <= 0) 95 | { 96 | break; 97 | } 98 | } 99 | } 100 | catch (std::exception& ex) 101 | { 102 | std::cerr << ex.what() << std::endl; 103 | } 104 | } 105 | 106 | public: 107 | static void run() 108 | { 109 | std::cout << "slice_ut begin." << std::endl; 110 | test_base(); 111 | std::cout << "slice_ut end." << std::endl; 112 | } 113 | 114 | public: 115 | static void test_base() 116 | { 117 | try 118 | { 119 | context ctx; 120 | my_3d_engine engine(ctx, 20); 121 | engine.run(); 122 | } 123 | catch (std::exception& ex) 124 | { 125 | std::cerr << ex.what() << std::endl; 126 | } 127 | } 128 | }; 129 | } 130 | -------------------------------------------------------------------------------- /gce/actor/thread_mapped_actor.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_MIXIN_HPP 11 | #define GCE_ACTOR_MIXIN_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | namespace gce 23 | { 24 | class thread_mapped_actor; 25 | class nonblocking_actor; 26 | class context; 27 | struct attributes; 28 | namespace detail 29 | { 30 | class cache_pool; 31 | } 32 | 33 | class thread_mapped_actor 34 | : public basic_actor 35 | { 36 | typedef basic_actor base_type; 37 | 38 | public: 39 | explicit thread_mapped_actor(detail::cache_pool*); 40 | ~thread_mapped_actor(); 41 | 42 | public: 43 | void send(aid_t, message const&); 44 | void send(svcid_t, message const&); 45 | void relay(aid_t, message&); 46 | void relay(svcid_t, message&); 47 | 48 | response_t request(aid_t, message const&); 49 | response_t request(svcid_t, message const&); 50 | void reply(aid_t, message const&); 51 | 52 | void link(aid_t); 53 | void monitor(aid_t); 54 | 55 | aid_t recv(message&, match const& mach = match()); 56 | aid_t recv( 57 | response_t, message&, 58 | duration_t tmo = seconds_t(GCE_DEFAULT_REQUEST_TIMEOUT_SEC) 59 | ); 60 | void wait(duration_t); 61 | 62 | public: 63 | /// internal use 64 | inline void add_nonblocking_actor(nonblocking_actor& a) 65 | { 66 | nonblocking_actor_list_.push_back(&a); 67 | } 68 | inline std::vector& get_nonblocking_actor_list() 69 | { 70 | return nonblocking_actor_list_; 71 | } 72 | void on_recv(detail::pack&, base_type::send_hint); 73 | 74 | sid_t spawn(detail::spawn_type, match_t func, match_t ctxid, std::size_t stack_size); 75 | 76 | private: 77 | typedef boost::optional > recv_optional_t; 78 | typedef boost::optional > res_optional_t; 79 | typedef boost::promise recv_promise_t; 80 | typedef boost::promise res_promise_t; 81 | typedef boost::unique_future recv_future_t; 82 | typedef boost::unique_future res_future_t; 83 | void try_recv(recv_promise_t&, match const&); 84 | void try_response(res_promise_t&, response_t, duration_t); 85 | void start_recv_timer(duration_t, recv_promise_t&); 86 | void start_recv_timer(duration_t, res_promise_t&); 87 | void handle_recv_timeout(errcode_t const&, recv_promise_t&, std::size_t); 88 | void handle_res_timeout(errcode_t const&, res_promise_t&, std::size_t); 89 | 90 | void handle_recv(detail::pack&); 91 | 92 | private: 93 | /// Ensure start from a new cache line. 94 | byte_t pad0_[GCE_CACHE_LINE_SIZE]; 95 | 96 | /// local 97 | std::vector nonblocking_actor_list_; 98 | recv_promise_t* recv_p_; 99 | res_promise_t* res_p_; 100 | response_t recving_res_; 101 | match curr_match_; 102 | timer_t tmr_; 103 | std::size_t tmr_sid_; 104 | }; 105 | } 106 | 107 | #endif /// GCE_ACTOR_MIXIN_HPP 108 | -------------------------------------------------------------------------------- /libs/actor/test/test_message.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | namespace gce 19 | { 20 | struct arg1_t 21 | { 22 | std::string hi_; 23 | int i_; 24 | }; 25 | 26 | struct arg2_t 27 | { 28 | std::vector v_; 29 | int i_; 30 | }; 31 | } 32 | GCE_PACK(gce::arg1_t, (hi_&smax(100))(i_&sfix)); 33 | GCE_PACK(gce::arg2_t, (v_&smax(5))(i_)); 34 | 35 | namespace gce 36 | { 37 | static std::size_t const lv1_thr_num = 5; 38 | static std::size_t const lv2_thr_num = 5; 39 | class message_ut 40 | { 41 | public: 42 | static void run() 43 | { 44 | std::cout << "message_ut begin." << std::endl; 45 | for (std::size_t i=0; i<100; ++i) 46 | { 47 | test_common(); 48 | } 49 | std::cout << "message_ut end." << std::endl; 50 | } 51 | 52 | static void print(std::string const& tag, int size, message& msg, int i = 0) 53 | { 54 | arg1_t arg1; 55 | arg2_t arg2; 56 | 57 | std::stringstream ss; 58 | ss << "begin [" << tag << "," << i << "]\n"; 59 | { 60 | msg >> arg1 >> arg2; 61 | ss << "arg1: " << arg1.hi_ << ", " << arg1.i_ << ", arg2: " 62 | << arg2.v_[0] << ", " << arg2.v_[1] << ", " << arg2.v_[2] 63 | << ", " << arg2.v_[3] << ", " << arg2.v_[4] << ", " 64 | << arg2.i_ << "\n"; 65 | } 66 | 67 | for (i=1; i> m; 71 | print(tag, 0, m, i); 72 | } 73 | ss << "end.\n"; 74 | //std::cout << ss.str(); 75 | } 76 | 77 | static void add_arg(message& msg) 78 | { 79 | arg1_t arg1; 80 | arg1.hi_ = "arg1.hi_, need more words"; 81 | arg1.i_ = 1; 82 | arg2_t arg2; 83 | arg2.v_ = boost::assign::list_of(1)(2)(3)(4)(5).to_container(arg2.v_); 84 | arg2.i_ = 2; 85 | msg << arg1 << arg2; 86 | } 87 | 88 | static void lv2_thr(int size, message& msg) 89 | { 90 | print("lv2_thr", size, msg); 91 | } 92 | 93 | static void lv1_thr(message& msg) 94 | { 95 | print("lv1_thr", 2, msg); 96 | 97 | boost::thread_group thrs; 98 | for (std::size_t i=0; i 1) 107 | { 108 | size = 3; 109 | } 110 | thrs.create_thread(boost::bind(&message_ut::lv2_thr, size, msg)); 111 | } 112 | 113 | thrs.join_all(); 114 | } 115 | 116 | static void test_common() 117 | { 118 | try 119 | { 120 | message m(1); 121 | add_arg(m); 122 | m << m; 123 | 124 | boost::thread_group thrs; 125 | for (std::size_t i=0; i 14 | #include 15 | #include 16 | 17 | namespace gce 18 | { 19 | class basic_actor; 20 | class actor_id 21 | { 22 | public: 23 | actor_id() 24 | : ctxid_(ctxid_nil) 25 | , timestamp_(0) 26 | , uintptr_(0) 27 | , sid_(sid_nil) 28 | { 29 | } 30 | 31 | actor_id( 32 | ctxid_t ctxid, timestamp_t timestamp, 33 | basic_actor* ptr, sid_t sid 34 | ) 35 | : ctxid_(ctxid) 36 | , timestamp_(timestamp) 37 | , uintptr_((boost::uint64_t)ptr) 38 | , sid_(sid) 39 | { 40 | } 41 | 42 | ~actor_id() 43 | { 44 | } 45 | 46 | public: 47 | inline operator bool() const 48 | { 49 | return uintptr_ != 0; 50 | } 51 | 52 | inline bool operator!() const 53 | { 54 | return uintptr_ == 0; 55 | } 56 | 57 | inline bool operator==(actor_id const& rhs) const 58 | { 59 | return 60 | ctxid_ == rhs.ctxid_ && 61 | timestamp_ == rhs.timestamp_ && 62 | uintptr_ == rhs.uintptr_ && 63 | sid_ == rhs.sid_; 64 | } 65 | 66 | inline bool operator!=(actor_id const& rhs) const 67 | { 68 | return !(*this == rhs); 69 | } 70 | 71 | inline bool operator<(actor_id const& rhs) const 72 | { 73 | if (ctxid_ < rhs.ctxid_) 74 | { 75 | return true; 76 | } 77 | else if (ctxid_ > rhs.ctxid_) 78 | { 79 | return false; 80 | } 81 | 82 | if (timestamp_ < rhs.timestamp_) 83 | { 84 | return true; 85 | } 86 | else if (timestamp_ > rhs.timestamp_) 87 | { 88 | return false; 89 | } 90 | 91 | if (uintptr_ < rhs.uintptr_) 92 | { 93 | return true; 94 | } 95 | else if (uintptr_ > rhs.uintptr_) 96 | { 97 | return false; 98 | } 99 | 100 | if (sid_ < rhs.sid_) 101 | { 102 | return true; 103 | } 104 | else if (sid_ > rhs.sid_) 105 | { 106 | return false; 107 | } 108 | 109 | return false; 110 | } 111 | 112 | inline basic_actor* get_actor_ptr(ctxid_t ctxid, timestamp_t timestamp) const 113 | { 114 | BOOST_ASSERT(ctxid == ctxid_); 115 | BOOST_ASSERT(timestamp == timestamp_); 116 | BOOST_ASSERT(uintptr_ != 0); 117 | return (basic_actor*)uintptr_; 118 | } 119 | 120 | ctxid_t ctxid_; 121 | timestamp_t timestamp_; 122 | boost::uint64_t uintptr_; 123 | sid_t sid_; 124 | 125 | /// internal use 126 | inline void set_svcid(svcid_t svc) 127 | { 128 | svc_ = svc; 129 | } 130 | 131 | svcid_t svc_; 132 | }; 133 | 134 | typedef actor_id aid_t; 135 | typedef actor_id sktaid_t; 136 | } 137 | 138 | template 139 | std::basic_ostream& operator<<( 140 | std::basic_ostream& strm, gce::aid_t const& aid 141 | ) 142 | { 143 | strm << "<" << aid.ctxid_ << "." << aid.timestamp_ << 144 | "." << aid.uintptr_ << "." << aid.sid_ << "." << aid.svc_ << ">"; 145 | return strm; 146 | } 147 | 148 | GCE_PACK(gce::aid_t, (ctxid_)(timestamp_)(uintptr_)(sid_)(svc_)); 149 | 150 | #endif /// GCE_ACTOR_ACTOR_ID_HPP 151 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is part of the CMake build system for Gce 3 | # 4 | # CMake auto-generated configuration options. 5 | # Do not check in modified versions of this file. 6 | # 7 | # Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 8 | # 9 | # Distributed under the Boost Software License, Version 1.0. (See accompanying 10 | # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 11 | # 12 | # See https://github.com/nousxiong/gce for latest version. 13 | # 14 | 15 | cmake_minimum_required (VERSION 2.8.6 FATAL_ERROR) 16 | project (gce) 17 | 18 | # The version number. 19 | set (GCE_VERSION_MAJOR 1) 20 | set (GCE_VERSION_MINOR 0) 21 | 22 | # Provide user options to customise the build process. 23 | set (GCE_ASIO_ALLOC_HANDLER_SIZE "1024" CACHE STRING "Boost.Asio handler allocator size") 24 | 25 | if (UNIX) 26 | option (GCE_STATIC "Build gce runtime static" OFF) 27 | endif () 28 | 29 | if (WIN32) 30 | set (GCE_WINVER "0x0501" CACHE STRING "Windows version maro. Default is 0x0501 - winxp, user can reset") 31 | add_definitions (-D_WIN32_WINNT=${GCE_WINVER}) 32 | endif () 33 | 34 | add_definitions (-DBOOST_ASIO_DISABLE_STD_CHRONO) 35 | 36 | # Set glibc. 37 | if (GCE_STATIC) 38 | set (GLIBC_INCLUDEDIR "" CACHE PATH "Path to glibc include directory") 39 | set (GLIBC_LIBRARYDIR "" CACHE PATH "Path to glibc libraries directory") 40 | if (GLIBC_INCLUDEDIR) 41 | include_directories (${GLIBC_INCLUDEDIR}) 42 | endif () 43 | if (GLIBC_LIBRARYDIR) 44 | link_directories (${GLIBC_LIBRARYDIR}) 45 | endif () 46 | endif () 47 | 48 | # Add the source and build tree to the search path for include gce header files. 49 | include_directories (${PROJECT_SOURCE_DIR}) 50 | include_directories (${PROJECT_BINARY_DIR}) 51 | 52 | # Boost libraries search. 53 | set (Boost_USE_STATIC_LIBS ON) 54 | set (Boost_USE_MULTITHREADED ON) 55 | if (GCE_STATIC) 56 | set (Boost_USE_STATIC_RUNTIME ON) 57 | else () 58 | set (Boost_USE_STATIC_RUNTIME OFF) 59 | endif () 60 | if (BOOST_ROOT) 61 | set (Boost_NO_SYSTEM_PATHS ON) 62 | endif () 63 | 64 | find_package (Boost 1.55.0 REQUIRED atomic coroutine context system regex date_time timer chrono thread) 65 | 66 | mark_as_advanced (Boost_DIR) 67 | if (WIN32) 68 | mark_as_advanced (Boost_LIB_DIAGNOSTIC_DEFINITIONS) 69 | endif () 70 | 71 | include_directories (${Boost_INCLUDE_DIRS}) 72 | 73 | # Find threads 74 | find_package (Threads) 75 | 76 | set (CMAKE_VERBOSE_MAKEFILE true) 77 | 78 | # Select build sub libs. 79 | set (SUB_LIBRARYS "actor amsg" CACHE STRING "Select build sub libs") 80 | string (TOLOWER ${SUB_LIBRARYS} SUB_LIBRARYS_TOLOWER) 81 | string (REGEX MATCHALL "[a-z_]+" SUB_LIB_NAME_LIST ${SUB_LIBRARYS_TOLOWER}) 82 | 83 | foreach (SUB_LIB_NAME ${SUB_LIB_NAME_LIST}) 84 | add_subdirectory (libs/${SUB_LIB_NAME}) 85 | endforeach(SUB_LIB_NAME) 86 | 87 | # Configure a header file to pass some of the CMake settings to the source code. 88 | configure_file ( 89 | "${PROJECT_SOURCE_DIR}/user.hpp.in" 90 | "${PROJECT_BINARY_DIR}/gce/user.hpp" 91 | ) 92 | 93 | install (FILES ${PROJECT_BINARY_DIR}/gce/user.hpp DESTINATION include/gce) 94 | 95 | # Build a CPack driven installer package. 96 | include (InstallRequiredSystemLibraries) 97 | set (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt") 98 | set (CPACK_PACKAGE_VERSION_MAJOR "${GCE_VERSION_MAJOR}") 99 | set (CPACK_PACKAGE_VERSION_MINOR "${GCE_VERSION_MINOR}") 100 | set (CPACK_PACKAGE_CONTACT "Nous Xiong: 348944179@qq.com") 101 | include (CPack) 102 | -------------------------------------------------------------------------------- /libs/actor/test/test_link.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class link_ut 13 | { 14 | public: 15 | static void run() 16 | { 17 | std::cout << "link_ut begin." << std::endl; 18 | test_common(); 19 | std::cout << "link_ut end." << std::endl; 20 | } 21 | 22 | static void my_actor_child(actor& self) 23 | { 24 | aid_t aid = recv(self, 3); 25 | } 26 | 27 | static void my_actor(actor& self) 28 | { 29 | detail::cache_pool* cac_pool = self.get_cache_pool(); 30 | std::size_t size = 10; 31 | std::vector res_list(size); 32 | for (std::size_t i=0; iget_context().get_attributes().id_, 46 | cac_pool->get_context().get_timestamp() 47 | ); 48 | sid_t sid = aid.sid_; 49 | sid += 100000; 50 | ++sid; 51 | 52 | message msg; 53 | 54 | self.link(aid_t(ctxid_nil, aid.timestamp_, a, sid)); 55 | self.recv(msg); 56 | BOOST_ASSERT(msg.get_type() == exit); 57 | 58 | response_t res = request(self, aid_t(ctxid_nil, aid.timestamp_, a, sid)); 59 | self.recv(res, msg); 60 | BOOST_ASSERT(msg.get_type() == exit); 61 | 62 | res_list[i] = request(self, aid, 3); 63 | } 64 | 65 | recv(self, exit); 66 | } 67 | 68 | static void my_thr(context& ctx) 69 | { 70 | actor a = spawn(ctx); 71 | for (std::size_t i=0; i<100; ++i) 72 | { 73 | spawn( 74 | a, 75 | boost::bind(&link_ut::my_actor, _1), 76 | monitored 77 | ); 78 | } 79 | 80 | for (std::size_t i=0; i<100; ++i) 81 | { 82 | recv(a); 83 | } 84 | } 85 | 86 | static void my_root(actor& self) 87 | { 88 | for (std::size_t i=0; i<100; ++i) 89 | { 90 | spawn( 91 | self, 92 | boost::bind(&link_ut::my_actor, _1), 93 | monitored 94 | ); 95 | } 96 | 97 | for (std::size_t i=0; i<100; ++i) 98 | { 99 | recv(self); 100 | } 101 | } 102 | 103 | static void test_common() 104 | { 105 | try 106 | { 107 | std::size_t user_thr_num = 5; 108 | //std::size_t my_actor_size = 21; 109 | attributes attrs; 110 | context ctx(attrs); 111 | actor base = spawn(ctx); 112 | 113 | boost::thread_group thrs; 114 | for (std::size_t i=0; i& self) 24 | { 25 | aid_t aid = recv(self); 26 | send(self, aid, atom("ret")); 27 | } 28 | 29 | static void my_actor(actor& self, aid_t base_id) 30 | { 31 | //try 32 | { 33 | std::size_t size = 50; 34 | std::vector res_list(size); 35 | for (std::size_t i=0; iget_context().get_io_service()); 51 | yield_t yield = self.get_yield(); 52 | tmr.expires_from_now(boost::chrono::milliseconds(1)); 53 | tmr.async_wait(yield); 54 | 55 | for (std::size_t i=0; i a = spawn(ctx); 80 | for (std::size_t i=0; i<2; ++i) 81 | { 82 | spawn(a, boost::bind(&response_ut::my_actor, _1, base_id)); 83 | } 84 | } 85 | 86 | static void test_common() 87 | { 88 | try 89 | { 90 | std::size_t free_actor_num = 20; 91 | std::size_t user_thr_num = 0; 92 | std::size_t my_actor_size = free_actor_num + user_thr_num * 2; 93 | attributes attrs; 94 | context ctx(attrs); 95 | actor base = spawn(ctx); 96 | 97 | aid_t base_id = base.get_aid(); 98 | for (std::size_t i=0; i 14 | #include 15 | #include 16 | 17 | struct msg_header 18 | { 19 | boost::uint32_t size_; 20 | gce::match_t type_; 21 | boost::uint32_t tag_offset_; 22 | }; 23 | GCE_PACK(msg_header, (size_&sfix)(type_&sfix)(tag_offset_&sfix)); 24 | 25 | #define MSG_HEADER_SIZE sizeof(boost::uint32_t) + sizeof(gce::match_t) + sizeof(boost::uint32_t) 26 | 27 | template 28 | class basic_socket 29 | { 30 | public: 31 | typedef Socket socket_t; 32 | 33 | public: 34 | explicit basic_socket(gce::io_service_t& ios) 35 | : sock_(ios) 36 | #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) 37 | , max_batch_size_(64) /// no winnt below 38 | #else 39 | , max_batch_size_((std::min)(64, IOV_MAX)) 40 | #endif 41 | { 42 | } 43 | 44 | ~basic_socket() 45 | { 46 | close(); 47 | } 48 | 49 | public: 50 | socket_t& get_socket() 51 | { 52 | return sock_; 53 | } 54 | 55 | std::size_t get_max_batch_size() const 56 | { 57 | return max_batch_size_; 58 | } 59 | 60 | gce::message recv(gce::yield_t yield) 61 | { 62 | BOOST_STATIC_ASSERT((MaxMsgSize > MSG_HEADER_SIZE)); 63 | 64 | gce::byte_t buf[MaxMsgSize]; 65 | std::size_t header_size = MSG_HEADER_SIZE; 66 | boost::asio::async_read( 67 | sock_, 68 | boost::asio::buffer(buf, header_size), 69 | yield 70 | ); 71 | 72 | if (yield.ec_ && *yield.ec_) 73 | { 74 | return gce::message(); 75 | } 76 | 77 | msg_header hdr; 78 | boost::amsg::zero_copy_buffer zbuf(buf, MaxMsgSize); 79 | boost::amsg::read(zbuf, hdr); 80 | if (zbuf.bad()) 81 | { 82 | throw std::runtime_error("message header parse error"); 83 | } 84 | 85 | std::size_t size = hdr.size_; 86 | std::size_t max_size = MaxMsgSize; 87 | if (size > max_size) 88 | { 89 | std::runtime_error("message out of length"); 90 | } 91 | 92 | if (size > 0) 93 | { 94 | boost::asio::async_read( 95 | sock_, 96 | boost::asio::buffer(buf, size), 97 | yield 98 | ); 99 | } 100 | return gce::message(hdr.type_, buf, hdr.size_, hdr.tag_offset_); 101 | } 102 | 103 | void send(gce::message const& msg, gce::yield_t yield) 104 | { 105 | msg_header hdr; 106 | hdr.size_ = msg.size(); 107 | hdr.type_ = msg.get_type(); 108 | hdr.tag_offset_ = msg.get_tag_offset(); 109 | gce::byte_t buf[sizeof(msg_header)]; 110 | boost::amsg::zero_copy_buffer zbuf(buf, sizeof(msg_header)); 111 | boost::amsg::write(zbuf, hdr); 112 | 113 | boost::array bufs; 114 | bufs[0] = boost::asio::buffer(buf, zbuf.write_length()); 115 | bufs[1] = boost::asio::buffer(msg.data(), msg.size()); 116 | 117 | boost::asio::async_write(sock_, bufs, yield); 118 | } 119 | 120 | void close() 121 | { 122 | gce::errcode_t ignore_ec; 123 | sock_.close(ignore_ec); 124 | } 125 | 126 | private: 127 | socket_t sock_; 128 | std::size_t const max_batch_size_; 129 | }; 130 | 131 | typedef basic_socket tcp_socket; 132 | 133 | #endif /// GCE_ACTOR_EXAMPLE_CLUSTER_SOCKET_HPP 134 | -------------------------------------------------------------------------------- /libs/actor/test/test_remote_relay.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class remote_relay_ut 13 | { 14 | public: 15 | static void run() 16 | { 17 | std::cout << "remote_relay_ut begin." << std::endl; 18 | test_common(); 19 | std::cout << "remote_relay_ut end." << std::endl; 20 | } 21 | 22 | private: 23 | static void my_actor(actor& self) 24 | { 25 | message msg; 26 | aid_t last_id; 27 | recv(self, atom("init"), last_id); 28 | 29 | aid_t sender = self.recv(msg); 30 | if (last_id) 31 | { 32 | self.relay(last_id, msg); 33 | } 34 | else 35 | { 36 | message m(atom("hello")); 37 | self.reply(sender, m); 38 | } 39 | } 40 | 41 | static void test_common() 42 | { 43 | try 44 | { 45 | std::size_t root_num = 10; 46 | attributes attrs; 47 | attrs.id_ = atom("router"); 48 | attrs.thread_num_ = 1; 49 | context ctx(attrs); 50 | actor base = spawn(ctx); 51 | 52 | gce::bind(base, "tcp://127.0.0.1:14924", true); 53 | 54 | boost::thread_group thrs; 55 | for (std::size_t i=0; i root_list(root_num); 66 | for (std::size_t i=0; i mix 86 | ) 87 | { 88 | try 89 | { 90 | attributes attrs; 91 | attrs.id_ = id; 92 | attrs.thread_num_ = 1; 93 | context ctx(attrs); 94 | actor base = spawn(ctx); 95 | 96 | net_option opt; 97 | opt.reconn_period_ = seconds_t(1); 98 | remote_func_list_t func_list; 99 | func_list.push_back( 100 | std::make_pair( 101 | atom("my_actor"), 102 | make_actor_func( 103 | boost::bind(&remote_relay_ut::my_actor, _1) 104 | ) 105 | ) 106 | ); 107 | connect(base, atom("router"), "tcp://127.0.0.1:14924", true, opt, func_list); 108 | wait(base, boost::chrono::milliseconds(1000)); 109 | 110 | aid_t last_id; 111 | aid_t first_id; 112 | for (std::size_t i=0; i 11 | #include 12 | #include 13 | 14 | namespace gce 15 | { 16 | class object_pool_ut 17 | { 18 | struct my_base 19 | { 20 | my_base(std::size_t index, std::string const& name) 21 | : index_(index) 22 | , name_(name) 23 | { 24 | } 25 | 26 | virtual ~my_base() 27 | { 28 | } 29 | 30 | void clear() 31 | { 32 | index_ = 0; 33 | name_.clear(); 34 | } 35 | 36 | std::size_t index_; 37 | std::string name_; 38 | }; 39 | 40 | struct my_data 41 | : public detail::object_pool::object 42 | , public my_base 43 | { 44 | explicit my_data(std::string const& str) 45 | : my_base(0, str) 46 | , str_(str) 47 | , i_(0) 48 | , l_(0) 49 | { 50 | } 51 | 52 | ~my_data() 53 | { 54 | } 55 | 56 | void on_free() 57 | { 58 | my_base::clear(); 59 | str_.clear(); 60 | i_ = 0; 61 | l_ = 0; 62 | } 63 | 64 | void print() 65 | { 66 | //std::cout << "my_base[ index_: " << index_ << ", name_: " << name_ << " ]" << std::endl; 67 | //std::cout << "basic_object[ ]" << std::endl; 68 | //std::cout << "my_data[ str_: " << str_ << ", i_: " << i_ << ", l_: " << l_ << std::endl; 69 | } 70 | 71 | std::string str_; 72 | int i_; 73 | long l_; 74 | }; 75 | 76 | public: 77 | static void run() 78 | { 79 | std::cout << "object_pool_ut begin." << std::endl; 80 | test_freelist(); 81 | std::cout << "object_pool_ut end." << std::endl; 82 | } 83 | 84 | private: 85 | static void test_freelist() 86 | { 87 | typedef detail::object_pool op_t; 88 | detail::unique_ptr op(new op_t((detail::cache_pool*)0, std::string(), size_nil)); 89 | 90 | //boost::timer::auto_cpu_timer t; 91 | run_test(op.get(), true); 92 | } 93 | 94 | template 95 | static void run_test(Op* op, bool st) 96 | { 97 | for (std::size_t i=0; i<10; ++i) 98 | { 99 | { 100 | my_data* md = op->get(); 101 | md->str_ = "md"; 102 | md->index_ = 1; 103 | //md->print(); 104 | op->free(md); 105 | if (st) 106 | { 107 | BOOST_ASSERT(md->str_.empty()); 108 | BOOST_ASSERT(md->name_.empty()); 109 | BOOST_ASSERT(md->index_ == 0); 110 | } 111 | } 112 | 113 | for (std::size_t j=0; j<100; ++j) 114 | { 115 | std::vector my_data_list(1000, (my_data*)0); 116 | for (std::size_t i=0; iget(); 119 | md->str_ = "md_list"; 120 | md->index_ = i; 121 | BOOST_ASSERT(md->str_ == "md_list"); 122 | BOOST_ASSERT(md->name_.empty()); 123 | BOOST_ASSERT(md->index_ == i); 124 | //md->print(); 125 | my_data_list[i] = md; 126 | } 127 | 128 | for (std::size_t i=0; iprint(); 132 | op->free(md); 133 | BOOST_ASSERT(md->str_.empty()); 134 | BOOST_ASSERT(md->name_.empty()); 135 | BOOST_ASSERT(md->index_ == 0); 136 | } 137 | my_data_list.clear(); 138 | } 139 | } 140 | } 141 | }; 142 | } 143 | 144 | -------------------------------------------------------------------------------- /gce/detail/ref_count.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_DETAIL_REF_COUNT_HPP 11 | #define GCE_DETAIL_REF_COUNT_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | namespace gce 19 | { 20 | namespace detail 21 | { 22 | class ref_count 23 | { 24 | ref_count(); 25 | public: 26 | template 27 | explicit ref_count(D d) : deleter_(d), count_(0) {} 28 | 29 | template 30 | ref_count(D d, A a) : deleter_(d, a), count_(0) {} 31 | 32 | virtual ~ref_count() {} 33 | 34 | public: 35 | long use_count() const 36 | { 37 | return count_; 38 | } 39 | 40 | #if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) 41 | inline friend void intrusive_ptr_add_ref(ref_count* p) 42 | { 43 | p->count_.fetch_add(1, boost::memory_order_relaxed); 44 | } 45 | 46 | inline friend void intrusive_ptr_release(ref_count* p) 47 | { 48 | if (p->count_.fetch_sub(1, boost::memory_order_release) == 1) 49 | { 50 | boost::atomic_thread_fence(boost::memory_order_acquire); 51 | BOOST_ASSERT(p->deleter_); 52 | p->deleter_(); 53 | } 54 | } 55 | #endif 56 | 57 | void add_ref() 58 | { 59 | count_.fetch_add(1, boost::memory_order_relaxed); 60 | } 61 | 62 | void release() 63 | { 64 | if (count_.fetch_sub(1, boost::memory_order_release) == 1) 65 | { 66 | boost::atomic_thread_fence(boost::memory_order_acquire); 67 | BOOST_ASSERT(deleter_); 68 | deleter_(); 69 | } 70 | } 71 | 72 | private: 73 | boost::function deleter_; 74 | boost::atomic_long count_; 75 | }; 76 | } 77 | } 78 | 79 | #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) 80 | 81 | namespace boost 82 | { 83 | inline void intrusive_ptr_add_ref(gce::detail::ref_count* p) 84 | { 85 | p->add_ref(); 86 | } 87 | 88 | inline void intrusive_ptr_release(gce::detail::ref_count* p) 89 | { 90 | p->release(); 91 | } 92 | } // namespace boost 93 | 94 | #endif 95 | 96 | namespace gce 97 | { 98 | namespace detail 99 | { 100 | /// single-thread ref count 101 | class ref_count_st 102 | { 103 | ref_count_st(); 104 | public: 105 | template 106 | explicit ref_count_st(D d) : deleter_(d), count_(0) {} 107 | 108 | template 109 | ref_count_st(D d, A a) : deleter_(d, a), count_(0) {} 110 | 111 | virtual ~ref_count_st() {} 112 | 113 | public: 114 | long use_count() const 115 | { 116 | return count_; 117 | } 118 | 119 | #if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) 120 | inline friend void intrusive_ptr_add_ref(ref_count_st* p) 121 | { 122 | ++p->count_; 123 | } 124 | 125 | inline friend void intrusive_ptr_release(ref_count_st* p) 126 | { 127 | if (--p->count_ == 0) 128 | { 129 | BOOST_ASSERT(p->deleter_); 130 | p->deleter_(); 131 | } 132 | } 133 | #endif 134 | 135 | void add_ref() 136 | { 137 | ++count_; 138 | } 139 | 140 | void release() 141 | { 142 | if (--count_ == 0) 143 | { 144 | BOOST_ASSERT(deleter_); 145 | deleter_(); 146 | } 147 | } 148 | 149 | private: 150 | boost::function deleter_; 151 | long count_; 152 | }; 153 | } 154 | } 155 | 156 | #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) 157 | 158 | namespace boost 159 | { 160 | inline void intrusive_ptr_add_ref(gce::detail::ref_count_st* p) 161 | { 162 | p->add_ref(); 163 | } 164 | 165 | inline void intrusive_ptr_release(gce::detail::ref_count_st* p) 166 | { 167 | p->release(); 168 | } 169 | } // namespace boost 170 | 171 | #endif 172 | 173 | #endif /// GCE_DETAIL_REF_COUNT_HPP 174 | -------------------------------------------------------------------------------- /libs/actor/example/cluster/game.cpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #include "game.hpp" 11 | #include "user.hpp" 12 | #include 13 | #include 14 | 15 | ///---------------------------------------------------------------------------- 16 | gce::aid_t game::start(gce::actor& sire, gce::match_t svc_name, app_ctxid_list_t game_list) 17 | { 18 | return 19 | gce::spawn( 20 | sire, 21 | boost::bind( 22 | &game::run, _1, svc_name, game_list 23 | ), 24 | gce::monitored 25 | ); 26 | } 27 | ///---------------------------------------------------------------------------- 28 | void game::run(gce::actor& self, gce::match_t svc_name, app_ctxid_list_t game_list) 29 | { 30 | try 31 | { 32 | typedef std::map user_list_t; 33 | user_list_t user_list; 34 | 35 | gce::register_service(self, svc_name); 36 | 37 | std::printf("game %s setup\n", gce::atom(svc_name).c_str()); 38 | 39 | /// loop handle messages 40 | bool running = true; 41 | while (running) 42 | { 43 | gce::message msg; 44 | gce::aid_t sender = self.recv(msg); 45 | gce::match_t type = msg.get_type(); 46 | if (type == gce::exit) 47 | { 48 | running = false; 49 | } 50 | else if (type == gce::atom("stop")) 51 | { 52 | running = false; 53 | gce::reply(self, sender, gce::atom("ret")); 54 | } 55 | else if (type == gce::atom("cln_login")) 56 | { 57 | std::string username, passwd; 58 | cid_t cid; 59 | msg >> username >> passwd >> cid; 60 | 61 | std::printf("new client login, username: %s\n", username.c_str()); 62 | 63 | gce::aid_t old_usr_aid; 64 | std::pair pr = 65 | user_list.insert(std::make_pair(username, gce::aid_t())); 66 | if (!pr.second) 67 | { 68 | old_usr_aid = pr.first->second; 69 | } 70 | 71 | pr.first->second = 72 | gce::spawn( 73 | self, 74 | boost::bind( 75 | &user::run, _1, game_list, old_usr_aid, 76 | self.get_aid(), cid, username, passwd 77 | ) 78 | ); 79 | self.relay(pr.first->second, msg); 80 | } 81 | else if (type == gce::atom("chat")) 82 | { 83 | /// broadcast chat msg 84 | BOOST_FOREACH(user_list_t::value_type& pr, user_list) 85 | { 86 | self.send(pr.second, msg); 87 | } 88 | } 89 | else if (type == gce::atom("chat_to")) 90 | { 91 | /// find target and send chat msg 92 | std::string username; 93 | msg >> username; 94 | user_list_t::iterator itr(user_list.find(username)); 95 | if (itr != user_list.end()) 96 | { 97 | self.send(itr->second, msg); 98 | } 99 | } 100 | else if (type == gce::atom("rmv_user")) 101 | { 102 | std::string username; 103 | msg >> username; 104 | user_list_t::iterator itr(user_list.find(username)); 105 | if (itr != user_list.end() && itr->second == sender) 106 | { 107 | user_list.erase(itr); 108 | } 109 | } 110 | else 111 | { 112 | std::string errmsg("game unexpected message, type: "); 113 | errmsg += gce::atom(type); 114 | std::printf("%s\n", errmsg.c_str()); 115 | } 116 | } 117 | 118 | std::printf("game %s quit\n", gce::atom(svc_name).c_str()); 119 | } 120 | catch (std::exception& ex) 121 | { 122 | std::printf("game except: %s\n", ex.what()); 123 | } 124 | gce::deregister_service(self, svc_name); 125 | } 126 | ///---------------------------------------------------------------------------- 127 | -------------------------------------------------------------------------------- /gce/actor/context.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_CONTEXT_HPP 11 | #define GCE_ACTOR_CONTEXT_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | namespace gce 24 | { 25 | typedef std::size_t thrid_t; 26 | typedef boost::function thread_callback_t; 27 | struct attributes 28 | { 29 | attributes() 30 | : ios_(0) 31 | , id_(ctxid_nil) 32 | , thread_num_(boost::thread::hardware_concurrency()) 33 | , per_thread_cache_pool_num_(1) 34 | , slice_num_(1) 35 | , actor_pool_reserve_size_(8) 36 | , socket_pool_reserve_size_(8) 37 | , acceptor_pool_reserve_size_(8) 38 | , max_cache_match_size_(32) 39 | { 40 | } 41 | 42 | io_service_t* ios_; 43 | ctxid_t id_; 44 | std::size_t thread_num_; 45 | std::size_t per_thread_cache_pool_num_; 46 | std::size_t slice_num_; 47 | std::size_t actor_pool_reserve_size_; 48 | std::size_t socket_pool_reserve_size_; 49 | std::size_t acceptor_pool_reserve_size_; 50 | std::size_t max_cache_match_size_; 51 | std::vector thread_begin_cb_list_; 52 | std::vector thread_end_cb_list_; 53 | }; 54 | 55 | namespace detail 56 | { 57 | class cache_pool; 58 | } 59 | 60 | class nonblocking_actor; 61 | class thread_mapped_actor; 62 | class context 63 | { 64 | public: 65 | explicit context(attributes attrs = attributes()); 66 | ~context(); 67 | 68 | public: 69 | inline io_service_t& get_io_service() 70 | { 71 | BOOST_ASSERT(ios_); 72 | return *ios_; 73 | } 74 | 75 | public: 76 | /// internal use 77 | inline attributes const& get_attributes() const { return attrs_; } 78 | inline timestamp_t get_timestamp() const { return timestamp_; } 79 | inline std::size_t get_cache_queue_size() const { return cache_queue_size_; } 80 | 81 | thread_mapped_actor& make_thread_mapped_actor(); 82 | detail::cache_pool* select_cache_pool(); 83 | nonblocking_actor& make_nonblocking_actor(); 84 | 85 | void register_service(match_t name, aid_t svc, std::size_t cache_queue_index); 86 | void deregister_service(match_t name, aid_t svc, std::size_t cache_queue_index); 87 | 88 | void register_socket(ctxid_pair_t, aid_t skt, std::size_t cache_queue_index); 89 | void deregister_socket(ctxid_pair_t ctxid_pr, aid_t skt, std::size_t cache_queue_index); 90 | 91 | private: 92 | void run( 93 | thrid_t, 94 | std::vector const&, 95 | std::vector const& 96 | ); 97 | void stop(); 98 | 99 | private: 100 | /// Ensure start from a new cache line. 101 | byte_t pad0_[GCE_CACHE_LINE_SIZE]; 102 | 103 | GCE_CACHE_ALIGNED_VAR(attributes, attrs_) 104 | GCE_CACHE_ALIGNED_VAR(timestamp_t const, timestamp_) 105 | 106 | /// select cache pool 107 | GCE_CACHE_ALIGNED_VAR(std::size_t, curr_cache_pool_) 108 | GCE_CACHE_ALIGNED_VAR(std::size_t, cache_pool_size_) 109 | GCE_CACHE_ALIGNED_VAR(std::size_t, cache_queue_size_) 110 | 111 | GCE_CACHE_ALIGNED_VAR(detail::unique_ptr, ios_) 112 | GCE_CACHE_ALIGNED_VAR(boost::optional, work_) 113 | 114 | GCE_CACHE_ALIGNED_VAR(boost::thread_group, thread_group_) 115 | GCE_CACHE_ALIGNED_VAR(std::vector, cache_pool_list_) 116 | 117 | GCE_CACHE_ALIGNED_VAR(std::vector, nonblocking_actor_list_) 118 | GCE_CACHE_ALIGNED_VAR(boost::atomic_size_t, curr_nonblocking_actor_) 119 | 120 | GCE_CACHE_ALIGNED_VAR(boost::lockfree::queue, thread_mapped_actor_list_) 121 | }; 122 | } 123 | 124 | #endif /// GCE_ACTOR_CONTEXT_HPP 125 | -------------------------------------------------------------------------------- /gce/actor/detail/cache_pool.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_CACHE_POOL_HPP 11 | #define GCE_ACTOR_DETAIL_CACHE_POOL_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | namespace gce 25 | { 26 | class context; 27 | class coroutine_stackful_actor; 28 | class coroutine_stackless_actor; 29 | struct attributes; 30 | 31 | namespace detail 32 | { 33 | class socket; 34 | class acceptor; 35 | class cache_pool; 36 | typedef object_pool context_switching_actor_pool_t; 37 | typedef object_pool event_based_actor_pool_t; 38 | typedef object_pool socket_pool_t; 39 | typedef object_pool acceptor_pool_t; 40 | 41 | class cache_pool 42 | : private boost::noncopyable 43 | { 44 | public: 45 | cache_pool(context& ctx, std::size_t index, bool is_slice = false); 46 | ~cache_pool(); 47 | 48 | public: 49 | inline context& get_context() { return *ctx_; } 50 | inline std::size_t get_index() { return index_; } 51 | inline strand_t& get_strand() { return snd_; } 52 | 53 | coroutine_stackful_actor* get_context_switching_actor(); 54 | coroutine_stackless_actor* get_event_based_actor(); 55 | socket* get_socket(); 56 | acceptor* get_acceptor(); 57 | 58 | void free_actor(coroutine_stackful_actor*); 59 | void free_actor(coroutine_stackless_actor*); 60 | void free_socket(socket*); 61 | void free_acceptor(acceptor*); 62 | 63 | void register_service(match_t name, aid_t svc); 64 | aid_t find_service(match_t name); 65 | void deregister_service(match_t name, aid_t svc); 66 | 67 | void register_socket(ctxid_pair_t, aid_t skt); 68 | aid_t select_socket(ctxid_t ctxid = ctxid_nil, ctxid_t* target = 0); 69 | aid_t select_straight_socket(ctxid_t ctxid = ctxid_nil, ctxid_t* target = 0); 70 | aid_t select_joint_socket(ctxid_t ctxid = ctxid_nil, ctxid_t* target = 0); 71 | aid_t select_router(ctxid_t* target = 0); 72 | void deregister_socket(ctxid_pair_t, aid_t skt); 73 | 74 | void add_socket(socket*); 75 | void add_acceptor(acceptor*); 76 | void remove_socket(socket*); 77 | void remove_acceptor(acceptor*); 78 | 79 | void stop(); 80 | inline bool stopped() const { return stopped_; } 81 | 82 | private: 83 | /// Ensure start from a new cache line. 84 | byte_t pad0_[GCE_CACHE_LINE_SIZE]; 85 | 86 | GCE_CACHE_ALIGNED_VAR(context*, ctx_) 87 | GCE_CACHE_ALIGNED_VAR(std::size_t, index_) 88 | GCE_CACHE_ALIGNED_VAR(strand_t, snd_) 89 | 90 | /// pools 91 | GCE_CACHE_ALIGNED_VAR(boost::optional, context_switching_actor_pool_) 92 | GCE_CACHE_ALIGNED_VAR(boost::optional, event_based_actor_pool_) 93 | GCE_CACHE_ALIGNED_VAR(boost::optional, socket_pool_) 94 | GCE_CACHE_ALIGNED_VAR(boost::optional, acceptor_pool_) 95 | 96 | /// thread local vals 97 | typedef std::set skt_list_t; 98 | struct socket_list 99 | { 100 | skt_list_t skt_list_; 101 | skt_list_t::iterator curr_skt_; 102 | }; 103 | 104 | typedef std::map conn_list_t; 105 | conn_list_t conn_list_; 106 | conn_list_t joint_list_; 107 | conn_list_t router_list_; 108 | conn_list_t::iterator curr_router_list_; 109 | conn_list_t::iterator curr_socket_list_; 110 | conn_list_t::iterator curr_joint_list_; 111 | socket_list dummy_; 112 | 113 | typedef std::map service_list_t; 114 | service_list_t service_list_; 115 | 116 | std::set socket_list_; 117 | std::set acceptor_list_; 118 | 119 | bool stopped_; 120 | }; 121 | } 122 | } 123 | 124 | #endif /// GCE_ACTOR_DETAIL_CACHE_POOL_HPP 125 | -------------------------------------------------------------------------------- /gce/actor/detail/socket.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_ACTOR_DETAIL_SOCKET_HPP 11 | #define GCE_ACTOR_DETAIL_SOCKET_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | namespace gce 28 | { 29 | class thread_mapped_actor; 30 | namespace detail 31 | { 32 | class cache_pool; 33 | 34 | class socket 35 | : public object_pool::object 36 | , public basic_actor 37 | { 38 | typedef basic_actor base_type; 39 | 40 | enum status 41 | { 42 | ready = 0, 43 | on, 44 | off, 45 | }; 46 | 47 | public: 48 | explicit socket(cache_pool*); 49 | ~socket(); 50 | 51 | public: 52 | void init(net_option); 53 | void connect( 54 | aid_t sire, remote_func_list_t const&, ctxid_t target, 55 | std::string const&, bool target_is_router 56 | ); 57 | 58 | inline void send(aid_t recver, message const& msg) 59 | { 60 | base_type::pri_send(recver, msg); 61 | } 62 | 63 | public: 64 | void start(std::map const&, socket_ptr, bool is_router); 65 | void stop(); 66 | void on_free(); 67 | void on_recv(pack&, base_type::send_hint); 68 | 69 | void link(aid_t) {} 70 | void monitor(aid_t) {} 71 | 72 | private: 73 | void handle_net_msg(message&); 74 | void spawn_remote_actor(cache_pool*, spawn_t, remote_func); 75 | void end_spawn_remote_actor(spawn_t, aid_t aid); 76 | void send_spawn_ret(spawn_t*, pack&, spawn_error, aid_t aid, bool is_err_ret); 77 | void send(message const&); 78 | void send_msg(message const&); 79 | void send_msg_hb(); 80 | 81 | void run_conn(aid_t sire, ctxid_pair_t, std::string const&, yield_t); 82 | void run(socket_ptr, yield_t); 83 | 84 | socket_ptr make_socket(std::string const&); 85 | void handle_recv(pack&); 86 | void add_straight_link(aid_t src, aid_t des); 87 | void remove_straight_link(aid_t src, aid_t des); 88 | void add_router_link(aid_t src, aid_t des, sktaid_t skt); 89 | sktaid_t remove_router_link(aid_t src, aid_t des); 90 | 91 | void on_neterr(aid_t self_aid, errcode_t ec = errcode_t()); 92 | ctxid_pair_t sync_ctxid(ctxid_pair_t new_pr, ctxid_pair_t curr_pr); 93 | 94 | private: 95 | bool parse_message(message&); 96 | void connect(yield_t, bool init = false); 97 | errcode_t recv(message&, yield_t); 98 | void close(); 99 | void reconn(); 100 | template 101 | void start_heartbeat(F); 102 | void free_self( 103 | ctxid_pair_t, exit_code_t, 104 | std::string const&, yield_t 105 | ); 106 | 107 | private: 108 | /// Ensure start from a new cache line. 109 | byte_t pad0_[GCE_CACHE_LINE_SIZE]; 110 | 111 | GCE_CACHE_ALIGNED_VAR(status, stat_) 112 | GCE_CACHE_ALIGNED_VAR(net_option, opt_) 113 | 114 | /// thread local vals 115 | socket_ptr skt_; 116 | heartbeat hb_; 117 | timer_t sync_; 118 | std::size_t tmr_sid_; 119 | 120 | byte_t recv_buffer_[GCE_SOCKET_RECV_CACHE_SIZE]; 121 | detail::buffer_ref recv_cache_; 122 | 123 | bool conn_; 124 | std::deque conn_cache_; 125 | std::size_t curr_reconn_; 126 | 127 | /// remote links 128 | typedef std::map > straight_link_list_t; 129 | straight_link_list_t straight_link_list_; 130 | std::set straight_dummy_; 131 | 132 | typedef std::map > router_link_list_t; 133 | router_link_list_t router_link_list_; 134 | std::map router_dummy_; 135 | 136 | /// remote spawn's funcs 137 | typedef std::map remote_list_t; 138 | remote_list_t remote_list_; 139 | 140 | bool is_router_; 141 | }; 142 | } 143 | } 144 | 145 | #endif /// GCE_ACTOR_DETAIL_SOCKET_HPP 146 | -------------------------------------------------------------------------------- /gce/detail/freelist.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | #ifndef GCE_DETAIL_FREELIST_HPP 11 | #define GCE_DETAIL_FREELIST_HPP 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | namespace gce 19 | { 20 | namespace detail 21 | { 22 | template 23 | class freelist 24 | { 25 | public: 26 | typedef T value_type; 27 | typedef value_type* pointer; 28 | 29 | struct null_args {}; 30 | struct args_tag {}; 31 | 32 | typedef typename boost::mpl::if_< 33 | boost::is_same, 34 | null_args, Args 35 | >::type args_t; 36 | 37 | typedef Alloc allocator_t; 38 | 39 | private: 40 | typedef typename boost::mpl::if_< 41 | boost::is_same, 42 | null_args, args_tag 43 | >::type args_select_t; 44 | 45 | public: 46 | freelist( 47 | args_t args, 48 | std::size_t cache_size, 49 | std::size_t reserve_size, 50 | allocator_t a 51 | ) 52 | : cache_size_(cache_size) 53 | , size_(0) 54 | , args_(args) 55 | , head_(0) 56 | , a_(a) 57 | { 58 | reserve(reserve_size); 59 | } 60 | 61 | freelist( 62 | std::size_t cache_size, 63 | std::size_t reserve_size, 64 | allocator_t a 65 | ) 66 | : cache_size_(cache_size) 67 | , size_(0) 68 | , args_(null_args()) 69 | , head_(0) 70 | , a_(a) 71 | { 72 | reserve(reserve_size); 73 | } 74 | 75 | ~freelist() 76 | { 77 | clear_pool(); 78 | } 79 | 80 | public: 81 | pointer get() 82 | { 83 | pointer obj = 0; 84 | if (head_) 85 | { 86 | obj = head_; 87 | pointer next = object_access::get_next(head_); 88 | head_ = next; 89 | object_access::set_next(obj, pointer(0)); 90 | --size_; 91 | } 92 | else 93 | { 94 | obj = make_object(args_select_); 95 | } 96 | return obj; 97 | } 98 | 99 | void free(pointer obj) 100 | { 101 | pointer curr = obj; 102 | while (curr) 103 | { 104 | pointer next = object_access::get_next(curr); 105 | curr->on_free(); 106 | if (size_ < cache_size_) 107 | { 108 | object_access::set_next(curr, head_); 109 | head_ = curr; 110 | ++size_; 111 | } 112 | else 113 | { 114 | curr->~value_type(); 115 | a_.deallocate(curr, 1); 116 | } 117 | 118 | curr = next; 119 | } 120 | } 121 | 122 | private: 123 | inline pointer make_object(null_args) 124 | { 125 | return new (a_.allocate(1)) value_type; 126 | } 127 | 128 | inline pointer make_object(args_tag) 129 | { 130 | return new (a_.allocate(1)) value_type(args_); 131 | } 132 | 133 | void reserve(std::size_t size) 134 | { 135 | pointer curr = head_; 136 | try 137 | { 138 | for (std::size_t i=0; i~value_type(); 166 | a_.deallocate(curr, 1); 167 | curr = next; 168 | } 169 | head_ = 0; 170 | size_ = 0; 171 | } 172 | 173 | private: 174 | std::size_t const cache_size_; 175 | std::size_t size_; 176 | 177 | args_t args_; 178 | args_select_t args_select_; 179 | pointer head_; 180 | allocator_t a_; 181 | }; 182 | } /// namespace detail 183 | } /// namespace gce 184 | 185 | #endif /// GCE_DETAIL_FREELIST_HPP 186 | -------------------------------------------------------------------------------- /libs/actor/test/test_stackless.hpp: -------------------------------------------------------------------------------- 1 | /// 2 | /// Copyright (c) 2009-2014 Nous Xiong (348944179 at qq dot com) 3 | /// 4 | /// Distributed under the Boost Software License, Version 1.0. (See accompanying 5 | /// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 | /// 7 | /// See https://github.com/nousxiong/gce for latest version. 8 | /// 9 | 10 | namespace gce 11 | { 12 | class stackless_ut 13 | { 14 | public: 15 | static void run() 16 | { 17 | std::cout << "stackless_ut begin." << std::endl; 18 | test_common(); 19 | std::cout << "stackless_ut end." << std::endl; 20 | } 21 | 22 | public: 23 | class my_child 24 | : public boost::enable_shared_from_this 25 | { 26 | public: 27 | void run(actor& self) 28 | { 29 | GCE_REENTER (self) 30 | { 31 | GCE_YIELD recv(self, aid_); 32 | reply(self, aid_); 33 | } 34 | } 35 | 36 | private: 37 | aid_t aid_; 38 | message msg_; 39 | }; 40 | 41 | class my_actor 42 | : public boost::enable_shared_from_this 43 | { 44 | public: 45 | my_actor(io_service_t& ios, aid_t base_id) 46 | : size_(50) 47 | , res_list_(size_) 48 | , base_id_(base_id) 49 | , tmr_(ios) 50 | { 51 | } 52 | 53 | ~my_actor() 54 | { 55 | } 56 | 57 | public: 58 | void run(actor& self) 59 | { 60 | GCE_REENTER (self) 61 | { 62 | for (i_=0; i_(), _1 69 | ), 70 | aid_ 71 | ); 72 | res_list_[i_] = request(self, aid_); 73 | } 74 | 75 | GCE_YIELD wait(self, boost::chrono::milliseconds(1)); 76 | 77 | for (i_=0; i_ res_list_; 96 | 97 | std::size_t i_; 98 | aid_t base_id_; 99 | aid_t aid_; 100 | message msg_; 101 | 102 | timer_t tmr_; 103 | errcode_t ec_; 104 | }; 105 | 106 | static void my_thr(context& ctx, aid_t base_id) 107 | { 108 | actor a = spawn(ctx); 109 | io_service_t& ios = ctx.get_io_service(); 110 | for (std::size_t i=0; i<2; ++i) 111 | { 112 | spawn( 113 | a, 114 | boost::bind( 115 | &my_actor::run, 116 | boost::make_shared( 117 | boost::ref(ios), base_id 118 | ), 119 | _1 120 | ) 121 | ); 122 | } 123 | } 124 | 125 | static void test_common() 126 | { 127 | try 128 | { 129 | std::size_t free_actor_num = 20; 130 | std::size_t user_thr_num = 0; 131 | std::size_t my_actor_size = free_actor_num + user_thr_num * 2; 132 | attributes attrs; 133 | context ctx(attrs); 134 | actor base = spawn(ctx); 135 | 136 | io_service_t& ios = ctx.get_io_service(); 137 | aid_t base_id = base.get_aid(); 138 | for (std::size_t i=0; i( 141 | base, 142 | boost::bind( 143 | &my_actor::run, 144 | boost::make_shared( 145 | boost::ref(ios), base_id 146 | ), 147 | _1 148 | ) 149 | ); 150 | } 151 | 152 | boost::thread_group thrs; 153 | for (std::size_t i=0; i