Patch level : 10.0

Files correlati     : authoriz lurch
Ricompilazione Demo : [ ]
Commento            :
Migliorata gestione socket non validi


git-svn-id: svn://10.65.10.50/trunk@20484 c028cbd2-c16b-5b4b-a496-9718f37d4682
This commit is contained in:
guy 2010-05-20 15:35:49 +00:00
parent 225c9c7c06
commit f1c5a81588
4 changed files with 107 additions and 48 deletions

View File

@ -6,11 +6,6 @@
#include <wx/hashset.h>
#include <wx/textfile.h>
#ifdef __WXMSW__
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
///////////////////////////////////////////////////////////
// TUserInfo
///////////////////////////////////////////////////////////
@ -335,6 +330,12 @@ void TAuthorizationServer::GenerateIndex(wxString& strFilename)
ay.SetAttr("href", "year.htm") << "Assistance Year";
AddNumber(tr1, m_Dongle.YearAssist());
int y, v, t, p; GetVersionInfo(y, v, t, p);
wxString strVersion; strVersion.Printf(wxT("%d.%d.%d"), v, t, p);
TXmlItem& tr1a = body.AddChild("tr");
tr1a.AddChild("td") << "Version";
tr1a.AddChild("td").SetAttr("align", "right") << strVersion;
TXmlItem& tr2 = body.AddChild("tr");
TXmlItem& mu = tr2.AddChild("td").AddChild("a");
mu.SetAttr("href", "maxusers.htm") << "Maximum Users";

View File

@ -24,6 +24,7 @@
#include <wx/mimetype.h>
#include <wx/mstream.h>
#include <wx/sckstrm.h>
#include <wx/tokenzr.h>
#ifndef __WXMSW__
#include <wx/fileconf.h>
@ -33,15 +34,13 @@
// Utilities
///////////////////////////////////////////////////////////
IMPLEMENT_ABSTRACT_CLASS(TBaseServerApp, wxApp)
TBaseServerApp& GetServerApp()
{
return (TBaseServerApp&)*wxTheApp;
}
{ return *wxStaticCast(wxTheApp, TBaseServerApp); }
wxString Date2String(const wxDateTime& date)
{
return date.Format("%d-%m-%Y");
}
{ return date.Format("%d-%m-%Y"); }
wxDateTime String2Date(const wxString& str)
{
@ -66,9 +65,7 @@ wxSocketBase& operator<<(wxSocketBase& outf, wxString str)
}
wxSocketBase& operator<<(wxSocketBase& outf, size_t num)
{
return outf << wxString::Format("%u", num);
}
{ return outf << wxString::Format("%u", num); }
// --------------------------------------------------------------------------
// classes
@ -147,13 +144,15 @@ void TTaskBarIcon::Init()
enum
{
SERVER_ID = 1001,
SOCKET_ID = 1002
SOCKET_ID = 1002,
DATAGRAM_ID = 1003,
};
BEGIN_EVENT_TABLE(TBaseServerApp, wxApp)
EVT_SOCKET(SERVER_ID, TBaseServerApp::OnServerEvent)
EVT_SOCKET(SOCKET_ID, TBaseServerApp::OnSocketEvent)
EVT_SOCKET(DATAGRAM_ID, TBaseServerApp::OnDatagramEvent)
EVT_IDLE(TBaseServerApp::OnIdle)
END_EVENT_TABLE()
@ -504,10 +503,11 @@ void TBaseServerApp::OnSocketEvent(wxSocketEvent& e)
switch(e.GetSocketEvent())
{
case wxSOCKET_INPUT:
if (sock != NULL && sock->IsOk())
{
// We disable input events, so that the test doesn't trigger wxSocketEvent again.
sock->SetNotify(wxSOCKET_LOST_FLAG);
TCommand * message = new TCommand;
TCommand* message = new TCommand;
message->m_Sock = sock;
// Read the data
@ -529,23 +529,38 @@ void TBaseServerApp::OnSocketEvent(wxSocketEvent& e)
case wxSOCKET_LOST:
if (m_bLogVerbose)
WriteLog("--- Socket lost.");
if (IsAdvanced() || wxDateTime::Now().GetDay() > 15)
sock->Destroy();
if (sock != NULL)
{
for (int i = m_Sockets.GetCount()-1; i >= 0; i--)
{
TCommand& cmd = *(TCommand*)m_Sockets[i];
if (cmd.m_Sock == sock)
m_Sockets.RemoveAt(i);
}
if (sock->IsOk())
sock->Destroy();
}
break;
default:
break;
}
}
void TBaseServerApp::OnDatagramEvent(wxSocketEvent& WXUNUSED(e))
{
}
void TBaseServerApp::OnIdle(wxIdleEvent& evt)
{
if (m_Sockets.GetCount() > 0)
{
wxSocketBase& sock = *(((TCommand*)m_Sockets[0])->m_Sock);
TCommand& cmd = *(TCommand*)m_Sockets[0];
wxSocketBase& sock = *cmd.m_Sock;
const bool valid_socket = sock.IsOk();
if (valid_socket)
sock.SetNotify(wxSOCKET_LOST_FLAG);
wxString & str = ((TCommand *) m_Sockets[0])->m_Command;
wxString& str = cmd.m_Command;
//scrive sul log solo se chiacchierone!
if (m_bLogVerbose)
WriteLog(str);
@ -669,6 +684,9 @@ bool TBaseServerApp::GetConfigBool(const wxChar* key, bool def, const wxChar* ap
int TBaseServerApp::GetDefaultPort() const
{ return GetConfigInt("Port", 3883); }
int TBaseServerApp::GetUDPPort() const
{ return GetConfigInt("PortUDP", 0); }
#define OEM_INI wxT("../setup/oem.ini")
int TBaseServerApp::GetOemInt(const wxChar* key, int def) const
@ -715,7 +733,7 @@ wxString TBaseServerApp::GetInstallString(const wxChar* key, const wxChar* modul
bool TBaseServerApp::IsAdvanced() const
{
const int nOEM = GetOemInt(wxT("OEM"), -1);
return nOEM==0 || nOEM>2;
return nOEM==0;
}
wxString TBaseServerApp::GetLogFileName() const
@ -730,6 +748,7 @@ bool TBaseServerApp::IsRunning() const
bool TBaseServerApp::OnInit()
{
m_server = NULL;
m_datagram = NULL;
m_log = NULL;
m_bRunning = false;
@ -753,8 +772,9 @@ bool TBaseServerApp::OnInit()
// Create the address - defaults to localhost:0 initially
wxIPV4address addr;
addr.AnyAddress(); // I docs affermano che AnyAddress sia meglio di LocalHost
addr.Service(GetDefaultPort());
// Create the socket
const int nTCPPort = GetDefaultPort();
addr.Service(nTCPPort);
// Create the TCP socket
m_server = new wxSocketServer(addr);
// Create the Log file
@ -793,7 +813,7 @@ bool TBaseServerApp::OnInit()
// Setup the event handler and subscribe to connection events
m_server->SetEventHandler(*this, SERVER_ID);
m_server->SetNotify(wxSOCKET_CONNECTION_FLAG);
m_server->Notify(TRUE);
m_server->Notify(true);
wxString str;
str << GetAppName() << _(" listening at ") << addr.Hostname() << wxT(":") << addr.Service();
@ -817,7 +837,23 @@ bool TBaseServerApp::OnInit()
}
#endif
return m_bRunning;
/*
// Create the UDP socket
const int nUDPPort = GetUDPPort();
if (m_bRunning && nUDPPort > nTCPPort)
{
wxIPV4address udp_addr;
udp_addr.AnyAddress(); // I docs affermano che AnyAddress sia meglio di LocalHost
udp_addr.Service(nUDPPort);
m_datagram = new wxDatagramSocket(udp_addr);
// Setup the event handler and subscribe to connection events
m_datagram->SetEventHandler(*this, DATAGRAM_ID);
m_datagram->Notify(true);
}
*/
return m_bRunning;
}
int TBaseServerApp::OnExit()
@ -902,3 +938,20 @@ size_t TBaseServerApp::ParseArguments(wxString args, THashTable& hashArgs) const
}
return hashArgs.GetCount();
}
bool TBaseServerApp::GetVersionInfo(int& year, int& release, int& tag, int& patch) const
{
const char* const VERSIONANDPATCH = "Don't cry for me Argentina.2091.10.00.0000.2101";
wxStringTokenizer tok(VERSIONANDPATCH, wxT("."));
tok.GetNextToken(); // Ignore string
year = wxAtoi(tok.GetNextToken());
release = wxAtoi(tok.GetNextToken());
tag = wxAtoi(tok.GetNextToken());
patch = wxAtoi(tok.GetNextToken());
int checksum = wxAtoi(tok.GetNextToken());
bool valid = year >= 2091 && release > 0 && tag >= 0 && patch >= 0 &&
checksum == (year + release + tag + patch);
return valid;
}

View File

@ -80,8 +80,11 @@ public:
// Define a new application type
class TBaseServerApp : public wxApp
{
DECLARE_ABSTRACT_CLASS(TBaseServerApp)
private:
wxSocketServer* m_server;
wxDatagramSocket* m_datagram;
wxFileOutputStream* m_log;
wxString m_strPath, m_strTempDir, m_strIni;
bool m_bRunning;
@ -122,12 +125,14 @@ protected:
wxString GetInstallString(const wxChar* key, const wxChar* module = wxEmptyString) const;
bool IsAdvanced() const;
bool GetVersionInfo(int& year, int& release, int& tag, int& patch) const;
public:
// Utilities
virtual const wxChar* GetAppName() const = 0;
virtual const wxString& GetConfigName() const;
virtual int GetDefaultPort() const; // Retrieves Port usig GetConfigInt
virtual int GetUDPPort() const; // Retrieves UDP Port usig GetConfigInt
virtual void SetConfigString(const wxChar* key, const wxChar* val, const wxChar* app = NULL) const;
void SetConfigInt(const wxChar* key, int val, const wxChar* app = NULL) const;
@ -156,8 +161,10 @@ public:
// Initialization (Do NOT use nor reimplement!)
virtual bool OnInit();
virtual int OnExit();
void OnServerEvent(wxSocketEvent& event);
void OnSocketEvent(wxSocketEvent& event);
void OnServerEvent(wxSocketEvent& evt);
void OnSocketEvent(wxSocketEvent& evt);
void OnDatagramEvent(wxSocketEvent& evt);
void OnIdle(wxIdleEvent& event);
DECLARE_EVENT_TABLE();
};

View File

@ -75,16 +75,17 @@ bool TLurchServer::PingProcess(const wxString& strApp)
{
bool bPinged = false;
const wxString strPort = GetConfigString("Port", "", strApp);
if (!strPort.IsEmpty())
const unsigned short nPort = GetConfigInt("Port", 0, strApp);
if (nPort > 0)
{
int nTimeOut = m_PingTimer.GetInterval()/4; // msec
if (nTimeOut < 250) nTimeOut = 250; else
if (nTimeOut > 2000) nTimeOut = 2000;
wxIPV4address ipAddress;
ipAddress.LocalHost();
ipAddress.Service(strPort);
ipAddress.AnyAddress();
ipAddress.Service(nPort);
wxSocketClient sSock(wxSOCKET_NOWAIT);
sSock.Connect(ipAddress, false);
if (sSock.WaitOnConnect(0, nTimeOut))
@ -102,6 +103,11 @@ bool TLurchServer::PingProcess(const wxString& strApp)
sSock.Discard(); // Tralascia ulteriori dati in arrivo
}
}
/*
wxDatagramSocket dSock(ipAddress, wxSOCKET_NOWAIT);
ipAddress.Service(1884);
dSock.SendTo(ipAddress, "PING\n", 5);
*/
}
return bPinged;
@ -302,17 +308,12 @@ void TLurchServer::GenerateFile(wxString& strFilename)
wxString strHost; strHost << wxGetHostName() << wxT(":") << GetDefaultPort();
AddTableRow(panel, wxT("Host Name"), strHost);
const wxString strVersion = GetInstallString(wxT("Versione"));
wxString str; str.Printf(wxT("%04d / %5.2lf"), wxAtoi(strVersion.Left(4)), wxAtoi(strVersion.Mid(4))/100.0);
AddTableRow(panel, wxT("Version"), str);
AddTableRow(panel, wxT("Patch"), GetInstallString(wxT("Patch")));
AddTableRow(panel, wxT("Date"), GetInstallString(wxT("Data")));
int y, v, t, p; GetVersionInfo(y, v, t, p);
wxString strVer; strVer.Printf(wxT("%d.%d.%d"), v, t, p);
AddTableRow(panel, wxT("Version"), strVer);
if (IsAdvanced())
{
wxString strFreq; strFreq << m_PingTimer.GetInterval()/1000;
AddTableRow(panel, wxT("Ping Frequency"), strFreq);
}
wxString strFreq; strFreq << m_PingTimer.GetInterval()/1000;
AddTableRow(panel, wxT("Ping Frequency"), strFreq);
html.Save(strFilename);
}
@ -657,14 +658,11 @@ bool TLurchServer::Initialization()
{
for (size_t i = 0; i < nAuto; i++)
StartProcess(arr[i]);
if (IsAdvanced())
const int nFreq = GetConfigInt("PingFreq");
if (nFreq > 0)
{
const int nFreq = GetConfigInt("PingFreq");
if (nFreq > 0)
{
m_PingTimer.Start(nFreq * 1000); // sec to msec
m_Semaphore.Post(); // GREEN!
}
m_PingTimer.Start(nFreq * 1000); // sec to msec
m_Semaphore.Post(); // GREEN!
}
}
return true;