Example #1
0
    public void SubThr(object TempSocket)
    {
        Actions     action = new Actions();
        SendActions Send   = new SendActions();

        action.AsServer = true;
        action.Send     = Send;
        action.lobby    = lobby;
        Send.lobby      = lobby;
        Socket client = (Socket)TempSocket;

        SocketList.Add(client);

        string     Name     = null;                                         //
        string     Mex      = null;                                         //
        IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;            //IP del client

        client.NoDelay           = true;                                    //imposto che invia sempre in base alla dimensione che riceve senza dover riempire il pacchetto
        client.ReceiveBufferSize = action.BufferSize;                       //imposto la dimensione massima del buffersize per action
        action.lobby             = lobby;
        Send.BufferSize          = action.BufferSize;                       //Imposto la dimesione massima del buffersize per Send
        Send.lobby = lobby;

        Packets Pacchetto = new Packets();

        Pacchetto.Inizialize_Packet(D, true, settings, lobby, "Server ==> (" + clientep.ToString() + ")");


        if (D)
        {
            settings.Console_Write("Connesso con: " + clientep, false);
        }



        // Primo contatto
        try
        {
            Pacchetto.Destinatario = client;
            Pacchetto.Clear();



            Pacchetto.Receive_Packet("Server", 1);
            Pacchetto.Get_Header();
            if (Pacchetto.Header == 0 && Pacchetto.Risultato)
            {
                int T = Pacchetto.Fetch_Header(Pacchetto.Header);
                Pacchetto.Receive_Packet("Server", T);
                if (!Pacchetto.Risultato)
                {
                    settings.Console_Write("Server > Errore di comunicazione (FIrstCOntact).", false);
                }
                T   = Pacchetto.First_Contact(255, true);
                Mex = T.ToString();
            }
            else
            {
                settings.Console_Write("Server > Errore di comunicazione (FIrstCOntact). pt2", false);
            }
            Pacchetto.Clear();
            //Mex =  Send.Receive_by_one(client, 1, "Server");
            switch (Mex)
            {
            case "1":
                settings.Console_Write("Server > Client connesso come giocatore. " + Mex, false);
                Pacchetto.First_Contact(4, false);
                Pacchetto.Send_Packet("Server", "Errore durante la risposta al FirstCOntact per vero");
                break;

            case "2":
                settings.Console_Write("Server > Client connesso per invio dati. " + Mex, false);
                Pacchetto.First_Contact(5, false);
                Pacchetto.Send_Packet("Server", "Errore durante la risposta al FirstCOntact per falso");
                break;

            default:
                settings.Error_Profiler("N014", 0, "Codice header non consono. Codice ricevuto:" + Mex, 2, false);
                Pacchetto.First_Contact(5, false);
                Pacchetto.Send_Packet("Server", "Errore durante la risposta al FirstCOntact per falso");
                break;
            }
            Close_Single_ConnectedClient(client);
            return;
        }
        catch (Exception e)
        {
            if (Shutdown)
            {
                if (D)
                {
                    settings.Console_Write("connessione con " + Name + "  (" + clientep.ToString() + ") chiusa.", false);
                }
            }
            else
            {
                settings.Error_Profiler("N012", 0, "(" + Name + " || " + clientep.ToString() + ") (Server => SubThr) : " + e, 1, false);
                Send.Server_Broadcast(Send.Player_Come_Online(lobby.Retrive_ID_by_Name(Name), false));
                lobby.Set_Online_by_ID(lobby.Retrive_ID_by_Name(Name), false);
                Close_Single_ConnectedClient(client);
            }
        }



        // fine primo contatto


        if (!Shutdown)
        {
            try
            {
                if (D == true)
                {
                    settings.Console_Write("Connesso con: " + clientep, false);
                }

                //richiedo il motivo di connessione e nel caso l'username
                string[] ConnetionMotivation = Send.Receive_by_one(client, 1, "Server").Split('#');
                switch (Int32.Parse(ConnetionMotivation[0]))          //controllo se si connette per inviare immagini
                {
                case 0:
                    break;

                case 1:
                    TransferChannel();
                    return;

                default:
                    Send.Send_to_One("Server", "0", client, "Errore nella della motivazione di connessione");
                    return;
                }



                Name = ConnetionMotivation[1];
                if (!lobby.Check_Exist_by_Name(Name))
                {
                    lobby.Add_ServerPlayer(client, Name, 0);
                }
                else
                {
                    if (lobby.Check_Online_by_ID(lobby.Retrive_ID_by_Name(Name)) != 0)
                    {
                        Debug.LogError("Esiste già un client connesso con il nome: " + Name + " chiusura connessione");

                        Send.Send_to_One("Server", "0", client, "Errore nella comunicazione dell'errato nome");
                        client.Close();
                        return;
                    }
                    else
                    {
                        lobby.Set_Online_by_ID(lobby.Retrive_ID_by_Name(Name), true);
                        lobby.Set_User_by_ID(lobby.Retrive_ID_by_Name(Name), client);
                        Send.Server_Broadcast(Send.Player_Come_Online(lobby.Retrive_ID_by_Name(Name), true));
                    }
                }


                //avverto che l'username è stato accettato
                Send.Send_to_One("Server", "1", client, "Errore nella comunicazione della genuinità del nome");

                //aggiorno tutti sul nuovo host connesso
                Send.Server_Broadcast(Send.Client_Player_Login_Inizialize(lobby.Retrive_ID_by_Name(Name), 0, 1, Name));
                while (true)
                {
                    Mex = Send.Receive_by_one(client, 1, "Server");

                    if (D)
                    {
                        Debug.Log("S_Ricevuto: " + Mex);
                    }
                    Actions TempActions = new Actions
                    {
                        User     = client,
                        Ricevuto = Mex,
                        contesto = 0,
                        lobby    = lobby,
                        AsServer = true,
                    };
                    Thread newThread = new Thread(new ParameterizedThreadStart(TempActions.Run));
                    newThread.Start(client);
                }
                if (D == true)
                {
                    Debug.Log("il Client si è disconnesso");
                }
                lobby.Set_Online_by_ID(lobby.Retrive_ID_by_Name(Name), false);
                client.Close();
            }
            catch (Exception e)
            {
                Debug.LogError("Il socket " + clientep + " si è disconnesso a causa di un errore\n" + e);
                Send.Server_Broadcast(Send.Player_Come_Online(lobby.Retrive_ID_by_Name(Name), false));
                lobby.Set_Online_by_ID(lobby.Retrive_ID_by_Name(Name), false);
                client.Close();
                return;
            }
        }
    }
Example #2
0
    public void Refresh_Lobby(string Action)
    {
        bool Deb = false;
        int  numero;

        if (Deb)
        {
            Debug.Log("Entrato in Refresh Lobby");
        }
        bool result;

        string[] temp = Action.Split('#');
        int      I    = 0;

        if (Deb)
        {
            Debug.Log("Numero di elementi ricevuti: " + temp.Length);
        }
        if (string.Equals(temp[0], ""))
        {
            I++;
        }
        for (I = 0; I < temp.Length; I++)
        {
            if (Deb)
            {
                Debug.Log("Controllo se l'elemento è online : " + temp[I]);
            }
            result = Int32.TryParse(temp[I], out numero);
            if (result)
            {
                if (!lobby.Check_Exist_by_ID(numero))
                {
                    Send.Send_to_One("Server", Send.Player_Come_Online(Int32.Parse(temp[I]), false), User, "Errore durante di comunicazione con " + lobby.Retrive_Name_by_Socket(User));
                }
            }
        }
        if (Deb)
        {
            Debug.Log("Inizio controllo mancanze...");
        }
        List <int> IDList = lobby.List_of_ID();

        if (Deb)
        {
            Debug.Log("Elementi atttualmente connessi: " + IDList.Count);
        }
        bool[] IDExist = new bool[IDList.Count];
        for (I = 0; I < IDExist.Length; I++)
        {
            IDExist[I] = false;
        }
        for (I = 0; I < IDList.Count; I++)
        {
            if (Deb)
            {
                Debug.Log("Controllo esistenza ID: " + IDList[I]);
            }
            for (int J = 0; J < temp.Length; J++)
            {
                result = Int32.TryParse(temp[J], out numero);
                if (result)
                {
                    if (numero == IDList[I])
                    {
                        if (Deb)
                        {
                            Debug.Log("Trovato");
                        }
                        IDExist[I] = true;
                    }
                }
            }
            if (!IDExist[I])
            {
                if (Deb)
                {
                    Debug.Log("Non trovato, invio dati...");
                }
                Send.Send_to_One("Server", Send.Client_Player_Login_Inizialize(IDList[I], lobby.Retrive_Power_by_ID(IDList[I]), lobby.Check_Online_by_ID(IDList[I]), lobby.Retrive_Name_by_ID(IDList[I])), User, "Errore durante l'aggiornamento della lobby di " + lobby.Retrive_Name_by_Socket(User));
            }
        }
    }                                                   //viene inviata la lista degli ID attualmente connessi dal client, il server riceve e esegue il codice che controlla tutti gli id che comunica tutte le differenze tramite i comandi appositi formato di ricezione: RefL1#4#5#12