// Servers.cpp
#include "Servers.h"
#include "MemMan.h"
#include "KNetCli.h"
#include "Protocol.h"
extern void UpdateNetEventsTable(int TypeOfChange);
//---------------------------------------------------------------------------
#define SIMULATION
/*
// Network.cpp
#include "DiskMan.h"
#include "WinDlg.h"
#include "StatData.h"
#include "Views.h"
#include "AcidBase.h"
#include "Network.h"
#include "Compress.h"
#include "Protocol.h"
#include "Station.h"
*/
#define CALL_SERVER_PERIOD (10 * 1000)
// MasterPassword = Mitko_2001
// DefaultNetPassword = Acids_2001
// KrusPassword = Acids_2001
// DefaultNetPassword = KrusAdmin
//char MasterPassword[MAX_NET_PASSWORD_LEN + 1] = "Ws~uyi<::;";
char MasterPassword[MAX_NET_PASSWORD_LEN + 1] = "Mitko_2001";
//---------------------------------------------------------------------------
// 丑瘩�, 獗螂錨溘憂 頑 頡溝櫻詼獗錙 熱 窟熱 陲夔蒙
#pragma pack(push, 1)
struct DEVICE_SERVER_CONNECTION
{
DWORD Id;
WORD Port;
char *Address;
KNetClient *Client;
BOOL Connected;
DWORD CallServerTime;
};
#pragma pack(pop)
MemoryBlock *ConnectionsDataBlock = NULL;
DEVICE_SERVER_CONNECTION *ConnectionData = NULL;
int ConnectionsCount = 0;
/*
#pragma pack(push, 1)
struct NET_EVENT_DATA
{
DWORD ServerId;
DWORD MessageId;
DWORD Size;
};
#pragma pack(pop)
*/
MemoryBlock *NetEventDataBlock = NULL;
NET_EVENT_DATA *NetEventData = NULL;
int NetEventCount = 0;
//---------------------------------------------------------------------------
// 仁菴�� 臻菊錚� � 縉撇懼�
BOOL AddNetEvent(NET_EVENT_DATA *Event)
{
if ( !Event )
return FALSE;
if ( !NetEventDataBlock )
{
NetEventDataBlock =
new MemoryBlock(sizeof(NET_EVENT_DATA), sizeof(NET_EVENT_DATA));
if ( !NetEventDataBlock )
return FALSE;
}
if ( !NetEventDataBlock->Add(Event, sizeof(NET_EVENT_DATA)) )
return FALSE;
NetEventData = (NET_EVENT_DATA *) NetEventDataBlock->GetDataPointer();
NetEventCount = NetEventDataBlock->GetDataSize() / sizeof(NET_EVENT_DATA);
return TRUE;
}
//---------------------------------------------------------------------------
// 仁菴�� 臻菊錚� � 縉撇懼�
BOOL AddNetEvent(DWORD EventId, DWORD ServerId, DWORD MessageId, DWORD Size)
{
NET_EVENT_DATA Event;
ZeroMemory(&Event, sizeof(NET_EVENT_DATA));
Event.EventId = EventId;
Event.ServerId = ServerId;
Event.MessageId = MessageId;
Event.Size = Size;
SYSTEMTIME Time;
GetLocalTime(&Time);
Event.Hour = Time.wHour;
Event.Min = Time.wMinute;
Event.Sec = Time.wSecond;
return AddNetEvent(&Event);
}
//---------------------------------------------------------------------------
int GetNetEvenCount()
{
return NetEventCount;
}
//---------------------------------------------------------------------------
BOOL GetNetEven(int Index, NET_EVENT_DATA *Event)
{
if ( !Event )
return FALSE;
if ( NetEventDataBlock && NetEventData &&
Index >= 0 && Index < NetEventCount )
{
*Event = NetEventData[Index];
return TRUE;
}
return FALSE;
}
//---------------------------------------------------------------------------
void GetNetEventName(BYTE NetEventId, char *Text)
{
switch ( NetEventId )
{
case NET_EVENT_CONNECT:
strcpy(Text, "柯櫻頊痢�");
return;
case NET_EVENT_DISCONNECT:
strcpy(Text, "俏筧欄熱錶 陲夔蒙");
return;
case NET_EVENT_SEND:
strcpy(Text, "俏窟琵痢�");
return;
case NET_EVENT_RECEIVE:
strcpy(Text, "俏徹影獗");
return;
}
Text[0] = 0;
}
//---------------------------------------------------------------------------
struct NET_MSG_NAME
{
DWORD Id;
char *Name;
};
NET_MSG_NAME NetMsgName[] =
{
{ NET_MSG_IDENTIFICATION, "IDENTIFICATION" },
{ NET_MSG_SUCCESS, "SUCCESS" },
{ NET_MSG_EVENT, "EVENT" },
{ NET_MSG_DIAGNOSTICS, "DIAGNOSTICS" },
{ 0, NULL }
};
//---------------------------------------------------------------------------
void GetNetMessageName(DWORD MessageId, char *Text)
{
if ( !Text )
return;
if ( !MessageId )
{
Text[0] = 0;
return;
}
int Index = 0;
while ( NetMsgName[Index].Id )
{
if ( MessageId == NetMsgName[Index].Id )
{
strcpy(Text, NetMsgName[Index].Name);
return;
}
Index++;
}
wsprintf(Text, "%d", MessageId);
}
BOOL CriticalSectionInitialized = FALSE;
CRITICAL_SECTION CriticalSection;
//---------------------------------------------------------------------------
inline void EnterCriticalSection()
{
if ( CriticalSectionInitialized )
EnterCriticalSection(&CriticalSection);
}
//---------------------------------------------------------------------------
inline void LeaveCriticalSection()
{
if ( CriticalSectionInitialized )
LeaveCriticalSection(&CriticalSection);
}
//---------------------------------------------------------------------------
DEVICE_SERVER_CONNECTION *GetConnection(const KNetClient *Client)
{
int Index;
for (Index = 0; Index < ConnectionsCount; Index++)
if ( Client == ConnectionData[Index].Client )
return ConnectionData + Index;
return NULL;
}
//---------------------------------------------------------------------------
void FreeConnectionResources(DEVICE_SERVER_CONNECTION *Connection)
{
if ( !Connection )
return;
if ( Connection->Client )
{
delete Connection->Client;
Connection->Client = NULL;
}
if ( Connection->Address )
{
delete Connection->Address;
Connection->Address = NULL;
}
}
//---------------------------------------------------------------------------
/*
KNetClient *Client = NULL;
BOOL Connected = FALSE;
int StationId = 0;
int OperatorId = 0;
MemoryBlock *ServerDataBuffer = NULL;
//DWORD ServerDataTableId = 0;
int RecordsCount = 0;
//int DeviceCount = 0;
//int GateCount = 0;
//int CardCount = 0;
//int WorkingTimeCount = 0;
//int ShiftCount = 0;
//int EventCount = 0;
//MemoryBlock *WorkingTimesBlock = NULL;
//MemoryBlock *ShiftsBlock = NULL;
//MemoryBlock *Block = NULL;
// �蝗 熱 愿頜痞粵摘� 蒙暸�
static int CardsSent = 0;
static int CurrentCardRecord = 0;
*/
//---------------------------------------------------------------------------
inline void MakeMessageHeader(void *Buffer,
int MessageId,
int Param1, int Param2,
int DataSize)
{
NET_MSG_HEADER *Header = (NET_MSG_HEADER *) Buffer;
Header->MessageId = MessageId;
Header->PCId = 0; //StationId; //ServerConfiguration.PCId;
Header->OperatorId = 0;
Header->Param1 = Param1;
Header->Param2 = Param2;
Header->DataSize = DataSize;
}
//---------------------------------------------------------------------------
// 行頜盜� 滓瘩� 熱 臻撼櫻
void SendToServer(DEVICE_SERVER_CONNECTION *Connection,
int MessageId, int Param1, int Param2,
char *Data, int DataSize)
{
if ( !Connection || !Connection->Client || !Connection->Connected )
return;
if ( !Data || !DataSize )
{
NET_MSG_HEADER Header;
ZeroMemory(&Header, sizeof(NET_MSG_HEADER));
Header.MessageId = MessageId;
Header.PCId = 0; //StationId;
Header.OperatorId = 0; //OperatorId;
Header.Param1 = Param1;
Header.Param2 = Param2;
Header.DataSize = 0;
Connection->Client->Send((char *) &Header, sizeof(NET_MSG_HEADER));
return;
}
char *Buffer = new char[sizeof(NET_MSG_HEADER) + DataSize];
if ( !Buffer )
return;
ZeroMemory(Buffer, sizeof(NET_MSG_HEADER));
NET_MSG_HEADER *Header = (NET_MSG_HEADER *) Buffer;
Header->MessageId = MessageId;
Header->PCId = 0; //StationId;
Header->OperatorId = 0; //Operat