Commit 4da0b670 by NitefullWind

1. 更新DLL,Socket在返回数据一定时间后再断开。

parent ebd75d4b
...@@ -2,7 +2,7 @@ QT -= core ...@@ -2,7 +2,7 @@ QT -= core
TEMPLATE = lib TEMPLATE = lib
CONFIG += dll CONFIG += dll c++11
DEFINES += FMSOCK_BUILD _UNICODE DEFINES += FMSOCK_BUILD _UNICODE
......
#include "fmsockserver.h" #include "fmsockserver.h"
#include <fmutils/fmutils.hpp> #include <fmutils/fmutils.hpp>
// 定时关闭Socket连接
void CloseSocket()
{
while(!SocketStop) {
for(std::list<ENDSOCKET>::iterator it=EndSocketList.begin(); it!=EndSocketList.end(); it++) {
if(it->times == 120) {
closesocket(it->socket);
}
it->times += 1;
}
if(!EndSocketList.empty() && EndSocketList.begin()->times > 120) {
EndSocketList.pop_front();
}
std::this_thread::sleep_for(std::chrono::milliseconds(1 * 1000));
}
for(std::list<ENDSOCKET>::iterator it=EndSocketList.begin(); it!=EndSocketList.end(); it++) {
closesocket(it->socket);
}
EndSocketList.clear();
}
FMSockServer::FMSockServer(FMApiRelay *relay) : FMSockServer::FMSockServer(FMApiRelay *relay) :
_worker(0), _worker(0),
_worker_tid(0), _worker_tid(0),
...@@ -28,6 +49,8 @@ FMSockServer::FMSockServer(FMApiRelay *relay) : ...@@ -28,6 +49,8 @@ FMSockServer::FMSockServer(FMApiRelay *relay) :
_addr_in.sin_family = AF_INET; _addr_in.sin_family = AF_INET;
_addr_in.sin_addr.S_un.S_addr = htonl(INADDR_ANY); _addr_in.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
_closeSocketThread = std::thread(CloseSocket);
} }
...@@ -39,6 +62,9 @@ FMSockServer::~FMSockServer() ...@@ -39,6 +62,9 @@ FMSockServer::~FMSockServer()
WSACleanup(); WSACleanup();
DeleteCriticalSection(&_cs); DeleteCriticalSection(&_cs);
SocketStop = true;
_closeSocketThread.join();
} }
...@@ -211,23 +237,30 @@ void FMSockServer::RecvRoutine(DWORD err, DWORD bytes, LPWSAOVERLAPPED overlappe ...@@ -211,23 +237,30 @@ void FMSockServer::RecvRoutine(DWORD err, DWORD bytes, LPWSAOVERLAPPED overlappe
if (io_data->relay) { if (io_data->relay) {
io_data->relay->Transfer(io_data->msg, response, len); io_data->relay->Transfer(io_data->msg, response, len);
} }
// unsigned long mode = 0;
// int res = ioctlsocket(io_data->socket, FIONBIO, &mode);
if (response) { if (response) {
// HANDLE writeEvent = WSACreateEvent();
// WSAEventSelect(io_data->socket, writeEvent, FD_WRITE);
// WaitForSingleObject(writeEvent, 5000);
send(io_data->socket, response, len, 0); send(io_data->socket, response, len, 0);
FMLOG("Transfered %s", response); FMLOG("Transfered %s", response);
} }
else { else {
FMLOG(_T("Failed to determine response data.")); FMLOG(_T("Failed to determine response data."));
} }
HANDLE closeEvent = WSACreateEvent(); // HANDLE closeEvent = WSACreateEvent();
WSAEventSelect(io_data->socket, closeEvent, FD_CLOSE); // WSAEventSelect(io_data->socket, closeEvent, FD_CLOSE);
DWORD dwRet = WaitForSingleObject(closeEvent, 5000); // DWORD dwRet = WaitForSingleObject(closeEvent, 5000);
// if(dwRet == WSA_WAIT_EVENT_0) { // if(dwRet == WSA_WAIT_EVENT_0) {
closesocket(io_data->socket); // closesocket(io_data->socket);
// } // }
WSACloseEvent(closeEvent); // WSACloseEvent(closeEvent);
EndSocketList.push_back({io_data->socket, 0});
//! Free all buffer //! Free all buffer
HeapFree(GetProcessHeap(), 0, io_data); HeapFree(GetProcessHeap(), 0, io_data);
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <Windows.h> #include <Windows.h>
#include <winsock2.h> #include <winsock2.h>
#include <list> #include <list>
#include <thread>
#define BACKLOG 5 #define BACKLOG 5
...@@ -42,6 +43,15 @@ typedef struct ...@@ -42,6 +43,15 @@ typedef struct
}FMSOCKDATA, *LPFMSOCKDATA; }FMSOCKDATA, *LPFMSOCKDATA;
//!
typedef struct
{
SOCKET socket;
int times;
} ENDSOCKET;
static std::list<ENDSOCKET> EndSocketList;
static bool SocketStop = false;
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
//! Class FMSocketServer //! Class FMSocketServer
...@@ -60,7 +70,6 @@ public: ...@@ -60,7 +70,6 @@ public:
VOID SetRelay(FMApiRelay* relay); VOID SetRelay(FMApiRelay* relay);
protected: protected:
BOOL _Listen(); BOOL _Listen();
...@@ -76,7 +85,7 @@ private: ...@@ -76,7 +85,7 @@ private:
FMApiRelay *_relay; FMApiRelay *_relay;
CRITICAL_SECTION _cs; CRITICAL_SECTION _cs;
std::thread _closeSocketThread;
}; };
//#ifdef __cplusplus //#ifdef __cplusplus
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment