diff --git a/src/Network/Socket.cpp b/src/Network/Socket.cpp index bb8afc06..0587c0b0 100644 --- a/src/Network/Socket.cpp +++ b/src/Network/Socket.cpp @@ -49,20 +49,17 @@ static SockException getSockErr(int sock, bool try_errno = true) { return toSockException(error); } -Socket::Ptr Socket::createSocket(const EventPoller::Ptr &poller, bool enable_mutex) { - return std::make_shared(poller, enable_mutex); +Socket::Ptr Socket::createSocket(const EventPoller::Ptr &poller_in, bool enable_mutex) { + auto poller = poller_in ? poller_in : EventPollerPool::Instance().getPoller(); + return Socket::Ptr(new Socket(poller, enable_mutex), [poller](Socket *ptr) { poller->async([ptr]() { delete ptr; }); }); } -Socket::Socket(const EventPoller::Ptr &poller, bool enable_mutex) - : _mtx_sock_fd(enable_mutex) +Socket::Socket(EventPoller::Ptr poller, bool enable_mutex) + : _poller(std::move(poller)) + , _mtx_sock_fd(enable_mutex) , _mtx_event(enable_mutex) , _mtx_send_buf_waiting(enable_mutex) , _mtx_send_buf_sending(enable_mutex) { - - _poller = poller; - if (!_poller) { - _poller = EventPollerPool::Instance().getPoller(); - } setOnRead(nullptr); setOnErr(nullptr); setOnAccept(nullptr); diff --git a/src/Network/Socket.h b/src/Network/Socket.h index 5c0c8f3a..d43549aa 100644 --- a/src/Network/Socket.h +++ b/src/Network/Socket.h @@ -300,7 +300,6 @@ class Socket : public std::enable_shared_from_this, public noncopyable, * @param enable_mutex 是否启用互斥锁(接口是否线程安全) */ static Ptr createSocket(const EventPoller::Ptr &poller = nullptr, bool enable_mutex = true); - Socket(const EventPoller::Ptr &poller = nullptr, bool enable_mutex = true); ~Socket() override; /** @@ -512,6 +511,8 @@ class Socket : public std::enable_shared_from_this, public noncopyable, std::string getIdentifier() const override; private: + Socket(EventPoller::Ptr poller, bool enable_mutex = true); + void setSock(SockNum::Ptr sock); int onAccept(const SockNum::Ptr &sock, int event) noexcept; ssize_t onRead(const SockNum::Ptr &sock, const BufferRaw::Ptr &buffer) noexcept;