Beispiel #1
0
    void FirstContact()
    {
        string Mex;



        //Primo Contatto
        Pacchetto.Destinatario = Servente;
        Pacchetto.Clear();


        try
        {
            Mex = Pacchetto.First_Contact(1, false).ToString();
            Pacchetto.Send_Packet("Client", "Errore durante invio first Contact");

            Pacchetto.Clear();
            Pacchetto.Receive_Packet("Client", 1);
            if (Pacchetto.Get_Header())
            {
                if (Pacchetto.Header == 0)
                {
                    Mex = Pacchetto.First_Contact(0, true).ToString();
                    switch (Mex)
                    {
                    case "3":
                        settings.Console_Write("Il server ha rifiutato la tua connessione causa codice", false);
                        Shutdown_Client("Client => FirstContact (ByServerRefuse)");
                        return;

                    case "4":
                        settings.Console_Write("Benvenuto", false);
                        break;

                    default:
                        settings.Error_Profiler("N015", 0, "(Client)Codice First contact errato", 3, false);
                        Shutdown_Client("Client => FirstContact (ByWrongBodyCode)");
                        return;
                    }
                }
            }
            else
            {
                settings.Error_Profiler("N014", 0, "(Client)Errore nel Header:" + Name + " > Test di invio", 3, false);
                Shutdown_Client("Client => FirstContact (ByError)");
            }
            if (!Pacchetto.Risultato)
            {
                settings.Error_Profiler("N011", 0, "(Client)Errore nell'invio del pacchetto:" + Name + " > Test di invio", 3, false);
                Shutdown_Client("Client => FirstContact (ByError)");
            }
        }
        catch (Exception e)
        {
            if (Shutdown)
            {
                if (D)
                {
                    settings.Console_Write("Client non più in comunicazione (Inizialize_Client)", false);
                }
            }
            else
            {
                settings.Error_Profiler("N010", 0, "Errore nell'inizializzazione\n" + e, 2, false);
                Shutdown_Client("Client => FirstContact");
            }
        }



        return;


        //OLD

        try
        {
            Pacchetto.Set_Header(1, "Errore durante il primo contatto");
            Mex = "";

            Mex = Send.Receive_by_one(Servente, 1, Name);

            settings.Console_Write("Messaggio ricevuto dal client: " + Mex, false);

            Shutdown_Client("Fine comunicazione");

            if (Int32.Parse(Mex) == 0)
            {
                Debug.LogError("Ti è stato negato l'accesso al server causa Nome o motivazione");
                Shutdown_Client("Client => FirstContact");
            }
        }
        catch (Exception e)
        {
            if (Shutdown)
            {
                if (D)
                {
                    settings.Console_Write("Client non più in comunicazione (Username)", false);
                }
            }
            else
            {
                settings.Error_Profiler("N010", 0, "Errore nel passaggio dell'username\n" + e, 2, false);
                Shutdown_Client("Client => FirstContact");
            }
        }

        if (!Shutdown)
        {
            //richiesta di aggiornamento lobby
            try
            {
                List <int> IDlist     = gameObject.GetComponent <Lobby>().List_of_ID();
                List <int> OnlineList = new List <int>();
                for (int I = 0; I < IDlist.Count; I++)
                {
                    OnlineList.Add(gameObject.GetComponent <Lobby>().Check_Online_by_ID(IDlist[I]));
                }
                Send.Send_to_One(Name, Send.Refresh_Lobby(IDlist, OnlineList), Servente, "Errore durante la richiesta di refresh della lobby");
            }
            catch (Exception e)
            {
                if (Shutdown)
                {
                    if (D)
                    {
                        settings.Console_Write("Client non più in comunicazione (Aggiornamento Lobby)", false);
                    }
                }
                else
                {
                    settings.Error_Profiler("D001", 0, "Errore nella richiesta di aggiornamento della lobby \n" + e, 2, false);
                    Shutdown_Client("Client => FirstContact");
                }
            }
        }
    }
Beispiel #2
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;
            }
        }
    }