Separation socketPlateau en Base,SRV,CLI

This commit is contained in:
Adrien VAN DAMME 2018-12-17 15:13:05 +01:00
parent 960e870171
commit 38934283ec
12 changed files with 371 additions and 47 deletions

View File

@ -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}"); 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() private void broadcast()
{ {

View File

@ -12,6 +12,10 @@ namespace go01
{ {
public class Goban public class Goban
{ {
public PictureBox Conteneur; public PictureBox Conteneur;
int nombreDePionAAligne; int nombreDePionAAligne;
int ptBlanc = 0, ptNoir = 0; int ptBlanc = 0, ptNoir = 0;
@ -22,6 +26,9 @@ namespace go01
public string PlayerTurn { get { return this.LabelPlayerTurn.Text; } } public string PlayerTurn { get { return this.LabelPlayerTurn.Text; } }
string playerWName, playerBName; string playerWName, playerBName;
List<PictureBox> pboxList = new List<PictureBox>(); List<PictureBox> pboxList = new List<PictureBox>();
public enum Occupant_E { Vide, Blanc, Noir, LAST }
public enum Joueur_E { Blanc, Noir, LAST }
Joueur_E tour; Joueur_E tour;
Joueur_E Tour Joueur_E Tour
{ {
@ -50,14 +57,14 @@ namespace go01
[Flags] [Flags]
enum Deplacement_E enum Deplacement_E
{ {
SG = 0b00000001, SG = 0b00000001,
S = 0b00000010, S = 0b00000010,
SD = 0b00000100, SD = 0b00000100,
G = 0b00001000, G = 0b00001000,
D = 0b00010000, D = 0b00010000,
IG = 0b00100000, IG = 0b00100000,
I = 0b01000000, I = 0b01000000,
ID = 0b10000000 ID = 0b10000000
} }
[Serializable] [Serializable]
public struct sauvegarde_S public struct sauvegarde_S
@ -70,12 +77,12 @@ namespace go01
public Goban.Joueur_E tour; 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) 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.nomBlanc = nomBlanc; this.nomNoir = nomNoir;
this.ptBlanc = pointBlanc; this.ptNoir = pointNoir; this.ptBlanc = pointBlanc; this.ptNoir = pointNoir;
this.dimGoban = dimGoban; this.nbAlignementToWin = nbAlignementToWin; this.dimGoban = dimGoban; this.nbAlignementToWin = nbAlignementToWin;
this.win = win; this.win = win;
this.tableauOccupation = tableauOccupation; this.tableauOccupation = tableauOccupation;
this.tour = tour; this.tour = tour;
} }
} }
struct carrefour_S struct carrefour_S
@ -88,11 +95,10 @@ namespace go01
SensDeDeplacment = sens; 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; tableauOccupation = s.tableauOccupation;
playerBName = s.nomNoir; playerBName = s.nomNoir;
@ -109,6 +115,8 @@ namespace go01
GenerationPlateau(false); GenerationPlateau(false);
//redrawAllpb(); //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) public Goban(Plateau fenetre, PictureBox conteneur, int dim, int nbPionAlignn, string playerW, string playerB)
{ {
int x = 0; int x = 0;

View File

@ -2,6 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
@ -16,9 +17,13 @@ namespace go01
[STAThread] [STAThread]
static void Main() static void Main()
{ {
//StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
//sw.AutoFlush = true;
//Console.SetOut(sw);
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new mainWin()); Application.Run(new mainWin());
//Console.ReadKey();
} }
} }
} }

View File

@ -16,6 +16,7 @@ namespace go01
{ {
public partial class RejoindrePartieReseau_client : Form public partial class RejoindrePartieReseau_client : Form
{ {
socketPlateau.ConfigGo_S cfg;
Thread ecouteThread; Thread ecouteThread;
System.Threading.Timer timerTTLdtListPart; System.Threading.Timer timerTTLdtListPart;
private DataTable dtListPart; private DataTable dtListPart;
@ -148,7 +149,7 @@ namespace go01
{ {
if (dgvPart.SelectedRows.Count <= 0) if (dgvPart.SelectedRows.Count <= 0)
return; 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(); tbIpServ.Text = cfg.IpServeur.ToString();
tbPort.Text = cfg.port.ToString(); tbPort.Text = cfg.port.ToString();
} }
@ -159,8 +160,23 @@ namespace go01
return; return;
socketPlateau.ConfigGo_S cfg = (socketPlateau.ConfigGo_S)dgvPart.SelectedRows[0].Cells["partName"].Value; socketPlateau.ConfigGo_S cfg = (socketPlateau.ConfigGo_S)dgvPart.SelectedRows[0].Cells["partName"].Value;
cfg.NomJoeurClient = tbPlayername.Text; cfg.NomJoeurClient = tbPlayername.Text;
socketPlateau sc = new socketPlateau(socketPlateau.TypeSocket_E.Client, cfg, null); socketPlateau sc = new socketPlateau(socketPlateau.TypeSocket_E.Client, cfg, null, new socketPlateau.OnJoinACK(SocketACK));
sc.senCmd(new socketPlateau.CommandeSocket_S(socketPlateau.commande_E.Join, cfg)); /*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();
} }
} }
} }

View File

@ -81,8 +81,11 @@
<DesignTimeSharedInput>True</DesignTimeSharedInput> <DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings1.settings</DependentUpon> <DependentUpon>Settings1.settings</DependentUpon>
</Compile> </Compile>
<Compile Include="socketPlateau.cs" /> <Compile Include="socketPlateauCLI.cs" />
<Compile Include="socketPlateauServArgs.cs" /> <Compile Include="socketPlateauSRV.cs" />
<Compile Include="socketPlateauBase.cs" />
<Compile Include="socketPlateauInterface.cs" />
<Compile Include="socketPlateauEventArgs.cs" />
<Compile Include="TextBoxStreamWriter.cs" /> <Compile Include="TextBoxStreamWriter.cs" />
<EmbeddedResource Include="CreerPartieReseau_serv.resx"> <EmbeddedResource Include="CreerPartieReseau_serv.resx">
<DependentUpon>CreerPartieReseau_serv.cs</DependentUpon> <DependentUpon>CreerPartieReseau_serv.cs</DependentUpon>

View File

@ -12,7 +12,7 @@ using System.Net.NetworkInformation;
namespace go01 namespace go01
{ {
public class socketPlateau public class socketPlateauSRV
{ {
#region enum #region enum
public enum TypeSocket_E { NONE, Serveur, Client, LAST } public enum TypeSocket_E { NONE, Serveur, Client, LAST }
@ -99,9 +99,10 @@ namespace go01
ConfigGo_S cfgGo; ConfigGo_S cfgGo;
#endregion #endregion
#region event #region event
public delegate void OnJoinServ(object myObject, socketPlateauServArgs myArgs); public delegate void OnJoinServ(object myObject, socketPlateauServJoinArgs myArgs);
public event OnJoinServ onJoinServ; public event OnJoinServ onJoinServ;
public delegate void OnJoinACK(object myObject, socketPlateauServACKArgs myArgs);
public event OnJoinACK onJoinACK;
#endregion #endregion
public socketPlateau(TypeSocket_E socF, ConfigGo_S cfgGo, Goban g) public socketPlateau(TypeSocket_E socF, ConfigGo_S cfgGo, Goban g)
@ -117,14 +118,11 @@ namespace go01
try try
{ {
socCli = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 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); socCli.BeginConnect(new IPEndPoint(cfgGo.IpServeur, port), new AsyncCallback(SurConnection), socCli);
} }
catch (Exception ee) catch (Exception ee)
{ {
//controle(true);
MessageBox.Show("Connexion impossible : " + ee.Message); MessageBox.Show("Connexion impossible : " + ee.Message);
} }
} }
@ -133,6 +131,10 @@ namespace go01
//AttenteJoueur(); //AttenteJoueur();
} }
} }
public socketPlateau(TypeSocket_E socF, ConfigGo_S cfgGo, Goban g, OnJoinACK onJoinACK) : this(socF, cfgGo, g)
{
this.onJoinACK += onJoinACK;
}
#region serveur #region serveur
public void AttenteJoueur() public void AttenteJoueur()
{ {
@ -234,7 +236,11 @@ namespace go01
switch (cmd.commande) switch (cmd.commande)
{ {
case commande_E.Join: 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; break;
case commande_E.Leave: case commande_E.Leave:
break; break;

121
go01/socketPlateauBase.cs Normal file
View File

@ -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
}
}

20
go01/socketPlateauCLI.cs Normal file
View File

@ -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();
}
}
}

View File

@ -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;
}
}
}

View File

@ -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);
}
}

101
go01/socketPlateauSRV.cs Normal file
View File

@ -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));
}
}
}

View File

@ -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;
}
}
}