Skip to content

Commit

Permalink
Fix inproper socket closing (allows reuse of descriptors),
Browse files Browse the repository at this point in the history
Add Extern mutex for SimpleLog -- eliminates cases when on one line were more outputs,
and again some codestyling
!
  • Loading branch information
NTX authored and NTX committed Mar 10, 2012
1 parent f9bd997 commit 74aed7c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
18 changes: 12 additions & 6 deletions Application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ _diffTime(0)
Daemonize(StringConfigs[CONFIG_STRING_PID_FILE].c_str(), StringConfigs[CONFIG_STRING_WORKING_DIRECTORY].c_str());

_initGlobals();

sLog->outString(" ___ __ ___ __ ______ ___ ________");
sLog->outString("| | | | / / / \\ | __ \\ / \\ / ____/");
sLog->outString("| | | |_/ / / /\\ \\ | | | \\ / /\\ \\ \\ \\____");
Expand Down Expand Up @@ -336,6 +337,9 @@ bool Application::LoadLibrary()

void Application::_initGlobals()
{
writeMutex = new pthread_mutex_t;
pthread_mutex_init(writeMutex, NULL);

sLog = new SimpleLog(control, debug, "Server.log");
sigHandler = new SignalHandler();
libLoader = new SharedLibrary();
Expand All @@ -356,6 +360,7 @@ void Application::_uninitGlobals()
if (freezeDetector)
delete freezeDetector;
delete sLog;
delete writeMutex;
}

void Application::_InitServerSocket()
Expand All @@ -375,17 +380,17 @@ void Application::_InitServerSocket()
dest.sin_family = AF_INET;
dest.sin_port = htons(IntConfigs[CONFIG_INT_BIND_PORT]);
dest.sin_addr.s_addr = inet_addr(StringConfigs[CONFIG_STRING_BIND_IP].c_str());
//Bind
r= bind(ServerSocket, (struct sockaddr*)&dest, sizeof(dest));
// Bind
r = bind(ServerSocket, (struct sockaddr*)&dest, sizeof(dest));
if (r == -1)
{
sLog->outError("Bind Returned Error: %d", errno);
close(ServerSocket);
exit(-1);
}

//Listen
r=listen(ServerSocket, 5);
// Listen
r = listen(ServerSocket, 1024);
if (r == -1)
{
sLog->outError("Listen Returned Error: %d", errno);
Expand Down Expand Up @@ -457,10 +462,11 @@ uint32 Application::Update()
while (!terminate)
{
test = Recv;
selectTimeout.tv_sec = 1;
selectTimeout.tv_sec = 10;
selectTimeout.tv_usec = 500000;
nfds = ((socketMgr->GetHighestFd() > ServerSocket) ? socketMgr->GetHighestFd() : ServerSocket) + 1;
r = select(nfds, &test, NULL, NULL, &selectTimeout);

_diffTime = getMsTimeDiffToNow(_lastUpdate);
gettimeofday(&_lastUpdate, 0);
if (r == 0)
Expand All @@ -481,7 +487,7 @@ uint32 Application::Update()
setNonblocking(newSocket);
Socket* sock = new Socket(newSocket, peer);
socketMgr->AddSocket(sock);
sLog->outDebug("[Recv Thread] Recieved New Connection From: %s FD: %d", inet_ntoa(peer.sin_addr), sock->GetFD());
sLog->outDebug("[Recv Thread] Recieved New Connection From: %s FD: %d", inet_ntoa(peer.sin_addr), newSocket);
if (proto.OnConnect)
{
//handler->AddDelayedEvent(DelayedEvent(EVENT_CONNECT, newSocket, peer.sin_addr, 100));
Expand Down
22 changes: 12 additions & 10 deletions SimpleLog/SimpleLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,23 @@ logf(NULL), mdate(NULL), Debug(D), Control(C)
fclose(fopen(logf, "w"));
}
mdate= new char[256];
pthread_mutex_init(&writeMutex, NULL);
if (!writeMutex)
pthread_mutex_init(writeMutex, NULL);
}

SimpleLog::~SimpleLog()
{
if (mdate)
delete mdate;
pthread_mutex_destroy(&writeMutex);
if (writeMutex)
pthread_mutex_destroy(writeMutex);
}

int SimpleLog::outControl(const char* format, ...)
{
if (Control)
{
pthread_mutex_lock(&writeMutex);
pthread_mutex_lock(writeMutex);
va_list arg;
int done = 0;
if (strlen(format))
Expand Down Expand Up @@ -53,7 +55,7 @@ int SimpleLog::outControl(const char* format, ...)
}
std::cout << std::endl;
std::cout.flush();
pthread_mutex_unlock(&writeMutex);
pthread_mutex_unlock(writeMutex);
return done;
}
else
Expand All @@ -64,7 +66,7 @@ int SimpleLog::outString(const char* format, ...)
{
va_list arg;
int done = 0;
pthread_mutex_lock(&writeMutex);
pthread_mutex_lock(writeMutex);
if (strlen(format))
{
memset(mdate, 0, 256);
Expand Down Expand Up @@ -92,15 +94,15 @@ int SimpleLog::outString(const char* format, ...)
}
std::cout << std::endl;
std::cout.flush();
pthread_mutex_unlock(&writeMutex);
pthread_mutex_unlock(writeMutex);
return done;
}

int SimpleLog::outDebug(const char* format, ...)
{
if (Debug)
{
pthread_mutex_lock(&writeMutex);
pthread_mutex_lock(writeMutex);
va_list arg;
int done = 0;
if (strlen(format))
Expand Down Expand Up @@ -130,7 +132,7 @@ int SimpleLog::outDebug(const char* format, ...)
va_end(arg);
std::cout << std::endl;
std::cout.flush();
pthread_mutex_unlock(&writeMutex);
pthread_mutex_unlock(writeMutex);
return done;
}
else
Expand All @@ -141,7 +143,7 @@ int SimpleLog::outError(const char* format, ...)
{
va_list arg;
int done = 0;
pthread_mutex_lock(&writeMutex);
pthread_mutex_lock(writeMutex);
if (strlen(format))
{
memset(mdate, 0, 200);
Expand Down Expand Up @@ -169,6 +171,6 @@ int SimpleLog::outError(const char* format, ...)
}
std::cout << std::endl;
std::cout.flush();
pthread_mutex_unlock(&writeMutex);
pthread_mutex_unlock(writeMutex);
return done;
}
3 changes: 2 additions & 1 deletion SimpleLog/SimpleLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using std::endl ;
using std::cout ;

extern pthread_mutex_t* writeMutex;

class SimpleLog
{
public:
Expand All @@ -29,7 +31,6 @@ class SimpleLog
time_t d ;
bool &Debug;
bool &Control;
pthread_mutex_t writeMutex;
};

#endif
5 changes: 2 additions & 3 deletions Sockets/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ Socket::~Socket()

int Socket::Close()
{
if (socket && isConnected)
return close(socket);
return 0;
shutdown(socket, SHUT_RDWR);
return close(socket);
}

int Socket::Send(const void* buffer, size_t len, int flags)
Expand Down
1 change: 1 addition & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

SimpleLog* sLog;
Application* app;
pthread_mutex_t* writeMutex;

int main(int argc, char* argv[])
{
Expand Down

0 comments on commit 74aed7c

Please sign in to comment.