fCraft.IRC.IRCThread.IoThread C# (CSharp) Method

IoThread() private method

private IoThread ( ) : void
return void
            private void IoThread() {
                lastMessageSent = DateTime.UtcNow;

                do {
                    try {
                        ActualBotNick = desiredBotNick;
                        reconnect = false;
                        Logger.Log( LogType.IRC,
                                    "Connecting to {0}:{1} as {2}",
                                    hostName, port, ActualBotNick );
                        Connect();

                        // register
                        Send( IRCCommands.User( ActualBotNick, 8, ConfigKey.ServerName.GetString() ) );
                        Send( IRCCommands.Nick( ActualBotNick ) );
                        nickTry = 0;

                        while ( isConnected && !reconnect ) {
                            Thread.Sleep( 10 );

                            string outputLine;
                            if ( DateTime.UtcNow.Subtract( lastMessageSent ).TotalMilliseconds >= SendDelay &&
                                localQueue.TryDequeue( out outputLine ) ) {
#if DEBUG_IRC
                                Logger.Log( LogType.IRC, "[Out.Local] {0}", outputLine );
#endif
                                writer.Write( outputLine + "\r\n" );
                                lastMessageSent = DateTime.UtcNow;
                                writer.Flush();
                            }

                            if ( DateTime.UtcNow.Subtract( lastMessageSent ).TotalMilliseconds >= SendDelay &&
                                OutputQueue.TryDequeue( out outputLine ) ) {
#if DEBUG_IRC
                                Logger.Log( LogType.IRC, "[Out.Global] {0}", outputLine );
#endif
                                writer.Write( outputLine + "\r\n" );
                                lastMessageSent = DateTime.UtcNow;
                                writer.Flush();
                            }

                            if ( client.Client.Available > 0 ) {
                                string line = reader.ReadLine();
                                if ( line == null ) {
                                    reconnect = true;
                                    break;
                                }
                                HandleMessage( line );
                            }
                        }
                    } catch ( SocketException ) {
                        Logger.Log( LogType.Warning, "IRC: Disconnected. Will retry in {0} seconds.",
                                    ReconnectDelay / 1000 );
                        reconnect = true;
                    } catch ( IOException ) {
                        Logger.Log( LogType.Warning, "IRC: Disconnected. Will retry in {0} seconds.",
                                    ReconnectDelay / 1000 );
                        reconnect = true;
#if !DEBUG
                    } catch ( Exception ex ) {
                        Logger.Log( LogType.Error, "IRC: {0}", ex );
                        reconnect = true;
#endif
                    }

                    if ( reconnect )
                        Thread.Sleep( ReconnectDelay );
                } while ( reconnect );
            }