Пример #1
0
 private static void send_client(string message, Client user)
 {
     try
     {
         user.context.Send(message);
     }
     catch (Exception e)
     {
         ConsoleWrite(e.ToString());
     }
 }
Пример #2
0
        private static void OnDisconnect(UserContext context)
        {
            #if(DEBUG)
            ConsoleWrite("Client Disconnected : " + context.ClientAddress);
            #endif
            Client user = null;
            foreach (Client kvp in Clients)
                if (kvp.context.ClientAddress == context.ClientAddress)
                    user = kvp;

            if (user == null)
                return;

            if (Client_console != null && user == Client_console)
                Client_console = null;

            Clients.Remove(user);
        }
Пример #3
0
        private static void OnReceive(UserContext context)
        {
            string json = string.Empty;
            dynamic obj = null;

            try
            {
                json = context.DataFrame.ToString();
                if(json == string.Empty || ! json.Contains("}"))
                    return;

                obj = JsonConvert.DeserializeObject(json);
                if (obj == null)
                    return;
            }
            catch (Exception e)
            {
            #if(DEBUG)
                ConsoleWrite(e.ToString());
            #endif
                return;
            }
            #if(DEBUG)
            ConsoleWrite("Received Data : " + json);
            #endif
            Client client = getClient(context.ClientAddress);

            if (client == null)
                return;

            if (client.user == null && (int)obj.comm != (int)Command.CHANGE_NAME)
            {
                client.context.Dispose();
                return;
            }

            double unixTime = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds;

            switch ((int)obj.comm)
            {
                case (int)Command.MESSAGE:
                    if((string)obj.data.message == "console" && client.user.id == 1 && Client_console == null)
                    {
                        Client_console = client;
                        break;
                    }
                    if (Client_console != null && client == Client_console)
                    {
                        ConsoleSend((string)obj.data.message);
                        break;
                    }
                    if (unixTime - client.last_rec < 250)
                    {
                        if (++client.user.warnings > 3)
                        {
                            Data data;
                            if (++client.user.errors > 3)
                            {
                                context.Dispose();
                                return;
                            }
                            else
                            {
                                data = new Data { comm = Command.MUTE, data = ((client.user.errors) * 10).ToString() };
                                send_client(JsonConvert.SerializeObject(data), client);
                                client.user.warnings = 0;
                            }
                        }
                    }
                    else
                        client.user.warnings = 0;

                    client.last_rec = unixTime;
                    send_clients(JsonConvert.SerializeObject(obj));
                    break;
                case (int)Command.MESSAGE_STATUS:
                    send_clients(JsonConvert.SerializeObject(obj), UInt16.Parse((string)obj.data.id));
                    break;
                case (int)Command.PING:
                    unixTime = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds;
                    double unixLastTime = double.Parse((string)obj.data);
                    client.ping = Convert.ToUInt16(unixTime - unixLastTime);
                    break;
                case (int)Command.CHANGE_NAME:
                    client.user = new User((string)obj.data.name, (ushort)obj.data.id);
                    break;
            }
        }
Пример #4
0
        private static void OnConnect(UserContext context)
        {
            #if(DEBUG)
            ConsoleWrite("Client Connection From : " + context.ClientAddress);
            #endif

            Client me = new Client { context = context };
            Clients.Add(me);
        }