} //restituisce una lista di interi di tutti gli ID all'interno della propria lobby public void Check_Connection(string Richiedente, Socket User) { int I = 0; for (I = 0; I < lobby.Count; I++) { if (lobby[I].User == User) { break; } } if (I == lobby.Count) { Debug.LogError("Impossibile trovare l'User " + (IPEndPoint)lobby[I].User.RemoteEndPoint); return; } try { send.Send_to_One(Richiedente, send.Lobby_Ping(), User, "Check connection failed"); } catch (Exception e) { Debug.LogError("Tentativo di check con il client fallito " + (IPEndPoint)User.RemoteEndPoint + " imposto offline e chiudo il socket."); Set_Online_by_ID(lobby[I].ID, false); } } //effettua un controllo di connessione con un socket, nel caso lo elimina dalla lista
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"); } } } }
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; } } }
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