From 38934283ec2f5bd1dfac0653cc9b0d7223e91a16 Mon Sep 17 00:00:00 2001 From: Adrien VAN DAMME Date: Mon, 17 Dec 2018 15:13:05 +0100 Subject: [PATCH] Separation socketPlateau en Base,SRV,CLI --- go01/CreerPartieReseau_serv.cs | 8 +- go01/Goban.cs | 42 +++--- go01/Program.cs | 5 + go01/RejoindrePartieReseau_client.cs | 22 +++- go01/go01.csproj | 7 +- ...ketPlateau.cs => socketPlateau - Copie.cs} | 20 ++- go01/socketPlateauBase.cs | 121 ++++++++++++++++++ go01/socketPlateauCLI.cs | 20 +++ go01/socketPlateauEventArgs.cs | 41 ++++++ go01/socketPlateauInterface.cs | 14 ++ go01/socketPlateauSRV.cs | 101 +++++++++++++++ go01/socketPlateauServArgs.cs | 17 --- 12 files changed, 371 insertions(+), 47 deletions(-) rename go01/{socketPlateau.cs => socketPlateau - Copie.cs} (94%) create mode 100644 go01/socketPlateauBase.cs create mode 100644 go01/socketPlateauCLI.cs create mode 100644 go01/socketPlateauEventArgs.cs create mode 100644 go01/socketPlateauInterface.cs create mode 100644 go01/socketPlateauSRV.cs delete mode 100644 go01/socketPlateauServArgs.cs diff --git a/go01/CreerPartieReseau_serv.cs b/go01/CreerPartieReseau_serv.cs index 96eec79..9cfabe1 100644 --- a/go01/CreerPartieReseau_serv.cs +++ b/go01/CreerPartieReseau_serv.cs @@ -44,9 +44,15 @@ namespace go01 } - private void JoueurRejoinServeur(object o, socketPlateauServArgs e) + private void JoueurRejoinServeur(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))); + Console.WriteLine("CONNETION ETABLEIE"); + Plateau p = new Plateau(e.ConfigGo.Taille, e.ConfigGo.PionAligner, e.ConfigGo.NomJoeurServeur, e.ConfigGo.NomJoeurClient); + this.Hide(); + p.ShowDialog(); + this.Close(); } private void broadcast() { diff --git a/go01/Goban.cs b/go01/Goban.cs index 320403d..8f558e8 100644 --- a/go01/Goban.cs +++ b/go01/Goban.cs @@ -12,6 +12,10 @@ namespace go01 { public class Goban { + + + + public PictureBox Conteneur; int nombreDePionAAligne; int ptBlanc = 0, ptNoir = 0; @@ -22,6 +26,9 @@ namespace go01 public string PlayerTurn { get { return this.LabelPlayerTurn.Text; } } string playerWName, playerBName; List pboxList = new List(); + public enum Occupant_E { Vide, Blanc, Noir, LAST } + public enum Joueur_E { Blanc, Noir, LAST } + Joueur_E tour; Joueur_E Tour { @@ -50,14 +57,14 @@ namespace go01 [Flags] enum Deplacement_E { - SG = 0b00000001, - S = 0b00000010, - SD = 0b00000100, - G = 0b00001000, - D = 0b00010000, - IG = 0b00100000, - I = 0b01000000, - ID = 0b10000000 + SG = 0b00000001, + S = 0b00000010, + SD = 0b00000100, + G = 0b00001000, + D = 0b00010000, + IG = 0b00100000, + I = 0b01000000, + ID = 0b10000000 } [Serializable] public struct sauvegarde_S @@ -70,12 +77,12 @@ namespace go01 public Goban.Joueur_E tour; public sauvegarde_S(string nomBlanc, string nomNoir, int pointBlanc, int pointNoir, int nbAlignementToWin, int dimGoban, Goban.Occupant_E[,] tableauOccupation, Goban.Joueur_E tour, bool win) { - this.nomBlanc = nomBlanc; this.nomNoir=nomNoir; - this.ptBlanc = pointBlanc; this.ptNoir = pointNoir; - this.dimGoban = dimGoban; this.nbAlignementToWin = nbAlignementToWin; - this.win = win; - this.tableauOccupation = tableauOccupation; - this.tour = tour; + this.nomBlanc = nomBlanc; this.nomNoir = nomNoir; + this.ptBlanc = pointBlanc; this.ptNoir = pointNoir; + this.dimGoban = dimGoban; this.nbAlignementToWin = nbAlignementToWin; + this.win = win; + this.tableauOccupation = tableauOccupation; + this.tour = tour; } } struct carrefour_S @@ -88,11 +95,10 @@ namespace go01 SensDeDeplacment = sens; } } - public enum Occupant_E { Vide, Blanc, Noir, LAST} - public enum Joueur_E { Blanc, Noir, LAST} - public Goban(Plateau fenetre, PictureBox conteneur, sauvegarde_S s) + + public Goban(Plateau fenetre, PictureBox conteneur, sauvegarde_S s) { tableauOccupation = s.tableauOccupation; playerBName = s.nomNoir; @@ -109,6 +115,8 @@ 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, int dim, int nbPionAlignn, string playerW, string playerB) { int x = 0; diff --git a/go01/Program.cs b/go01/Program.cs index 5dde9c8..dd132f2 100644 --- a/go01/Program.cs +++ b/go01/Program.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; @@ -16,9 +17,13 @@ namespace go01 [STAThread] static void Main() { + //StreamWriter sw = new StreamWriter(Console.OpenStandardOutput()); + //sw.AutoFlush = true; + //Console.SetOut(sw); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new mainWin()); + //Console.ReadKey(); } } } diff --git a/go01/RejoindrePartieReseau_client.cs b/go01/RejoindrePartieReseau_client.cs index ae91a3c..c4b352e 100644 --- a/go01/RejoindrePartieReseau_client.cs +++ b/go01/RejoindrePartieReseau_client.cs @@ -16,6 +16,7 @@ namespace go01 { public partial class RejoindrePartieReseau_client : Form { + socketPlateau.ConfigGo_S cfg; Thread ecouteThread; System.Threading.Timer timerTTLdtListPart; private DataTable dtListPart; @@ -148,7 +149,7 @@ namespace go01 { if (dgvPart.SelectedRows.Count <= 0) return; - socketPlateau.ConfigGo_S cfg = (socketPlateau.ConfigGo_S)dgvPart.SelectedRows[0].Cells["partName"].Value; + cfg = (socketPlateau.ConfigGo_S)dgvPart.SelectedRows[0].Cells["partName"].Value; tbIpServ.Text = cfg.IpServeur.ToString(); tbPort.Text = cfg.port.ToString(); } @@ -159,8 +160,23 @@ namespace go01 return; socketPlateau.ConfigGo_S cfg = (socketPlateau.ConfigGo_S)dgvPart.SelectedRows[0].Cells["partName"].Value; cfg.NomJoeurClient = tbPlayername.Text; - socketPlateau sc = new socketPlateau(socketPlateau.TypeSocket_E.Client, cfg, null); - sc.senCmd(new socketPlateau.CommandeSocket_S(socketPlateau.commande_E.Join, cfg)); + socketPlateau sc = new socketPlateau(socketPlateau.TypeSocket_E.Client, cfg, null, new socketPlateau.OnJoinACK(SocketACK)); + /*sc. + sc.senCmd(new socketPlateau.CommandeSocket_S(socketPlateau.commande_E.Join, cfg));*/ + } + private void SocketACK(object o, socketPlateauServACKArgs e) + { + //################################################################################################ + //################################################################################################ + //################################################################################################ + //################################################################################################ + //################################################################################################ + //################################################################################################ + Console.WriteLine("CONNETION ETABLEIE"); + Plateau p = new Plateau(cfg.Taille, cfg.PionAligner, cfg.NomJoeurServeur, cfg.NomJoeurClient); + this.Hide(); + p.ShowDialog(); + this.Close(); } } } diff --git a/go01/go01.csproj b/go01/go01.csproj index 692ff23..dcb358b 100644 --- a/go01/go01.csproj +++ b/go01/go01.csproj @@ -81,8 +81,11 @@ True Settings1.settings - - + + + + + CreerPartieReseau_serv.cs diff --git a/go01/socketPlateau.cs b/go01/socketPlateau - Copie.cs similarity index 94% rename from go01/socketPlateau.cs rename to go01/socketPlateau - Copie.cs index bf0e97a..a554231 100644 --- a/go01/socketPlateau.cs +++ b/go01/socketPlateau - Copie.cs @@ -12,7 +12,7 @@ using System.Net.NetworkInformation; namespace go01 { - public class socketPlateau + public class socketPlateauSRV { #region enum public enum TypeSocket_E { NONE, Serveur, Client, LAST } @@ -99,9 +99,10 @@ namespace go01 ConfigGo_S cfgGo; #endregion #region event - public delegate void OnJoinServ(object myObject, socketPlateauServArgs myArgs); - + public delegate void OnJoinServ(object myObject, socketPlateauServJoinArgs myArgs); public event OnJoinServ onJoinServ; + public delegate void OnJoinACK(object myObject, socketPlateauServACKArgs myArgs); + public event OnJoinACK onJoinACK; #endregion public socketPlateau(TypeSocket_E socF, ConfigGo_S cfgGo, Goban g) @@ -117,14 +118,11 @@ namespace go01 try { socCli = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - //socCli.Blocking = false; - //IPAddress IPDist = Verifier(cfgGo.IpServeur.ToString()); socCli.BeginConnect(new IPEndPoint(cfgGo.IpServeur, port), new AsyncCallback(SurConnection), socCli); } catch (Exception ee) { - //controle(true); MessageBox.Show("Connexion impossible : " + ee.Message); } } @@ -133,6 +131,10 @@ namespace go01 //AttenteJoueur(); } } + public socketPlateau(TypeSocket_E socF, ConfigGo_S cfgGo, Goban g, OnJoinACK onJoinACK) : this(socF, cfgGo, g) + { + this.onJoinACK += onJoinACK; + } #region serveur public void AttenteJoueur() { @@ -234,7 +236,11 @@ namespace go01 switch (cmd.commande) { case commande_E.Join: - onJoinServ(this, new socketPlateauServArgs((ConfigGo_S)cmd.data)); + if (socFlag == TypeSocket_E.Serveur) + onJoinServ(this, new socketPlateauServJoinArgs((ConfigGo_S)cmd.data)); + else if (socFlag == TypeSocket_E.Client) + onJoinACK(this, new socketPlateauServACKArgs(true)); + break; case commande_E.Leave: break; diff --git a/go01/socketPlateauBase.cs b/go01/socketPlateauBase.cs new file mode 100644 index 0000000..d4098fc --- /dev/null +++ b/go01/socketPlateauBase.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using System.Net; +using System.IO; +using System.Threading; +using System.Net.NetworkInformation; + +namespace go01 +{ + public abstract class socketPlateauBase + { + #region enum + public enum TypeSocket_E { NONE, Serveur, Client, LAST } + public enum commande_E { Join, Leave, Place, Win, WhoTurn, LAST } // ConfigGo_S ; null ; Point ; Goban.PlayerTuen(string) ; Goban.Joueur_E(Tour) + #endregion + #region struct + [Serializable] + public struct CommandeSocket_S + { + public commande_E commande; + public object data; + public CommandeSocket_S(commande_E cmd, object dt) + { + commande = cmd; + data = dt; + } + } + [Serializable] + public struct ConfigGo_S + { + private string uid; + public string Uid { get { return uid; } } + private string partName; + public string PartName { get { return partName; } } + public string NomJoeurClient; + private string nomJoeurServeur; + public string NomJoeurServeur { get { return nomJoeurServeur; } } + private int taille; + public int Taille { get { return taille; } } + private int pionaAligner; + public int PionAligner { get { return pionaAligner; } } + private IPAddress ipServeur; + public IPAddress IpServeur { get => ipServeur; } + public readonly int port; + + public ConfigGo_S(string Uid, string PartName, string NomJoeurClient, string NomJoeurServeur, int Taille, int PionaAligner, IPAddress ipServeur, int port) + { + this.uid = Uid; + this.partName = PartName; + this.NomJoeurClient = NomJoeurClient; + this.nomJoeurServeur = NomJoeurServeur; + this.taille = Taille; + this.pionaAligner = PionaAligner; + this.ipServeur = ipServeur; + this.port = port; + } + public override string ToString() + { + return partName; + } + public static implicit operator string(ConfigGo_S part) + { + return part.ToString(); + } + } + #endregion + #region variable + protected Socket socServ, socCli; + protected Socket SocCli + { + get + { + if (socFlag == TypeSocket_E.Serveur && socCli != null || socFlag == TypeSocket_E.Client) + { + return socCli; + } + else + { + return null; + } + } + set + { + socCli = value; + } + } + protected TypeSocket_E socFlag = 0; + protected IPAddress adresseIpCourante; + protected int port; + protected static Boolean connexionEtablie = false; + + protected Goban plateauDeJeu; + protected ConfigGo_S cfgGo; + #endregion + + public socketPlateauBase(TypeSocket_E socF, ConfigGo_S cfgGo, Goban g) + { + plateauDeJeu = g; + adresseIpCourante = cfgGo.IpServeur; + socFlag = socF; + this.port = cfgGo.port; + this.cfgGo = cfgGo; + } + + #region commun + protected abstract void InitialiserReception(Socket sArg); + public void senCmd(CommandeSocket_S cmd) + { + var netStream = new NetworkStream(socCli, true); + + var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + binaryFormatter.Serialize(netStream, cmd); + } + #endregion + } +} diff --git a/go01/socketPlateauCLI.cs b/go01/socketPlateauCLI.cs new file mode 100644 index 0000000..566f9ca --- /dev/null +++ b/go01/socketPlateauCLI.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace go01 +{ + class socketPlateauCLI : socketPlateauBase + { + public socketPlateauCLI(ConfigGo_S cfgGo, Goban g) : base(TypeSocket_E.Client, cfgGo, g) + { + } + protected override void InitialiserReception(Socket sArg) + { + throw new NotImplementedException(); + } + } +} diff --git a/go01/socketPlateauEventArgs.cs b/go01/socketPlateauEventArgs.cs new file mode 100644 index 0000000..63bc263 --- /dev/null +++ b/go01/socketPlateauEventArgs.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace go01 +{ + public class socketPlateauServJoinArgs : EventArgs + { + public socketPlateauBase.ConfigGo_S ConfigGo; + public socketPlateauServJoinArgs(socketPlateauBase.ConfigGo_S cfg) + { + this.ConfigGo = cfg; + } + } + + public class socketPlateauServACKArgs : EventArgs + { + public bool ack; + public socketPlateauServACKArgs(bool ack) + { + this.ack = ack; + } + } + + public class socketPlateauServDemandeCo : EventArgs + { + public socketPlateauServDemandeCo() + { + } + } + public class socketPlateauServReception : EventArgs + { + public socketPlateauBase.CommandeSocket_S cmd; + public socketPlateauServReception(socketPlateauBase.CommandeSocket_S cmd) + { + this.cmd = cmd; + } + } +} diff --git a/go01/socketPlateauInterface.cs b/go01/socketPlateauInterface.cs new file mode 100644 index 0000000..2b793b4 --- /dev/null +++ b/go01/socketPlateauInterface.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace go01 +{ + public interface socketPlateauInterface + { + void InitialiserReception(Socket sArg); + } +} diff --git a/go01/socketPlateauSRV.cs b/go01/socketPlateauSRV.cs new file mode 100644 index 0000000..fa5dd8e --- /dev/null +++ b/go01/socketPlateauSRV.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using System.Net; +using System.IO; +using System.Threading; +using System.Net.NetworkInformation; + +namespace go01 +{ + public class socketPlateauSRV : socketPlateauBase + { + System.Threading.Timer timerBroadcast; + System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); + + + #region event + public delegate void OnJoinServ(object myObject, socketPlateauServJoinArgs myArgs); + public event OnJoinServ onJoinServ; + public delegate void OnJoinACK(object myObject, socketPlateauServACKArgs myArgs); + 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; + #endregion + public socketPlateauSRV(ConfigGo_S cfgGo, Goban g) : base(TypeSocket_E.Serveur, cfgGo, g) + { + + } + public socketPlateauSRV(ConfigGo_S cfgGo, Goban g, OnJoinACK onJoinACK) : this(cfgGo, g) + { + this.onJoinACK += onJoinACK; + } + public void AttenteJoueur() + { + if (base.socFlag != TypeSocket_E.Serveur) + return; + var startTimeSpan = TimeSpan.Zero; + var periodTimeSpan = TimeSpan.FromSeconds(5); + + timerBroadcast = new System.Threading.Timer((ee) => + { + if (socFlag != TypeSocket_E.Serveur) + return; + var Client = new UdpClient(); + using (MemoryStream ms = new MemoryStream()) + { + binaryFormatter.Serialize(ms, cfgGo); + Client.EnableBroadcast = true; + byte[] RequestData = ms.ToArray(); + Client.Send(RequestData, RequestData.Length, new IPEndPoint(IPAddress.Broadcast, 8888)); + + } + // byte[] RequestData = { }; /*= Encoding.ASCII.GetBytes($"{cfgGo.Uid};{cfgGo.PartName};{cfgGo.NomJoeurAdverse};{cfgGo.Taille};{cfgGo.PionaAligner}");*/; + Client.Close(); + }, null, startTimeSpan, periodTimeSpan); + + socFlag = TypeSocket_E.Serveur; + socServ = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + + socServ.Bind(new IPEndPoint(adresseIpCourante, port)); + socServ.Listen(1); + socServ.BeginAccept(new AsyncCallback(SurDemandeDeCo), socServ); + } + private void SurDemandeDeCo(IAsyncResult iAR) + { + onDemandeCo(this, new socketPlateauServDemandeCo()); + if (socFlag == TypeSocket_E.Serveur && !connexionEtablie) + { + Socket sTmp = (Socket)iAR.AsyncState; + socCli = sTmp.EndAccept(iAR); + +#if DEBUG + Console.WriteLine($"Connexion effectuée par {((IPEndPoint)socCli.RemoteEndPoint).Address}"); +#endif + connexionEtablie = true; + timerBroadcast.Dispose(); + InitialiserReception(socCli); + } + else if (socFlag == TypeSocket_E.Serveur && connexionEtablie) + { + Socket sTmp = (Socket)iAR.AsyncState; + socCli = sTmp.EndAccept(iAR); + socCli.Send(Encoding.Unicode.GetBytes("Serveur en cours de partie !")); + socCli.Close(); + } + } + protected override 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); + onReceived(this, new socketPlateauServReception(cmd)); + } + } +} diff --git a/go01/socketPlateauServArgs.cs b/go01/socketPlateauServArgs.cs deleted file mode 100644 index 271f71b..0000000 --- a/go01/socketPlateauServArgs.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace go01 -{ - public class socketPlateauServArgs : EventArgs - { - public socketPlateau.ConfigGo_S ConfigGo; - public socketPlateauServArgs(socketPlateau.ConfigGo_S cfg) - { - this.ConfigGo = cfg; - } - } -}