Skip to content

Commit

Permalink
disable FDSet temporarily (may implement in the future)
Browse files Browse the repository at this point in the history
  • Loading branch information
navining committed Jul 12, 2020
1 parent 9eb2fbd commit 94662ce
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 41 deletions.
2 changes: 0 additions & 2 deletions examples/client/client.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#ifdef _WIN32
#endif

#include <iostream>
#include <thread>
Expand Down
3 changes: 2 additions & 1 deletion examples/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <atomic>
#include "TcpServer.h"
#include "Config.h"
#include "FDSet.h"
//#include "Allocator.hpp"

class MyServer : public TcpServer {
Expand Down Expand Up @@ -59,7 +60,7 @@ int main(int argc, char* argv[]) {
Config::Init(argc, argv);
const char *ip = Config::Instance().parseStr("IP", NULL);
u_short port = Config::Instance().parseInt("PORT", 4567);
int numOfThreads = Config::Instance().parseInt("THREAD", 2);
int numOfThreads = Config::Instance().parseInt("THREAD", 1);

LOG_SETPATH("zeus-server.log", "w");
MyServer server;
Expand Down
1 change: 1 addition & 0 deletions examples/server/server.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\net\Buffer.cpp" />
<ClCompile Include="..\..\net\FDSet.cpp" />
<ClCompile Include="..\..\net\Network.cpp" />
<ClCompile Include="..\..\net\TcpClient.cpp" />
<ClCompile Include="..\..\net\TcpConnection.cpp" />
Expand Down
25 changes: 14 additions & 11 deletions net/FDSet.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "FDSet.h"

FDSet::FDSet(int fdCount) {
_fdCount = fdCount <= MAX_FD_SIZE ? fdCount : MAX_FD_SIZE;
#define FD_SIZE 10240

FDSet::FDSet() {
_fdCount = FD_SIZE;
#ifdef _WIN32
_fdSize = sizeof(u_int) + sizeof(SOCKET) * _fdCount;
#else
Expand All @@ -19,15 +21,21 @@ FDSet::~FDSet() {
}
}

inline void FDSet::set(SOCKET sock) {
void FDSet::set(SOCKET sock) {
#ifndef _WIN32
if (sock >= FD_SIZE) {
LOG_ERROR("Socket fd exceed limit: %d\n", FD_SIZE);
return;
}
#endif // _WIN32
FD_SET(sock, _fdset);
}

inline void FDSet::clear(SOCKET sock) {
void FDSet::clear(SOCKET sock) {
FD_CLR(sock, _fdset);
}

inline void FDSet::zero() {
void FDSet::zero() {
#ifdef _WIN32
FD_ZERO(_fdset);
#else
Expand All @@ -53,11 +61,6 @@ int FDSet::fdCount() const {

// Warning: assume same size here

FDSet::FDSet(const FDSet & other) {
memcpy(_fdset, other.fdset(), other.fdSize());
}

FDSet& FDSet::operator=(const FDSet &other) {
void FDSet::copy(const FDSet & other) {
memcpy(_fdset, other.fdset(), other.fdSize());
return *this;
}
10 changes: 3 additions & 7 deletions net/FDSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

#include "common.h"

#define MAX_FD_SIZE 65535

class FDSet {
public:
FDSet(int fdCount);
FDSet();

~FDSet();

Expand All @@ -26,10 +24,8 @@ class FDSet {
int fdCount() const;

// Warning: assume same size here
FDSet(const FDSet &other);

// Warning: assume same size here
FDSet& operator=(const FDSet &other);
void copy(const FDSet &other);

private:
fd_set *_fdset = nullptr;
size_t _fdSize = 0;
Expand Down
47 changes: 28 additions & 19 deletions net/TcpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,35 +162,44 @@ void TcpServer::onRun(Thread & thread) {

while (thread.isRun()) {

// Select
fd_set fdRead;
FD_ZERO(&fdRead);
// Put server sockets inside fd_set
FD_SET(_sock, &fdRead);

// Timeval
timeval t = { 0, 1 };

int ret = select(_sock + 1, &fdRead, 0, 0, &t);

if (ret < 0) {
LOG_ERROR("<server %d> Select - Fail...\n", _sock);
// IO multiplexing
if (!select()) {
thread.exit();
return;
}

// Server socket response: accept connection
if (FD_ISSET(_sock, &fdRead)) {
FD_CLR(_sock, &fdRead);
accept();
}

// Handle other services here...
// Benchmark
onIdle();
}
}

bool TcpServer::select()
{
fd_set fdRead;
FD_ZERO(&fdRead);
// Put server sockets inside fd_set
FD_SET(_sock, &fdRead);

// Timeval
timeval t = { 0, 1 };

int ret = ::select(_sock + 1, &fdRead, 0, 0, &t);

if (ret < 0) {
LOG_ERROR("<server %d> Select - Fail...\n", _sock);
return false;
}

// Server socket response: accept connection
if (FD_ISSET(_sock, &fdRead)) {
FD_CLR(_sock, &fdRead);
accept();
}

return true;
}

void TcpServer::onIdle() {

}
Expand Down
2 changes: 2 additions & 0 deletions net/TcpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class TcpServer : public Event {
// If connected
bool isRun();

bool select();

// Close socket
void close();

Expand Down
2 changes: 1 addition & 1 deletion utils/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define _common_h_

#ifdef _WIN32
#define FD_SETSIZE 10240 // Size of FD_SET
#define FD_SETSIZE 10240 // Size of fd_set
#define WIN32_LEAN_AND_MEAN
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
Expand Down

0 comments on commit 94662ce

Please sign in to comment.