From f88809a50ab500a1441397c798d729059653ad44 Mon Sep 17 00:00:00 2001 From: Adrien VAN DAMME Date: Thu, 3 Jan 2019 11:20:16 +0100 Subject: [PATCH] commit maj ? --- go01/CreerPartieReseau_serv.cs | 17 +++---- go01/Goban.cs | 2 +- go01/Plateau.cs | 16 +++--- go01/RejoindrePartieReseau_client.cs | 24 ++++----- go01/socketPlateauBase.cs | 75 +++++++++++++++++++++++++++- go01/socketPlateauCLI.cs | 41 ++++++++++++++- go01/socketPlateauEventArgs.cs | 9 ++++ go01/socketPlateauSRV.cs | 45 +++++++++++++---- 8 files changed, 190 insertions(+), 39 deletions(-) diff --git a/go01/CreerPartieReseau_serv.cs b/go01/CreerPartieReseau_serv.cs index 9cfabe1..45cb2d6 100644 --- a/go01/CreerPartieReseau_serv.cs +++ b/go01/CreerPartieReseau_serv.cs @@ -15,14 +15,14 @@ namespace go01 { public partial class CreerPartieReseau_serv : Form { - socketPlateau serveur; + socketPlateauSRV serveur; public CreerPartieReseau_serv() { InitializeComponent(); this.tbUid.Text = Guid.NewGuid().ToString(); this.tbUid.Select(0, 0); - socketPlateau.net_adapters(cbNic); + socketPlateauBase.net_adapters(cbNic); //serveur = new socketPlateau() @@ -37,22 +37,21 @@ namespace go01 var startTimeSpan = TimeSpan.Zero; var periodTimeSpan = TimeSpan.FromSeconds(5); - socketPlateau.ConfigGo_S config = new socketPlateau.ConfigGo_S(tbUid.Text, tbPartName.Text, "", tbNomJoeur.Text, int.Parse(tbTaille.Text), int.Parse(tbPionaAligner.Text), IPAddress.Parse(tbIp.Text), int.Parse(tbPort.Text)); - serveur = new socketPlateau(socketPlateau.TypeSocket_E.Serveur, config, null); + socketPlateauBase.ConfigGo_S config = new socketPlateauBase.ConfigGo_S(tbUid.Text, tbPartName.Text, "", tbNomJoeur.Text, int.Parse(tbTaille.Text), int.Parse(tbPionaAligner.Text), IPAddress.Parse(tbIp.Text), int.Parse(tbPort.Text)); + serveur = new socketPlateauSRV(config, null, new socketPlateauSRV.OnJoinServ(ConnectionEtablie)); serveur.AttenteJoueur(); - serveur.onJoinServ += new socketPlateau.OnJoinServ(JoueurRejoinServeur); } - private void JoueurRejoinServeur(object o, socketPlateauServJoinArgs e) + private void ConnectionEtablie(object o, socketPlateauServJoinArgs e) { Console.WriteLine($"{e.ConfigGo.NomJoeurClient} vs {e.ConfigGo.NomJoeurServeur}"); - serveur.senCmd(new socketPlateau.CommandeSocket_S(socketPlateau.commande_E.Join, new socketPlateauServACKArgs(true))); + serveur.senCmd(new socketPlateauBase.CommandeSocket_S(socketPlateauBase.commande_E.Join, new socketPlateauServACKArgs(true))); Console.WriteLine("CONNETION ETABLEIE"); Plateau p = new Plateau(e.ConfigGo.Taille, e.ConfigGo.PionAligner, e.ConfigGo.NomJoeurServeur, e.ConfigGo.NomJoeurClient); - this.Hide(); + //this.Hide(); p.ShowDialog(); - this.Close(); + //this.Close(); } private void broadcast() { diff --git a/go01/Goban.cs b/go01/Goban.cs index 8f558e8..1bd0965 100644 --- a/go01/Goban.cs +++ b/go01/Goban.cs @@ -115,7 +115,7 @@ namespace go01 GenerationPlateau(false); //redrawAllpb(); } - public Goban(Plateau fenetre, PictureBox conteneur, socketPlateau.ConfigGo_S cfg) : this(fenetre, conteneur, cfg.Taille, cfg.PionAligner, cfg.NomJoeurClient, cfg.NomJoeurServeur) + public Goban(Plateau fenetre, PictureBox conteneur, socketPlateauBase.ConfigGo_S cfg) : this(fenetre, conteneur, cfg.Taille, cfg.PionAligner, cfg.NomJoeurClient, cfg.NomJoeurServeur) {} public Goban(Plateau fenetre, PictureBox conteneur, int dim, int nbPionAlignn, string playerW, string playerB) { diff --git a/go01/Plateau.cs b/go01/Plateau.cs index 80922ec..1351a8e 100644 --- a/go01/Plateau.cs +++ b/go01/Plateau.cs @@ -19,7 +19,7 @@ namespace go01 CouleurVoulue[,] tableauCouleur = new CouleurVoulue[19,19]; List pboxList = new List(); Goban g; - +int dim; int nbPionAlign; string playerW; string playerB; public Plateau(Goban.sauvegarde_S s) { InitializeComponent(); @@ -30,7 +30,11 @@ namespace go01 { InitializeComponent(); this.Text = $"Morpion {dim}x{dim}"; - g = new Goban(this, pictureBox1,dim,nbPionAlign, playerW, playerB); + this.dim = dim; + this.nbPionAlign = nbPionAlign; + this.playerW = playerW; + this.playerB = playerB; + } private void pictureBox1_Click(object sender, EventArgs e) @@ -58,7 +62,7 @@ namespace go01 private void Plateau_Load(object sender, EventArgs e) { //base.OnLoad(e); - + g = new Goban(this, pictureBox1, dim, nbPionAlign, playerW, playerB); this.DoubleBuffered = true; } @@ -163,17 +167,17 @@ namespace go01 private void Plateau_Shown(object sender, EventArgs e) { - + } private void pictureBox1_Paint(object sender, PaintEventArgs e) { - + } private void Plateau_Paint(object sender, PaintEventArgs e) { - + } } } diff --git a/go01/RejoindrePartieReseau_client.cs b/go01/RejoindrePartieReseau_client.cs index c4b352e..ec43321 100644 --- a/go01/RejoindrePartieReseau_client.cs +++ b/go01/RejoindrePartieReseau_client.cs @@ -16,7 +16,7 @@ namespace go01 { public partial class RejoindrePartieReseau_client : Form { - socketPlateau.ConfigGo_S cfg; + socketPlateauBase.ConfigGo_S cfg; Thread ecouteThread; System.Threading.Timer timerTTLdtListPart; private DataTable dtListPart; @@ -61,7 +61,7 @@ namespace go01 dtListPart = new DataTable(); dtListPart.Columns.Add("uid", typeof(string)); - dtListPart.Columns.Add("partName", typeof(socketPlateau.ConfigGo_S)); + dtListPart.Columns.Add("partName", typeof(socketPlateauBase.ConfigGo_S)); dtListPart.Columns.Add("playerName", typeof(string)); dtListPart.Columns.Add("ip", typeof(string)); dtListPart.Columns.Add("port", typeof(int));//timeStamp @@ -93,7 +93,7 @@ namespace go01 byte[] ClientRequestData = Server.Receive(ref ClientEp); using (MemoryStream ms = new MemoryStream(ClientRequestData)) { - socketPlateau.ConfigGo_S cfgGo = (socketPlateau.ConfigGo_S)binaryFormatter.Deserialize(ms); + socketPlateauBase.ConfigGo_S cfgGo = (socketPlateauBase.ConfigGo_S)binaryFormatter.Deserialize(ms); Invoke(new Action(() => { ajoutPartie(cfgGo, ClientEp.Address.ToString()); })); } @@ -104,7 +104,7 @@ namespace go01 } } - public void ajoutPartie(socketPlateau.ConfigGo_S cfgGo, string ip) + public void ajoutPartie(socketPlateauBase.ConfigGo_S cfgGo, string ip) { if(dtListPart.Select($"uid = '{cfgGo.Uid}'").Length==0) @@ -149,7 +149,7 @@ namespace go01 { if (dgvPart.SelectedRows.Count <= 0) return; - cfg = (socketPlateau.ConfigGo_S)dgvPart.SelectedRows[0].Cells["partName"].Value; + cfg = (socketPlateauBase.ConfigGo_S)dgvPart.SelectedRows[0].Cells["partName"].Value; tbIpServ.Text = cfg.IpServeur.ToString(); tbPort.Text = cfg.port.ToString(); } @@ -158,13 +158,13 @@ namespace go01 { if (dgvPart.SelectedRows.Count <= 0) return; - socketPlateau.ConfigGo_S cfg = (socketPlateau.ConfigGo_S)dgvPart.SelectedRows[0].Cells["partName"].Value; + cfg = (socketPlateauBase.ConfigGo_S)dgvPart.SelectedRows[0].Cells["partName"].Value; cfg.NomJoeurClient = tbPlayername.Text; - socketPlateau sc = new socketPlateau(socketPlateau.TypeSocket_E.Client, cfg, null, new socketPlateau.OnJoinACK(SocketACK)); + socketPlateauCLI sc = new socketPlateauCLI(cfg, null, new socketPlateauCLI.OnConnected(SocketCoonected)); /*sc. - sc.senCmd(new socketPlateau.CommandeSocket_S(socketPlateau.commande_E.Join, cfg));*/ + sc.senCmd(new socketPlateauBase.CommandeSocket_S(socketPlateauBase.commande_E.Join, cfg));*/ } - private void SocketACK(object o, socketPlateauServACKArgs e) + private void SocketCoonected(object o, socketPlateauConnected e) { //################################################################################################ //################################################################################################ @@ -172,11 +172,11 @@ namespace go01 //################################################################################################ //################################################################################################ //################################################################################################ - Console.WriteLine("CONNETION ETABLEIE"); + Console.WriteLine("CONNECTION ETABLEIE"); Plateau p = new Plateau(cfg.Taille, cfg.PionAligner, cfg.NomJoeurServeur, cfg.NomJoeurClient); - this.Hide(); + //this.Hide(); p.ShowDialog(); - this.Close(); + //this.Close(); } } } diff --git a/go01/socketPlateauBase.cs b/go01/socketPlateauBase.cs index d4098fc..8685659 100644 --- a/go01/socketPlateauBase.cs +++ b/go01/socketPlateauBase.cs @@ -97,6 +97,9 @@ namespace go01 protected Goban plateauDeJeu; protected ConfigGo_S cfgGo; #endregion + public delegate void OnReceived(object myObject, socketPlateauServReception myArgs); + public event OnReceived onReceived; + public socketPlateauBase(TypeSocket_E socF, ConfigGo_S cfgGo, Goban g) { @@ -108,7 +111,29 @@ namespace go01 } #region commun - protected abstract void InitialiserReception(Socket sArg); + protected void InitialiserReception(Socket sArg) + { + var netStream = new NetworkStream(sArg, true); + var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + CommandeSocket_S cmd = (CommandeSocket_S)binaryFormatter.Deserialize(netStream); + try { onReceived(this, new socketPlateauServReception(cmd)); } catch { } + switch (cmd.commande) + { + case commande_E.Join: + break; + case commande_E.Leave: + break; + case commande_E.Place: + break; + case commande_E.WhoTurn: + break; + case commande_E.Win: + break; + } + Received(cmd); + InitialiserReception(sArg); + } + protected abstract void Received(CommandeSocket_S cmd); public void senCmd(CommandeSocket_S cmd) { var netStream = new NetworkStream(socCli, true); @@ -117,5 +142,53 @@ namespace go01 binaryFormatter.Serialize(netStream, cmd); } #endregion + + #region outil + public static void net_adapters(ComboBox cb) + { + List values = new List(); + foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) + { + cb.Items.Add(nic.Name); + } + } + + public static IPAddress Verifier(string sAdresse) + { + IPAddress rep = null; + if (sAdresse.Trim().Length > 0) + { + IPAddress[] ipVerifs = Dns.GetHostEntry(sAdresse).AddressList; + for (int i = 0; i < ipVerifs.Length; i++) + { + //rep += ipVerifs[0].ToString(); + if (ipVerifs[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) + { + if (ipVerifs[i] != new IPAddress(0X0100007F)) + { + Ping pVerif = new Ping(); + PingReply pRepon = pVerif.Send(ipVerifs[i]); + if (pRepon.Status == IPStatus.Success) + { + rep = ipVerifs[i]; + Console.WriteLine(ipVerifs[i] + "Ping réussi"); + break; + } + else + { + Console.WriteLine(ipVerifs[i] + "Ping KO"); + } + } + } + } + + } + else + { + Console.WriteLine("Renseigner une adresse"); + } + return rep; + } + #endregion } } diff --git a/go01/socketPlateauCLI.cs b/go01/socketPlateauCLI.cs index 566f9ca..9d7b167 100644 --- a/go01/socketPlateauCLI.cs +++ b/go01/socketPlateauCLI.cs @@ -1,18 +1,57 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; +using System.Windows.Forms; namespace go01 { class socketPlateauCLI : socketPlateauBase { + public delegate void OnConnected(object myObject, socketPlateauConnected myArgs); + public event OnConnected onConnected; + public delegate void OnJoinACK(object myObject, socketPlateauServACKArgs myArgs); + public event OnJoinACK onJoinACK; + public socketPlateauCLI(ConfigGo_S cfgGo, Goban g) : base(TypeSocket_E.Client, cfgGo, g) { + try + { + socCli = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + socCli.BeginConnect(new IPEndPoint(cfgGo.IpServeur, port), new AsyncCallback(SurConnection), socCli); + + } + catch (Exception ee) + { + MessageBox.Show("Connexion impossible : " + ee.Message); + } } - protected override void InitialiserReception(Socket sArg) + public socketPlateauCLI(ConfigGo_S cfgGo, Goban g, OnJoinACK onJoinACK) : this(cfgGo, g) + { + this.onJoinACK = onJoinACK; + } + public socketPlateauCLI(ConfigGo_S cfgGo, Goban g, OnConnected onConnected) : this(cfgGo, g) + { + this.onConnected = onConnected; + } + private void SurConnection(IAsyncResult iAR) + { + + Socket sTmp = (Socket)iAR.AsyncState; + if (sTmp.Connected) { + connexionEtablie = true; + try { onConnected(this, new socketPlateauConnected(cfgGo)); } catch { } + senCmd(new CommandeSocket_S(commande_E.Join, cfgGo)); + InitialiserReception(sTmp); + + } + else { MessageBox.Show("Serveur innacessible"); } + + } + protected override void Received(CommandeSocket_S cmd) { throw new NotImplementedException(); } diff --git a/go01/socketPlateauEventArgs.cs b/go01/socketPlateauEventArgs.cs index 63bc263..3912ae5 100644 --- a/go01/socketPlateauEventArgs.cs +++ b/go01/socketPlateauEventArgs.cs @@ -38,4 +38,13 @@ namespace go01 this.cmd = cmd; } } + public class socketPlateauConnected : EventArgs + { + public socketPlateauBase.ConfigGo_S ConfigGo; + + public socketPlateauConnected(socketPlateauBase.ConfigGo_S cfg) + { + this.ConfigGo = cfg; + } + } } diff --git a/go01/socketPlateauSRV.cs b/go01/socketPlateauSRV.cs index fa5dd8e..85a5090 100644 --- a/go01/socketPlateauSRV.cs +++ b/go01/socketPlateauSRV.cs @@ -25,8 +25,8 @@ namespace go01 public event OnJoinACK onJoinACK; public delegate void OnDemandeCo(object myObject, socketPlateauServDemandeCo myArgs); public event OnDemandeCo onDemandeCo; - public delegate void OnReceived(object myObject, socketPlateauServReception myArgs); - public event OnReceived onReceived; + public delegate void OnConnected(object myObject, socketPlateauConnected myArgs); + public event OnConnected onConnected; #endregion public socketPlateauSRV(ConfigGo_S cfgGo, Goban g) : base(TypeSocket_E.Serveur, cfgGo, g) { @@ -34,7 +34,15 @@ namespace go01 } public socketPlateauSRV(ConfigGo_S cfgGo, Goban g, OnJoinACK onJoinACK) : this(cfgGo, g) { - this.onJoinACK += onJoinACK; + this.onJoinACK = onJoinACK; + } + public socketPlateauSRV(ConfigGo_S cfgGo, Goban g, OnConnected onConnected) : this(cfgGo, g) + { + this.onConnected = onConnected; + } + public socketPlateauSRV(ConfigGo_S cfgGo, Goban g, OnJoinServ onJoinServ) : this(cfgGo, g) + { + this.onJoinServ = onJoinServ; } public void AttenteJoueur() { @@ -69,7 +77,14 @@ namespace go01 } private void SurDemandeDeCo(IAsyncResult iAR) { - onDemandeCo(this, new socketPlateauServDemandeCo()); + try + { + onDemandeCo(this, new socketPlateauServDemandeCo()); + } + catch + { + + } if (socFlag == TypeSocket_E.Serveur && !connexionEtablie) { Socket sTmp = (Socket)iAR.AsyncState; @@ -79,6 +94,7 @@ namespace go01 Console.WriteLine($"Connexion effectuée par {((IPEndPoint)socCli.RemoteEndPoint).Address}"); #endif connexionEtablie = true; + try { onConnected(this, new socketPlateauConnected(cfgGo)); } catch { } timerBroadcast.Dispose(); InitialiserReception(socCli); } @@ -90,12 +106,23 @@ namespace go01 socCli.Close(); } } - protected override void InitialiserReception(Socket sArg) + + protected override void Received(CommandeSocket_S cmd) { - var netStream = new NetworkStream(sArg, true); - var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); - CommandeSocket_S cmd = (CommandeSocket_S)binaryFormatter.Deserialize(netStream); - onReceived(this, new socketPlateauServReception(cmd)); + switch (cmd.commande) + { + case commande_E.Join: + try{ onJoinServ(this, new socketPlateauServJoinArgs((ConfigGo_S)cmd.data)); } catch { } + break; + case commande_E.Leave: + break; + case commande_E.Place: + break; + case commande_E.WhoTurn: + break; + case commande_E.Win: + break; + } } } }