SpiecsEngine
 
Loading...
Searching...
No Matches
Acceptor.cpp
Go to the documentation of this file.
1/**
2* @file Acceptor.cpp.
3* @brief The Acceptor Class Implementation.
4* @author Spices & Muduo.
5*/
6
7#include "Pchheader.h"
8#include "Acceptor.h"
9#include "EventLoop.h"
10
11namespace Spices {
12
13namespace Net {
14
15 Acceptor::Acceptor(const InetAddress& listenAddress, bool reusePort)
16 : m_IsListening(false)
17 {
19
20 /**
21 * @brief Create Server Socket.
22 */
23 m_AcceptSocket.Create();
24 m_AcceptSocket.SetReuseAddress(true);
25 m_AcceptSocket.BindAddress(listenAddress);
26
27 m_AcceptChannel = std::make_shared<Channel>(m_AcceptSocket.Fd(), EventLoopThreadWrapper::GetInst());
28 m_AcceptChannel->SetReadCallback([=]() { HandleRead(); });
29 }
30
32 {
34
35 m_AcceptChannel->DisableAll();
36 m_AcceptChannel->Remove();
37 }
38
40 {
42
43 m_IsListening = true;
44 m_AcceptSocket.Listen();
45 m_AcceptChannel->EnableReading();
46 }
47
48 void Acceptor::HandleRead() const
49 {
50 InetAddress peerAddress;
51 const SOCKET connectFd = m_AcceptSocket.Accept(&peerAddress);
52
53 if (m_ConnectionCallback)
54 {
55 m_ConnectionCallback(connectFd, peerAddress);
56 }
57 else
58 {
59 ::closesocket(connectFd);
60 }
61 }
62
63}
64
65}
#define SPICES_PROFILE_ZONE
void Listen()
Listen accept socket.
Definition Acceptor.cpp:39
void HandleRead() const
On Read Event Callback.
Definition Acceptor.cpp:48
Acceptor(const InetAddress &listenAddress, bool reusePort)
Constructor Function.
Definition Acceptor.cpp:15
bool m_IsListening
Boolean of whether is in listening.
Definition Acceptor.h:102
virtual ~Acceptor()
Destructor Function.
Definition Acceptor.cpp:31
Wrapper of Channel and Socket, As the entrance of comm.
Definition Acceptor.h:24
This class is Wrapper of current socket address.
Definition InetAddress.h:22