forked from derandark/PhatAC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
162 lines (134 loc) · 3.41 KB
/
Copy pathServer.cpp
File metadata and controls
162 lines (134 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "StdAfx.h"
#include "Server.h"
#include "Rules.h"
#include "Database.h"
#include "World.h"
#include "Network.h"
#include "NetFood.h"
#include "ChatMsgs.h"
CDatabase *g_pDB = 0;
CWorld *g_pWorld = 0;
CNetwork *g_pNetwork = 0;
TURBINEPORTAL *g_pPortal = 0;
TURBINECELL *g_pCell = 0;
GAMERULES *g_pGameRules = 0;
CPhatServer::CPhatServer(in_addr hostmachine, u_short hostport)
{
m_lastframe = g_pGlobals->m_CounterTime;
m_frames = 0;
//
memcpy(&m_hostaddr, &hostmachine, sizeof(in_addr));
m_hostport = hostport;
g_pGlobals->ResetPackets();
m_socketCount = 10;
for (int i = 0; i < m_socketCount; i++)
m_sockets[i] = INVALID_SOCKET;
InitializeSocket(hostport);
g_pPortal = new TURBINEPORTAL();
g_pCell = new TURBINECELL();
g_pGameRules = new GAMERULES();
g_pDB = new CDatabase;
g_pWorld = new CWorld;
g_pNetwork = new CNetwork(m_sockets, m_socketCount);
OutputConsole("The server is now online.\r\n");
}
CPhatServer::~CPhatServer()
{
if (g_pNetwork)
{
SystemBroadcast("ATTENTION - This Asheron's Call Server is shutting down NOW!!!!");
g_pNetwork->Think();
SafeDelete(g_pNetwork);
}
SafeDelete(g_pWorld);
SafeDelete(g_pDB);
SafeDelete(g_pGameRules);
SafeDelete(g_pCell);
SafeDelete(g_pPortal);
for (int i = 0; i < m_socketCount; i++)
{
if (m_sockets[i] != INVALID_SOCKET)
{
closesocket(m_sockets[i]);
m_sockets[i] = INVALID_SOCKET;
}
}
}
void CPhatServer::InitializeSocket(u_short port)
{
SOCKADDR_IN localhost;
localhost.sin_family = AF_INET;
localhost.sin_addr.s_addr = INADDR_ANY;
for (int i = 0; i < m_socketCount; i++)
m_sockets[i] = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
u_short startport = port;
while (port < (startport + 30))
{
localhost.sin_port = htons(port);
if (!bind(m_sockets[0], (struct sockaddr *)&localhost, sizeof(SOCKADDR_IN)))
{
OutputConsole("Bound to port %u!\r\n", port);
break;
}
OutputConsole("Failed bind on port %u!\r\n", port);
port++;
}
if (port == 9080)
{
OutputConsole("Failure to bind socket!\r\n");
}
else
{
SendMessage(GetDlgItem(g_pGlobals->GetWindowHandle(), IDC_SERVERPORT), WM_SETTEXT, 0, (LPARAM)csprintf("%u", port));
int basePort = port;
// Try to bind the other ports
for (int i = 1; i < m_socketCount; i++)
{
localhost.sin_port = htons(basePort + i);
if (bind(m_sockets[i], (struct sockaddr *)&localhost, sizeof(SOCKADDR_IN)))
{
OutputConsole("Failure to bind socket port %d!\r\n", basePort + i);
}
}
}
//Non-blocking sockets are more fun
for (int i = 0; i < m_socketCount; i++)
{
unsigned long arg = 1;
ioctlsocket(m_sockets[i], FIONBIO, &arg);
}
}
void CPhatServer::SystemBroadcast(char *text)
{
if (!g_pNetwork || !g_pWorld)
return;
OutputConsole("Broadcast, \"%s\"\r\n", text);
g_pWorld->BroadcastGlobal(ServerBroadcast("System", text, 1), PRIVATE_MSG);
}
void CPhatServer::Tick(void)
{
g_pGlobals->Update();
if ((m_lastframe + g_pGlobals->m_CounterFreq) < g_pGlobals->m_CounterTime)
{
double timediff = (g_pGlobals->m_CounterTime - m_lastframe) / (double)g_pGlobals->m_CounterFreq;
UpdateFramesHUD((UINT64)(m_frames / timediff));
m_lastframe = g_pGlobals->m_CounterTime;
m_frames = 0;
}
g_pNetwork->Think();
g_pWorld->Think();
g_pCell->Think();
m_frames++;
}
u_short CPhatServer::GetPort()
{
return m_hostport;
}
void CPhatServer::KickClient(WORD index)
{
g_pNetwork->KickClient(index);
}
void CPhatServer::BanClient(WORD index)
{
KickClient(index); //for now
}