-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNetCode.cpp
163 lines (134 loc) · 3.51 KB
/
NetCode.cpp
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
// @Begin License@
// This file is part of Coldest.
//
// Coldest is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Coldest is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Coldest. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2008-2012 Ben Nemec
// @End License@
#include "NetCode.h"
#include "ServerInfo.h"
#include "globals.h"
#include <iostream>
using std::endl;
const int NetCode::version = 8;
NetCode::NetCode() : lastnettick(SDL_GetTicks()),
currnettick(0),
error(false),
sendbps(0),
sentbytes(0)
{
sendpacketnum.next(); // 0 has special meaning
if (!(packet = SDLNet_AllocPacket(5000))) // 5000 is somewhat arbitrary
{
logout << "SDLNet_AllocPacket: " << SDLNet_GetError() << endl;
error = true;
}
// Keep a universal list of packets that need acking so we don't have to handle it specially in every subclass
acktypes.insert("h");
acktypes.insert("s");
acktypes.insert("D");
acktypes.insert("m");
acktypes.insert("O");
acktypes.insert("r");
acktypes.insert("T");
acktypes.insert("I");
acktypes.insert("K");
acktypes.insert("P");
acktypes.insert("f");
acktypes.insert("t");
acktypes.insert("L");
acktypes.insert("d");
acktypes.insert("R");
acktypes.insert("Y");
}
NetCode::~NetCode()
{
SDLNet_FreePacket(packet);
SDLNet_UDP_Close(socket);
}
void NetCode::Update()
{
if (!error)
{
Receive();
Send();
SendLoop();
}
}
void NetCode::SendLoop()
{
list<Packet>::iterator i = sendqueue.begin();
Uint32 currtick = SDL_GetTicks();
while (i != sendqueue.end())
{
if (i->sendtick <= currtick)
{
sentbytes += i->Send(packet, socket);
if (!i->ack || i->attempts > 5000) // Non-ack packets get sent once and then are on their own
{
i = sendqueue.erase(i);
continue;
}
}
++i;
}
if (sentbytestimer.elapsed() > 1000)
{
sendbps = (size_t)((float)sentbytes / (float)sentbytestimer.elapsed() * 1000.f);
sentbytes = 0;
sentbytestimer.start();
}
}
void NetCode::SendPacket(Packet& p)
{
sendqueue.push_back(p);
}
void NetCode::Receive()
{
while (SDLNet_UDP_Recv(socket, packet))
{
getdata = (char*)packet->data;
stringstream get(getdata);
get >> packettype;
get >> packetnum;
HandlePacket(get);
DoAck();
}
ReceiveExtended();
}
void NetCode::HandleAck(const unsigned long acknum)
{
for (list<Packet>::iterator i = sendqueue.begin(); i != sendqueue.end(); ++i)
{
if (i->ack == acknum)
{
sendqueue.erase(i);
break;
}
}
}
void NetCode::Ack(const unsigned long acknum)
{
Packet response(&address);
response << "A\n";
response << 0 << eol;
response << acknum << eol;
SendPacket(response);
}
// If you need to use a different Ack function, you will need to override this
void NetCode::DoAck()
{
if (acktypes.find(packettype) != acktypes.end())
Ack(packetnum);
}