Skip to content

Commit d05c3c5

Browse files
committed
Move socket binding out of Transport::init to separate method.
1 parent ee4eb87 commit d05c3c5

File tree

5 files changed

+55
-31
lines changed

5 files changed

+55
-31
lines changed

GeneralsMD/Code/GameEngine/Include/GameNetwork/Transport.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class Transport //: public MemoryPoolObject
5050

5151
Bool init( AsciiString ip, UnsignedShort port );
5252
Bool init( UnsignedInt ip, UnsignedShort port );
53+
Bool bind( void );
5354
void reset( void );
5455
Bool update( void ); ///< Call this once a GameEngine tick, regardless of whether the frame advances.
5556

@@ -80,10 +81,14 @@ class Transport //: public MemoryPoolObject
8081
DelayedTransportMessage m_delayedInBuffer[MAX_MESSAGES];
8182
#endif
8283

83-
UnsignedShort m_port;
8484
private:
85+
86+
void clearBuffers( void );
87+
8588
Bool m_winsockInit;
8689
UDP *m_udpsock;
90+
UnsignedInt m_ip;
91+
UnsignedShort m_port;
8792

8893
// Latency insertion and packet loss
8994
Bool m_useLatency;

GeneralsMD/Code/GameEngine/Source/GameNetwork/ConnectionManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,7 @@ void ConnectionManager::initTransport() {
14511451
m_transport = new Transport;
14521452
m_transport->reset();
14531453
m_transport->init(m_localAddr, m_localPort);
1454+
m_transport->bind();
14541455
}
14551456

14561457
/**

GeneralsMD/Code/GameEngine/Source/GameNetwork/LANAPI.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ void LANAPI::init( void )
100100
{
101101
m_gameStartTime = 0;
102102
m_gameStartSeconds = 0;
103+
m_transport->reset();
104+
m_transport->init(m_localIP, lobbyPort);
103105

104106
m_pendingAction = ACT_NONE;
105107
m_expiration = 0;
@@ -1255,6 +1257,10 @@ Bool LANAPI::SetLocalIP( UnsignedInt localIP )
12551257

12561258
m_transport->reset();
12571259
retval = m_transport->init(m_localIP, lobbyPort);
1260+
if (retval)
1261+
{
1262+
retval = m_transport->bind();
1263+
}
12581264
m_transport->allowBroadcasts(true);
12591265

12601266
return retval;

GeneralsMD/Code/GameEngine/Source/GameNetwork/NAT.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ void NAT::attachSlotList(GameSlot *slotList[], Int localSlot, UnsignedInt localI
621621
DEBUG_LOG(("NAT::attachSlotList - using %d as the starting port number", m_startingPortNumber));
622622
generatePortNumbers(slotList, localSlot);
623623
m_transport->init(m_localIP, getSlotPort(localSlot));
624+
m_transport->bind();
624625
}
625626

626627
Int NAT::getSlotPort(Int slot) {

GeneralsMD/Code/GameEngine/Source/GameNetwork/Transport.cpp

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -103,39 +103,34 @@ Bool Transport::init( UnsignedInt ip, UnsignedShort port )
103103
m_winsockInit = true;
104104
}
105105

106-
// ------- Bind our port --------
107-
if (m_udpsock)
108-
delete m_udpsock;
109-
m_udpsock = NEW UDP();
106+
m_ip = ip;
107+
m_port = port;
108+
clearBuffers();
110109

111-
if (!m_udpsock)
112-
return false;
110+
#if defined(RTS_DEBUG)
111+
if (TheGlobalData->m_latencyAverage > 0 || TheGlobalData->m_latencyNoise)
112+
m_useLatency = true;
113113

114-
int retval = -1;
115-
time_t now = timeGetTime();
116-
while ((retval != 0) && ((timeGetTime() - now) < 1000)) {
117-
retval = m_udpsock->Bind(ip, port);
118-
}
114+
if (TheGlobalData->m_packetLoss)
115+
m_usePacketLoss = true;
116+
#endif
119117

120-
if (retval != 0) {
121-
DEBUG_CRASH(("Could not bind to 0x%8.8X:%d", ip, port));
122-
DEBUG_LOG(("Transport::init - Failure to bind socket with error code %x", retval));
123-
delete m_udpsock;
124-
m_udpsock = NULL;
125-
return false;
126-
}
118+
return true;
119+
}
127120

121+
void Transport::clearBuffers( void )
122+
{
128123
// ------- Clear buffers --------
129-
int i=0;
130-
for (; i<MAX_MESSAGES; ++i)
124+
int i = 0;
125+
for (; i < MAX_MESSAGES; ++i)
131126
{
132127
m_outBuffer[i].length = 0;
133128
m_inBuffer[i].length = 0;
134129
#if defined(RTS_DEBUG)
135130
m_delayedInBuffer[i].message.length = 0;
136131
#endif
137132
}
138-
for (i=0; i<MAX_TRANSPORT_STATISTICS_SECONDS; ++i)
133+
for (i = 0; i < MAX_TRANSPORT_STATISTICS_SECONDS; ++i)
139134
{
140135
m_incomingBytes[i] = 0;
141136
m_outgoingBytes[i] = 0;
@@ -146,20 +141,36 @@ Bool Transport::init( UnsignedInt ip, UnsignedShort port )
146141
}
147142
m_statisticsSlot = 0;
148143
m_lastSecond = timeGetTime();
144+
}
149145

150-
m_port = port;
146+
Bool Transport::bind( void )
147+
{
148+
DEBUG_ASSERTCRASH(m_winsockInit, ("Transport::bind called before Transport::init"));
151149

152-
#if defined(RTS_DEBUG)
153-
if (TheGlobalData->m_latencyAverage > 0 || TheGlobalData->m_latencyNoise)
154-
m_useLatency = true;
150+
// ------- Bind our port --------
151+
if (m_udpsock)
152+
delete m_udpsock;
153+
m_udpsock = NEW UDP();
155154

156-
if (TheGlobalData->m_packetLoss)
157-
m_usePacketLoss = true;
158-
#endif
155+
if (!m_udpsock)
156+
return false;
159157

160-
return true;
161-
}
158+
int retval = -1;
159+
time_t now = timeGetTime();
160+
while ((retval != 0) && ((timeGetTime() - now) < 1000)) {
161+
retval = m_udpsock->Bind(m_ip, m_port);
162+
}
162163

164+
if (retval != 0) {
165+
DEBUG_CRASH(("Could not bind to 0x%8.8X:%d", m_ip, m_port));
166+
DEBUG_LOG(("Transport::bind - Failure to bind socket with error code %x", retval));
167+
delete m_udpsock;
168+
m_udpsock = NULL;
169+
return false;
170+
}
171+
172+
clearBuffers();
173+
}
163174
void Transport::reset( void )
164175
{
165176
if (m_udpsock)

0 commit comments

Comments
 (0)