// Use of this source code is governed by a BSD 3-Clause License // that can be found in the LICENSE file. // Author: caozhiyi (caozhiyi5@gmail.com) #ifndef CPPNET_SOCKET_SOCKET_INTERFACE #define CPPNET_SOCKET_SOCKET_INTERFACE #include #include #include "common/network/address.h" #ifdef __win__ #include "common/structure/thread_safe_unordered_map.h" #else #include #endif namespace cppnet { class Address; class CppNetBase; class Dispatcher; class EventActions; class Socket { public: Socket(): _sock(0) {} Socket(uint64_t sock): _sock(sock) {} virtual ~Socket() {} void SetSocket(const uint64_t& sock) { _sock = sock; } uint64_t GetSocket() { return _sock; } void SetAddress(const Address& addr) { _addr = addr; } const Address& GetAddress() const { return _addr; } void SetCppNetBase(std::shared_ptr base) { _cppnet_base = base; } const std::shared_ptr GetCppNetBase() const { return _cppnet_base.lock(); } void SetEventActions(std::weak_ptr actions) { _event_actions = actions; } const std::shared_ptr GetEventActions() const { return _event_actions.lock(); } void SetDispatcher(std::shared_ptr dis) { _dispatcher = dis; } std::shared_ptr GetDispatcher() { return _dispatcher.lock(); } protected: uint64_t _sock; Address _addr; std::weak_ptr _cppnet_base; std::weak_ptr _event_actions; std::weak_ptr _dispatcher; #ifdef __win__ static ThreadSafeUnorderedMap> __all_socket_map; #else static thread_local std::unordered_map> __all_socket_map; #endif }; } #endif