Пример #1
0
 private void ProcessString(string text, byte[] raw_data, IPEndPoint ip)
 {
     log.Debug(text);
     SendPayload toSend   = recieveData?.Invoke(text, raw_data, ip);
     Task        sendTask = Task.Factory.StartNew((object dataSend) =>
     {
         SendPayload payload = dataSend as SendPayload;
         byte[] data_b       = Encoding.ASCII.GetBytes(payload.data);
         server_login.Send(data_b, data_b.Length, payload.client.client_ip);
     }, toSend);
 }
Пример #2
0
        private SendPayload Server_recieveData(string data, byte[] raw_data, IPEndPoint ip)
        {
            //Search with LINQ for matching IP+socket
            Client      client  = clients.FirstOrDefault(i => i.client_ip.Equals(ip));
            SendPayload payload = new SendPayload();

            payload.client = client;
            if (client == null)
            {
                client = new Client(this, ip);
                client.ClientStatusChanged += Client_ClientStatusChanged;
                payload.client              = client;
                if (clients.Count == maxClients)
                {
                    payload.data = ServerPhrases.SERVER_FULL;
                    return(payload);
                }
                if (bannedEndPoints.Contains(ip))
                {
                    payload.data = ServerPhrases.BANNED;
                    return(payload);
                }
                else
                {
                    clients.Add(client);
                    RaiseEventOnUIThread(ClientConnected, this, EventArgs.Empty);
                }
            }
            client.resetTimer();
            switch (client.status)
            {
            case Client.ClientStatus.CONNECTING:
                if (data.StartsWith("login "))
                {
                    client.sentName(data.Substring(data.IndexOf(' ')));
                    if (requirePass)
                    {
                        client.status = Client.ClientStatus.PASSWORD_REQUESTED;
                        payload.data  = ServerPhrases.PASS_REQ;
                        return(payload);
                    }
                    else
                    {
                        client.status = Client.ClientStatus.LOGGED_IN;
                        payload.data  = ServerPhrases.LOGIN_SUCCESSFUL;

                        return(payload);
                    }
                }
                else
                {
                    log.Warning("Client with IP " + ip + " not using standard Protocol!");
                    bannedEndPoints.Add(ip);
                    clients.Remove(client);
                }
                break;

            case Client.ClientStatus.PASSWORD_REQUESTED:
                if (data.StartsWith("password "))
                {
                    string passwordSent = data.Substring(data.IndexOf(' '));
                    if (password != this.password)
                    {
                        client.status = Client.ClientStatus.PASSWORD_REQUESTED;
                        payload.data  = ServerPhrases.PASS_REQ;
                        return(payload);
                    }
                    else
                    {
                        client.status = Client.ClientStatus.LOGGED_IN;
                        payload.data  = ServerPhrases.LOGIN_SUCCESSFUL;
                        return(payload);
                    }
                }
                else
                {
                    log.Warning("Client with IP " + ip + " not using standard Protocol!");
                    bannedEndPoints.Add(ip);
                    clients.Remove(client);
                }
                break;

            case Client.ClientStatus.LOGGED_IN:
                client.input.analyzeMessage(data, raw_data);
                break;
            }
            payload.data = "" + ((char)3);
            return(payload);
        }