// runs in its own thread, started from Connect() void IoThread() { string outputLine = ""; lastMessageSent = DateTime.UtcNow; do { try { ActualBotNick = desiredBotNick; reconnect = false; Logger.Log(LogType.SystemActivity, "Connecting to GemsCraft Global Chat as {2}", hostName, port, ActualBotNick); if (ActualBotNick == "Custom Minecraft Server (GemsCraft)") { Logger.Log(LogType.Error, "You must set a server name to connect to global chat."); reconnect = false; DisconnectThread(); } else { Connect(); } // register Send(IRCCommands.User(ActualBotNick, 8, ConfigKey.ServerName.GetString())); Send(IRCCommands.Nick(ActualBotNick)); while (isConnected && !reconnect) { Thread.Sleep(10); if (localQueue.Count > 0 && DateTime.UtcNow.Subtract(lastMessageSent).TotalMilliseconds >= SendDelay && localQueue.TryDequeue(out outputLine)) { writer.Write(outputLine + "\r\n"); lastMessageSent = DateTime.UtcNow; writer.Flush(); } if (OutputQueue.Count > 0 && DateTime.UtcNow.Subtract(lastMessageSent).TotalMilliseconds >= SendDelay && OutputQueue.TryDequeue(out outputLine)) { writer.Write(outputLine + "\r\n"); lastMessageSent = DateTime.UtcNow; writer.Flush(); } if (client.Client.Available > 0) { string line = reader.ReadLine(); if (line == null) { break; } HandleMessage(line); } } } catch (SocketException) { Logger.Log(LogType.Warning, "GlobalChat: Socket Error. Disconnected. Will retry in {0} seconds.", ReconnectDelay / 1000); reconnect = true; } catch (IOException) { Logger.Log(LogType.Warning, "GlobalChat: IO Error. Disconnected. Will retry in {0} seconds.", ReconnectDelay / 1000); reconnect = true; #if !DEBUG } catch (Exception ex) { Logger.Log(LogType.Error, "GlobalChat: {0}", ex); reconnect = true; #endif } if (reconnect) { Thread.Sleep(ReconnectDelay); } } while (reconnect); }