Example #1
0
    }                                                   //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

    // Lobby
    private void Lobby_Change_Role(string Action)
    {
        if (AsServer)
        {
            string utente = lobby.Retrive_Name_by_Socket(User);
            Debug.Log("cambio di potere di " + utente + " da " + lobby.Retrive_Power_by_Name(utente) + " a " + Action);
            lobby.Set_Power_by_ID(lobby.Retrive_ID_by_Socket(User), Int32.Parse(Action));
            Send.Server_Broadcast(Send.Lobby_Change_Role(lobby.Retrive_ID_by_Name(utente), lobby.Retrive_Power_by_Name(utente)));
        }
        else
        {
            string[] azioni = Action.Split('#');
            Debug.Log("cambio di potere di " + lobby.Retrive_Name_by_ID(Int32.Parse(azioni[0])) + " da " + lobby.Retrive_Power_by_Name(lobby.Retrive_Name_by_ID(Int32.Parse(azioni[0]))) + " a " + azioni[1]);
            lobby.Set_Power_by_ID(Int32.Parse(azioni[0]), Int32.Parse(azioni[1]));
        }
    }
Example #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;
            }
        }
    }