Ejemplo n.º 1
0
        private void Work()
        {
            Console.WriteLine("Work thread begun");

            while (running)
            {
                if (server.Pending())
                {
                    IRCClient cl = new IRCClient(server.AcceptTcpClient());

                    clients.Add(cl);
                    Console.WriteLine("Recieved connection from {0}", clients[clients.Count - 1].HostName);
                }
                else if (clientList.NeedsToBeSaved)
                {
                    foreach (KeyValuePair <ulong, SteamClient> c in clientList.Clients)
                    {
                        if (c.Value.HasMessage())
                        {
                            IMessage m = c.Value.GetMessage();
                            Server.IRCCommands.CallCommand(m);
                        }
                    }

                    clientList.Save("clients.list");
                }
                else
                {
                    IClient[] clientDupe = clients.ToArray();
                    foreach (IClient c in clientDupe)
                    {
                        if (c == null)
                        {
                            continue;
                        }

                        if (c.IsDisposed)
                        {
                            Console.WriteLine("Client {0} disconnected: {1}", c.UserString, c.DisconnectMsg);
                            ClientHelpers.PartClient(c, c.DisconnectMsg);
                            clients.Remove(c);
                            continue;
                        }

                        if (c.HasMessage())
                        {
                            IMessage m = c.GetMessage();

                            if (m.IsCommand && IRCCommands.HasCommand(m.Command))
                            {
                                if (IRCCommands.ValidCommand(m.Command, m.Params.Length))
                                {
                                    IRCCommands.CallCommand(m);
                                    c.LastCommand = DateTime.Now;
                                }
                                else
                                {
                                    Console.WriteLine("Invalid command call: \"{0}\"", m.MessageString);
                                    c.SendMessage(IRCMessage.GetStatic().CreateMessage(c, c.NickName, Reply.ERR_UNKNOWNCOMMAND, new string[] { c.NickName, m.Command, "Wrong parameters" }));
                                }
                            }
                            else
                            {
                                Console.WriteLine("Unknown command: \"{0}\"", m.MessageString);
                                c.SendMessage(IRCMessage.GetStatic().CreateMessage(c, c.NickName, Reply.ERR_UNKNOWNCOMMAND, new string[] { c.NickName, m.Command, "Unknown command" }));
                            }
                        }

                        if (!c.Greeted && c.NickName != "*" && c.ClientType == ClientType.TYP_CLIENT)
                        {
                            ClientHelpers.MeetAndGreet(c);
                        }
                        else if (!c.Greeted && (DateTime.Now - c.LastCommand).TotalSeconds > 15)
                        {
                            c.Dispose("Never finished handshake");
                        }
                        else if (c.Greeted && (DateTime.Now - c.LastPing).TotalSeconds > 30)
                        {
                            c.LastPing = DateTime.Now;
                            c.SendMessage(IRCMessage.GetStatic().CreateMessage(c, null, "PING", new string[] { pingRandom.Next().ToString() }));
                            c.MissedPings++;
                        }
                        else if (c.Greeted && c.MissedPings > 5)
                        {
                            c.Dispose("Ping Timeout!");
                        }
                    }
                }

                Thread.Sleep(5);
            }

            Console.WriteLine("Work thread finished");
        }