-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
481 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<ClassDiagram /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include "PollDemultiplexer.h" | ||
#include<iostream> | ||
namespace Young | ||
{ | ||
Events PollDemultiplexer::ReadEvent() | ||
{ | ||
#ifdef WINDOWS_YOUNG | ||
return POLLIN; | ||
#else | ||
return POLLIN | POLLPRI; | ||
#endif // WINDOWS_YOUNG | ||
|
||
} | ||
Events PollDemultiplexer::WriteEvent() | ||
{ | ||
return POLLOUT; | ||
} | ||
Events PollDemultiplexer::ErrorEvent() | ||
{ | ||
return POLLERR; | ||
} | ||
void PollDemultiplexer::Run(UInt32 timeout) | ||
{ | ||
UInt32 nums = poll(&*m_Pollfds.begin(), m_Pollfds.size(), timeout); | ||
std::vector<pollfd> activityFds; | ||
//把活跃的fd取出来。防止执行handler事件时删除句柄,而此时又在遍历m_Pollfds | ||
for (UInt32 i = 0; i < m_Pollfds.size() && nums > 0; i++) | ||
{ | ||
if (m_Pollfds[i].revents) | ||
{ | ||
--nums; | ||
activityFds.push_back(m_Pollfds[i]); | ||
} | ||
|
||
} | ||
for (UInt32 i = 0; i < activityFds.size(); i++) | ||
{ | ||
SocketFd sfd = activityFds[i].fd; | ||
if (activityFds[i].revents & ReadEvent()) | ||
{ | ||
m_Handlers[sfd]->HandleRead(); | ||
} | ||
if (activityFds[i].revents & WriteEvent()) | ||
{ | ||
m_Handlers[sfd]->HandleWrite(); | ||
} | ||
if (activityFds[i].revents & ErrorEvent()) | ||
{ | ||
m_Handlers[sfd]->HandleError(); | ||
} | ||
} | ||
} | ||
void PollDemultiplexer::UpdateHandler(EventHandlerPtr EventHandler, Events events) | ||
{ | ||
if (EventHandler == NULL) | ||
{ | ||
return; | ||
} | ||
SocketFd sfd = EventHandler->GetFd(); | ||
if (m_IndexBySocketFd.count(sfd) == 0) | ||
{ | ||
//没事件 | ||
if (events == NoneEvent) | ||
{ | ||
return; | ||
} | ||
pollfd pfd; | ||
pfd.events = events; | ||
pfd.fd = EventHandler->GetFd(); | ||
|
||
m_Pollfds.push_back(pfd); | ||
|
||
m_IndexBySocketFd[sfd] = m_Pollfds.size() - 1; | ||
|
||
m_Handlers[sfd] = EventHandler; | ||
} | ||
else | ||
{ | ||
UInt32 idx = m_IndexBySocketFd[EventHandler->GetFd()]; | ||
|
||
if (events == NoneEvent) | ||
{ | ||
|
||
std::iter_swap(m_Pollfds.begin() + idx, m_Pollfds.end() - 1); | ||
m_Pollfds.pop_back(); | ||
m_IndexBySocketFd.erase(sfd); | ||
m_Handlers.erase(sfd); | ||
} | ||
else | ||
{ | ||
m_Pollfds[idx].events = events; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "Acceptor.h" | ||
namespace Young | ||
{ | ||
|
||
Acceptor::Acceptor(SocketFd fd, std::shared_ptr<Reactor>& reactor) :m_Fd(fd), m_Reactor(reactor) | ||
{ | ||
} | ||
|
||
void Acceptor::HandleRead() | ||
{ | ||
sockaddr_in saddr; | ||
socklen_t len = sizeof saddr; | ||
SocketFd connfd = accept(m_Fd, (sockaddr*)&saddr, &len); | ||
printf("%d\n", WSAGetLastError()); | ||
std::shared_ptr<Connector> conn = std::make_shared<Connector>(connfd, m_Reactor); | ||
m_Reactor->RegisterHandler(conn,m_Reactor->ReadEvent()); | ||
} | ||
|
||
void Acceptor::HandleWrite() | ||
{ | ||
} | ||
|
||
void Acceptor::HandleError() | ||
{ | ||
m_Reactor->RemoveHandler(shared_from_this()); | ||
} | ||
|
||
SocketFd Acceptor::GetFd() | ||
{ | ||
return m_Fd; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifndef _YOUNG_ACCEPTOR_H_ | ||
#define _YOUNG_ACCEPTOR_H_ | ||
#include<EventHandler.h> | ||
#include<memory> | ||
#include<Reactor.h> | ||
#include<Connector.h> | ||
namespace Young | ||
{ | ||
|
||
class Acceptor :public EventHandler, public std::enable_shared_from_this<Acceptor> | ||
{ | ||
public: | ||
Acceptor(SocketFd fd, std::shared_ptr<Reactor>& reactor); | ||
void HandleRead()override; | ||
void HandleWrite()override; | ||
void HandleError()override; | ||
SocketFd GetFd()override; | ||
private: | ||
SocketFd m_Fd; | ||
std::shared_ptr<Reactor> m_Reactor; | ||
}; | ||
} | ||
#endif // ! _YOUNG_ACCEPTOR_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include "Connector.h" | ||
#include<iostream> | ||
namespace Young | ||
{ | ||
Connector::Connector(SocketFd fd, std::shared_ptr<Reactor>& reactor) :m_Fd(fd), m_Reactor(reactor) | ||
{ | ||
m_Status = NCS_NORMAL; | ||
} | ||
void Connector::HandleRead() | ||
{ | ||
recv(m_Fd, m_Buffer, 1000,0); | ||
std::cout << m_Buffer << std::endl; | ||
} | ||
void Connector::HandleWrite() | ||
{ | ||
} | ||
void Connector::HandleError() | ||
{ | ||
} | ||
SocketFd Connector::GetFd() | ||
{ | ||
return m_Fd; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#ifndef _YOUNG_CONNECTOR_H_ | ||
#define _YOUNG_CONNECTOR_H_ | ||
#include<EventHandler.h> | ||
#include<memory> | ||
#include<Reactor.h> | ||
namespace Young | ||
{ | ||
|
||
enum NetConnectionType | ||
{ | ||
NCT_ACTIVE, //主动连接 | ||
NCT_PASSIVE, //被动连接 | ||
}; | ||
|
||
/** | ||
*@brief 连接状态 | ||
*/ | ||
enum NetConnectionStatus | ||
{ | ||
NCS_CLOSED, //初始状态,还没连接 | ||
NCS_VERIFY, //验证阶段 | ||
NCS_NORMAL //正常通信状态 | ||
}; | ||
class Connector :public EventHandler, public std::enable_shared_from_this<Connector> | ||
{ | ||
public: | ||
Connector(SocketFd fd, std::shared_ptr<Reactor>& reactor); | ||
void HandleRead()override; | ||
void HandleWrite()override; | ||
void HandleError()override; | ||
SocketFd GetFd()override; | ||
private: | ||
SocketFd m_Fd; | ||
std::shared_ptr<Reactor> m_Reactor; | ||
char m_Buffer[10086]; | ||
NetConnectionStatus m_Status; | ||
}; | ||
} | ||
#endif // !_YOUNG_CONNECTOR_H_ |
Oops, something went wrong.