aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ClientStack/Linden/UDP/LLUDPServer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ClientStack/Linden/UDP/LLUDPServer.cs')
-rw-r--r--OpenSim/Region/ClientStack/Linden/UDP/LLUDPServer.cs197
1 files changed, 148 insertions, 49 deletions
diff --git a/OpenSim/Region/ClientStack/Linden/UDP/LLUDPServer.cs b/OpenSim/Region/ClientStack/Linden/UDP/LLUDPServer.cs
index bd8273d..754d9d2 100644
--- a/OpenSim/Region/ClientStack/Linden/UDP/LLUDPServer.cs
+++ b/OpenSim/Region/ClientStack/Linden/UDP/LLUDPServer.cs
@@ -147,21 +147,34 @@ namespace OpenSim.Region.ClientStack.LindenUDP
147 private int m_elapsed500MSOutgoingPacketHandler; 147 private int m_elapsed500MSOutgoingPacketHandler;
148 148
149 /// <summary>Flag to signal when clients should check for resends</summary> 149 /// <summary>Flag to signal when clients should check for resends</summary>
150 private bool m_resendUnacked; 150 protected bool m_resendUnacked;
151
151 /// <summary>Flag to signal when clients should send ACKs</summary> 152 /// <summary>Flag to signal when clients should send ACKs</summary>
152 private bool m_sendAcks; 153 protected bool m_sendAcks;
154
153 /// <summary>Flag to signal when clients should send pings</summary> 155 /// <summary>Flag to signal when clients should send pings</summary>
154 private bool m_sendPing; 156 protected bool m_sendPing;
155 157
156 private int m_defaultRTO = 0; 158 private int m_defaultRTO = 0;
157 private int m_maxRTO = 0; 159 private int m_maxRTO = 0;
158 160 private int m_ackTimeout = 0;
161 private int m_pausedAckTimeout = 0;
159 private bool m_disableFacelights = false; 162 private bool m_disableFacelights = false;
160 163
161 public Socket Server { get { return null; } } 164 public Socket Server { get { return null; } }
162 165
163 private int m_malformedCount = 0; // Guard against a spamming attack 166 private int m_malformedCount = 0; // Guard against a spamming attack
164 167
168 /// <summary>
169 /// Record current outgoing client for monitoring purposes.
170 /// </summary>
171 private IClientAPI m_currentOutgoingClient;
172
173 /// <summary>
174 /// Recording current incoming client for monitoring purposes.
175 /// </summary>
176 private IClientAPI m_currentIncomingClient;
177
165 public LLUDPServer(IPAddress listenIP, ref uint port, int proxyPortOffsetParm, bool allow_alternate_port, IConfigSource configSource, AgentCircuitManager circuitManager) 178 public LLUDPServer(IPAddress listenIP, ref uint port, int proxyPortOffsetParm, bool allow_alternate_port, IConfigSource configSource, AgentCircuitManager circuitManager)
166 : base(listenIP, (int)port) 179 : base(listenIP, (int)port)
167 { 180 {
@@ -198,11 +211,15 @@ namespace OpenSim.Region.ClientStack.LindenUDP
198 m_defaultRTO = config.GetInt("DefaultRTO", 0); 211 m_defaultRTO = config.GetInt("DefaultRTO", 0);
199 m_maxRTO = config.GetInt("MaxRTO", 0); 212 m_maxRTO = config.GetInt("MaxRTO", 0);
200 m_disableFacelights = config.GetBoolean("DisableFacelights", false); 213 m_disableFacelights = config.GetBoolean("DisableFacelights", false);
214 m_ackTimeout = 1000 * config.GetInt("AckTimeout", 60);
215 m_pausedAckTimeout = 1000 * config.GetInt("PausedAckTimeout", 300);
201 } 216 }
202 else 217 else
203 { 218 {
204 PrimUpdatesPerCallback = 100; 219 PrimUpdatesPerCallback = 100;
205 TextureSendLimit = 20; 220 TextureSendLimit = 20;
221 m_ackTimeout = 1000 * 60; // 1 minute
222 m_pausedAckTimeout = 1000 * 300; // 5 minutes
206 } 223 }
207 224
208 #region BinaryStats 225 #region BinaryStats
@@ -239,19 +256,56 @@ namespace OpenSim.Region.ClientStack.LindenUDP
239 if (m_scene == null) 256 if (m_scene == null)
240 throw new InvalidOperationException("[LLUDPSERVER]: Cannot LLUDPServer.Start() without an IScene reference"); 257 throw new InvalidOperationException("[LLUDPSERVER]: Cannot LLUDPServer.Start() without an IScene reference");
241 258
242 m_log.Info("[LLUDPSERVER]: Starting the LLUDP server in " + (m_asyncPacketHandling ? "asynchronous" : "synchronous") + " mode"); 259 m_log.InfoFormat(
260 "[LLUDPSERVER]: Starting the LLUDP server in {0} mode",
261 m_asyncPacketHandling ? "asynchronous" : "synchronous");
243 262
244 base.Start(m_recvBufferSize, m_asyncPacketHandling); 263 base.Start(m_recvBufferSize, m_asyncPacketHandling);
245 264
246 // Start the packet processing threads 265 // Start the packet processing threads
247 Watchdog.StartThread( 266 Watchdog.StartThread(
248 IncomingPacketHandler, "Incoming Packets (" + m_scene.RegionInfo.RegionName + ")", ThreadPriority.Normal, false, true); 267 IncomingPacketHandler,
268 string.Format("Incoming Packets ({0})", m_scene.RegionInfo.RegionName),
269 ThreadPriority.Normal,
270 false,
271 true,
272 GetWatchdogIncomingAlarmData,
273 Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS);
274
249 Watchdog.StartThread( 275 Watchdog.StartThread(
250 OutgoingPacketHandler, "Outgoing Packets (" + m_scene.RegionInfo.RegionName + ")", ThreadPriority.Normal, false, true); 276 OutgoingPacketHandler,
277 string.Format("Outgoing Packets ({0})", m_scene.RegionInfo.RegionName),
278 ThreadPriority.Normal,
279 false,
280 true,
281 GetWatchdogOutgoingAlarmData,
282 Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS);
251 283
252 m_elapsedMSSinceLastStatReport = Environment.TickCount; 284 m_elapsedMSSinceLastStatReport = Environment.TickCount;
253 } 285 }
254 286
287 /// <summary>
288 /// If the outgoing UDP thread times out, then return client that was being processed to help with debugging.
289 /// </summary>
290 /// <returns></returns>
291 private string GetWatchdogIncomingAlarmData()
292 {
293 return string.Format(
294 "Client is {0}",
295 m_currentIncomingClient != null ? m_currentIncomingClient.Name : "none");
296 }
297
298 /// <summary>
299 /// If the outgoing UDP thread times out, then return client that was being processed to help with debugging.
300 /// </summary>
301 /// <returns></returns>
302 private string GetWatchdogOutgoingAlarmData()
303 {
304 return string.Format(
305 "Client is {0}",
306 m_currentOutgoingClient != null ? m_currentOutgoingClient.Name : "none");
307 }
308
255 public new void Stop() 309 public new void Stop()
256 { 310 {
257 m_log.Info("[LLUDPSERVER]: Shutting down the LLUDP server for " + m_scene.RegionInfo.RegionName); 311 m_log.Info("[LLUDPSERVER]: Shutting down the LLUDP server for " + m_scene.RegionInfo.RegionName);
@@ -485,19 +539,34 @@ namespace OpenSim.Region.ClientStack.LindenUDP
485 SendPacket(udpClient, completePing, ThrottleOutPacketType.Unknown, false, null); 539 SendPacket(udpClient, completePing, ThrottleOutPacketType.Unknown, false, null);
486 } 540 }
487 541
488 public void HandleUnacked(LLUDPClient udpClient) 542 public void HandleUnacked(LLClientView client)
489 { 543 {
544 LLUDPClient udpClient = client.UDPClient;
545
490 if (!udpClient.IsConnected) 546 if (!udpClient.IsConnected)
491 return; 547 return;
492 548
493 // Disconnect an agent if no packets are received for some time 549 // Disconnect an agent if no packets are received for some time
494 //FIXME: Make 60 an .ini setting 550 int timeoutTicks = m_ackTimeout;
495 if ((Environment.TickCount & Int32.MaxValue) - udpClient.TickLastPacketReceived > 1000 * 60) 551
552 // Allow more slack if the client is "paused" eg file upload dialogue is open
553 // Some sort of limit is needed in case the client crashes, loses its network connection
554 // or some other disaster prevents it from sendung the AgentResume
555 if (udpClient.IsPaused)
556 timeoutTicks = m_pausedAckTimeout;
557
558 if (client.IsActive &&
559 (Environment.TickCount & Int32.MaxValue) - udpClient.TickLastPacketReceived > timeoutTicks)
496 { 560 {
497 m_log.Warn("[LLUDPSERVER]: Ack timeout, disconnecting " + udpClient.AgentID); 561 // We must set IsActive synchronously so that we can stop the packet loop reinvoking this method, even
498 StatsManager.SimExtraStats.AddAbnormalClientThreadTermination(); 562 // though it's set later on by LLClientView.Close()
563 client.IsActive = false;
564
565 // Fire this out on a different thread so that we don't hold up outgoing packet processing for
566 // everybody else if this is being called due to an ack timeout.
567 // This is the same as processing as the async process of a logout request.
568 Util.FireAndForget(o => DeactivateClientDueToTimeout(client));
499 569
500 RemoveClient(udpClient);
501 return; 570 return;
502 } 571 }
503 572
@@ -823,7 +892,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP
823 #endregion Ping Check Handling 892 #endregion Ping Check Handling
824 893
825 // Inbox insertion 894 // Inbox insertion
826 packetInbox.Enqueue(new IncomingPacket(udpClient, packet)); 895 packetInbox.Enqueue(new IncomingPacket((LLClientView)client, packet));
827 } 896 }
828 897
829 #region BinaryStats 898 #region BinaryStats
@@ -919,7 +988,9 @@ namespace OpenSim.Region.ClientStack.LindenUDP
919 UDPPacketBuffer buffer = (UDPPacketBuffer)array[0]; 988 UDPPacketBuffer buffer = (UDPPacketBuffer)array[0];
920 UseCircuitCodePacket uccp = (UseCircuitCodePacket)array[1]; 989 UseCircuitCodePacket uccp = (UseCircuitCodePacket)array[1];
921 990
922 m_log.DebugFormat("[LLUDPSERVER]: Handling UseCircuitCode request from {0}", buffer.RemoteEndPoint); 991 m_log.DebugFormat(
992 "[LLUDPSERVER]: Handling UseCircuitCode request for circuit {0} to {1} from IP {2}",
993 uccp.CircuitCode.Code, m_scene.RegionInfo.RegionName, buffer.RemoteEndPoint);
923 994
924 remoteEndPoint = (IPEndPoint)buffer.RemoteEndPoint; 995 remoteEndPoint = (IPEndPoint)buffer.RemoteEndPoint;
925 996
@@ -948,8 +1019,8 @@ namespace OpenSim.Region.ClientStack.LindenUDP
948 { 1019 {
949 // Don't create clients for unauthorized requesters. 1020 // Don't create clients for unauthorized requesters.
950 m_log.WarnFormat( 1021 m_log.WarnFormat(
951 "[LLUDPSERVER]: Connection request for client {0} connecting with unnotified circuit code {1} from {2}", 1022 "[LLUDPSERVER]: Ignoring connection request for {0} to {1} with unknown circuit code {2} from IP {3}",
952 uccp.CircuitCode.ID, uccp.CircuitCode.Code, remoteEndPoint); 1023 uccp.CircuitCode.ID, m_scene.RegionInfo.RegionName, uccp.CircuitCode.Code, remoteEndPoint);
953 } 1024 }
954 1025
955 // m_log.DebugFormat( 1026 // m_log.DebugFormat(
@@ -1047,15 +1118,30 @@ namespace OpenSim.Region.ClientStack.LindenUDP
1047 return client; 1118 return client;
1048 } 1119 }
1049 1120
1050 private void RemoveClient(LLUDPClient udpClient) 1121 /// <summary>
1122 /// Deactivates the client if we don't receive any packets within a certain amount of time (default 60 seconds).
1123 /// </summary>
1124 /// <remarks>
1125 /// If a connection is active then we will always receive packets even if nothing else is happening, due to
1126 /// regular client pings.
1127 /// </remarks>
1128 /// <param name='client'></param>
1129 private void DeactivateClientDueToTimeout(IClientAPI client)
1051 { 1130 {
1052 // Remove this client from the scene 1131 // We must set IsActive synchronously so that we can stop the packet loop reinvoking this method, even
1053 IClientAPI client; 1132 // though it's set later on by LLClientView.Close()
1054 if (m_scene.TryGetClient(udpClient.AgentID, out client)) 1133 client.IsActive = false;
1055 { 1134
1056 client.IsLoggingOut = true; 1135 m_log.WarnFormat(
1057 client.Close(false); 1136 "[LLUDPSERVER]: Ack timeout, disconnecting {0} agent for {1} in {2}",
1058 } 1137 client.SceneAgent.IsChildAgent ? "child" : "root", client.Name, m_scene.RegionInfo.RegionName);
1138
1139 StatsManager.SimExtraStats.AddAbnormalClientThreadTermination();
1140
1141 if (!client.SceneAgent.IsChildAgent)
1142 client.Kick("Simulator logged you out due to connection timeout");
1143
1144 Util.FireAndForget(o => client.Close());
1059 } 1145 }
1060 1146
1061 private void IncomingPacketHandler() 1147 private void IncomingPacketHandler()
@@ -1164,6 +1250,8 @@ namespace OpenSim.Region.ClientStack.LindenUDP
1164 // client. m_packetSent will be set to true if a packet is sent 1250 // client. m_packetSent will be set to true if a packet is sent
1165 m_scene.ForEachClient(clientPacketHandler); 1251 m_scene.ForEachClient(clientPacketHandler);
1166 1252
1253 m_currentOutgoingClient = null;
1254
1167 // If nothing was sent, sleep for the minimum amount of time before a 1255 // If nothing was sent, sleep for the minimum amount of time before a
1168 // token bucket could get more tokens 1256 // token bucket could get more tokens
1169 if (!m_packetSent) 1257 if (!m_packetSent)
@@ -1180,18 +1268,21 @@ namespace OpenSim.Region.ClientStack.LindenUDP
1180 Watchdog.RemoveThread(); 1268 Watchdog.RemoveThread();
1181 } 1269 }
1182 1270
1183 private void ClientOutgoingPacketHandler(IClientAPI client) 1271 protected void ClientOutgoingPacketHandler(IClientAPI client)
1184 { 1272 {
1273 m_currentOutgoingClient = client;
1274
1185 try 1275 try
1186 { 1276 {
1187 if (client is LLClientView) 1277 if (client is LLClientView)
1188 { 1278 {
1189 LLUDPClient udpClient = ((LLClientView)client).UDPClient; 1279 LLClientView llClient = (LLClientView)client;
1280 LLUDPClient udpClient = llClient.UDPClient;
1190 1281
1191 if (udpClient.IsConnected) 1282 if (udpClient.IsConnected)
1192 { 1283 {
1193 if (m_resendUnacked) 1284 if (m_resendUnacked)
1194 HandleUnacked(udpClient); 1285 HandleUnacked(llClient);
1195 1286
1196 if (m_sendAcks) 1287 if (m_sendAcks)
1197 SendAcks(udpClient); 1288 SendAcks(udpClient);
@@ -1207,8 +1298,8 @@ namespace OpenSim.Region.ClientStack.LindenUDP
1207 } 1298 }
1208 catch (Exception ex) 1299 catch (Exception ex)
1209 { 1300 {
1210 m_log.Error("[LLUDPSERVER]: OutgoingPacketHandler iteration for " + client.Name + 1301 m_log.Error(
1211 " threw an exception: " + ex.Message, ex); 1302 string.Format("[LLUDPSERVER]: OutgoingPacketHandler iteration for {0} threw ", client.Name), ex);
1212 } 1303 }
1213 } 1304 }
1214 1305
@@ -1234,11 +1325,14 @@ namespace OpenSim.Region.ClientStack.LindenUDP
1234 { 1325 {
1235 nticks++; 1326 nticks++;
1236 watch1.Start(); 1327 watch1.Start();
1328 m_currentOutgoingClient = client;
1329
1237 try 1330 try
1238 { 1331 {
1239 if (client is LLClientView) 1332 if (client is LLClientView)
1240 { 1333 {
1241 LLUDPClient udpClient = ((LLClientView)client).UDPClient; 1334 LLClientView llClient = (LLClientView)client;
1335 LLUDPClient udpClient = llClient.UDPClient;
1242 1336
1243 if (udpClient.IsConnected) 1337 if (udpClient.IsConnected)
1244 { 1338 {
@@ -1247,7 +1341,7 @@ namespace OpenSim.Region.ClientStack.LindenUDP
1247 nticksUnack++; 1341 nticksUnack++;
1248 watch2.Start(); 1342 watch2.Start();
1249 1343
1250 HandleUnacked(udpClient); 1344 HandleUnacked(llClient);
1251 1345
1252 watch2.Stop(); 1346 watch2.Stop();
1253 avgResendUnackedTicks = (nticksUnack - 1)/(float)nticksUnack * avgResendUnackedTicks + (watch2.ElapsedTicks / (float)nticksUnack); 1347 avgResendUnackedTicks = (nticksUnack - 1)/(float)nticksUnack * avgResendUnackedTicks + (watch2.ElapsedTicks / (float)nticksUnack);
@@ -1318,23 +1412,15 @@ namespace OpenSim.Region.ClientStack.LindenUDP
1318 1412
1319 #endregion 1413 #endregion
1320 1414
1321 private void ProcessInPacket(object state) 1415 private void ProcessInPacket(IncomingPacket incomingPacket)
1322 { 1416 {
1323 IncomingPacket incomingPacket = (IncomingPacket)state;
1324 Packet packet = incomingPacket.Packet; 1417 Packet packet = incomingPacket.Packet;
1325 LLUDPClient udpClient = incomingPacket.Client; 1418 LLClientView client = incomingPacket.Client;
1326 IClientAPI client;
1327 1419
1328 // Sanity check 1420 if (client.IsActive)
1329 if (packet == null || udpClient == null)
1330 { 1421 {
1331 m_log.WarnFormat("[LLUDPSERVER]: Processing a packet with incomplete state. Packet=\"{0}\", UDPClient=\"{1}\"", 1422 m_currentIncomingClient = client;
1332 packet, udpClient);
1333 }
1334 1423
1335 // Make sure this client is still alive
1336 if (m_scene.TryGetClient(udpClient.AgentID, out client))
1337 {
1338 try 1424 try
1339 { 1425 {
1340 // Process this packet 1426 // Process this packet
@@ -1349,21 +1435,34 @@ namespace OpenSim.Region.ClientStack.LindenUDP
1349 catch (Exception e) 1435 catch (Exception e)
1350 { 1436 {
1351 // Don't let a failure in an individual client thread crash the whole sim. 1437 // Don't let a failure in an individual client thread crash the whole sim.
1352 m_log.ErrorFormat("[LLUDPSERVER]: Client packet handler for {0} for packet {1} threw an exception", udpClient.AgentID, packet.Type); 1438 m_log.Error(
1353 m_log.Error(e.Message, e); 1439 string.Format(
1440 "[LLUDPSERVER]: Client packet handler for {0} for packet {1} threw ",
1441 client.Name, packet.Type),
1442 e);
1443 }
1444 finally
1445 {
1446 m_currentIncomingClient = null;
1354 } 1447 }
1355 } 1448 }
1356 else 1449 else
1357 { 1450 {
1358 m_log.DebugFormat("[LLUDPSERVER]: Dropping incoming {0} packet for dead client {1}", packet.Type, udpClient.AgentID); 1451 m_log.DebugFormat(
1452 "[LLUDPSERVER]: Dropped incoming {0} for dead client {1} in {2}",
1453 packet.Type, client.Name, m_scene.RegionInfo.RegionName);
1359 } 1454 }
1360 } 1455 }
1361 1456
1362 protected void LogoutHandler(IClientAPI client) 1457 protected void LogoutHandler(IClientAPI client)
1363 { 1458 {
1364 client.SendLogoutPacket(); 1459 client.SendLogoutPacket();
1365 if (client.IsActive) 1460
1366 RemoveClient(((LLClientView)client).UDPClient); 1461 if (!client.IsLoggingOut)
1462 {
1463 client.IsLoggingOut = true;
1464 client.Close();
1465 }
1367 } 1466 }
1368 } 1467 }
1369} 1468}