Comunication socket OK

This commit is contained in:
Adrien VAN DAMME 2019-01-04 16:44:07 +01:00
parent 9db1e53dd7
commit 9d41bbe37b
8 changed files with 96 additions and 47 deletions

View File

@ -8,6 +8,7 @@ using System.Net;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
@ -38,19 +39,24 @@ namespace go01
var startTimeSpan = TimeSpan.Zero; var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromSeconds(5); var periodTimeSpan = TimeSpan.FromSeconds(5);
Goban.ConfigGo_S config = new Goban.ConfigGo_S(tbPartName.Text, "", tbNomJoeur.Text, int.Parse(tbTaille.Text), int.Parse(tbPionaAligner.Text), IPAddress.Parse(tbIp.Text), int.Parse(tbPort.Text)); Goban.ConfigGo_S config = new Goban.ConfigGo_S(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 = new socketPlateauSRV(config, null, new socketPlateauSRV.OnConnected(ConnectionEtablie));
serveur.AttenteJoueur(); serveur.AttenteJoueur();
} }
private void ConnectionEtablie(object o, socketPlateauServJoinArgs e) private void ConnectionEtablie(object o, socketPlateauConnected e)
{ {
Console.WriteLine($"{e.ConfigGo.NomJoeurBlanc} vs {e.ConfigGo.NomJoeurNoir}"); Console.WriteLine($"{e.ConfigGo.NomJoeurBlanc} vs {e.ConfigGo.NomJoeurNoir}");
serveur.senCmd(new socketPlateauBase.CommandeSocket_S(socketPlateauBase.commande_E.Join, new socketPlateauServACKArgs(true))); //serveur.senCmd("");
Console.WriteLine("CONNETION ETABLEIE"); Console.WriteLine("CONNETION ETABLEIE");
Plateau p = new Plateau(e.ConfigGo.taille, e.ConfigGo.PionAligner, e.ConfigGo.NomJoeurNoir, e.ConfigGo.NomJoeurBlanc); Plateau p = new Plateau(e.ConfigGo,serveur);
//this.Hide(); //this.Hide();
p.ShowDialog();
Thread t2 = new Thread(delegate ()
{
p.ShowDialog();
});
t2.Start();
//this.Close(); //this.Close();
} }
private void broadcast() private void broadcast()

View File

@ -48,6 +48,7 @@ namespace go01
} }
Occupant_E[,] tableauOccupation; Occupant_E[,] tableauOccupation;
ConfigGo_S cfg; ConfigGo_S cfg;
private socketPlateauBase sck = null;
#region struct&enum #region struct&enum
public enum Occupant_E { Vide, Blanc, Noir, LAST } public enum Occupant_E { Vide, Blanc, Noir, LAST }
@ -139,7 +140,7 @@ namespace go01
fenetre.lblBlanc = playerWName + ":0"; fenetre.lblBlanc = playerWName + ":0";
fenetre.lblNoir = playerBName + ":0"; fenetre.lblNoir = playerBName + ":0";
LabelPlayerTurn = new System.Windows.Forms.Label();
GenerationPlateau(false); GenerationPlateau(false);
//redrawAllpb(); //redrawAllpb();
} }
@ -154,8 +155,24 @@ namespace go01
Tour = Joueur_E.Noir; Tour = Joueur_E.Noir;
//gen des tab //gen des tab
tableauOccupation = new Occupant_E[cfg.taille, cfg.taille]; tableauOccupation = new Occupant_E[cfg.taille, cfg.taille];
LabelPlayerTurn = new System.Windows.Forms.Label();
GenerationPlateau(); GenerationPlateau();
} }
public Goban(Plateau fenetre, PictureBox conteneur, ConfigGo_S cfg, socketPlateauBase sck) : this(fenetre,conteneur,cfg)
{
this.sck = sck;
sck.onReceived += new socketPlateauBase.OnReceived(reception);
}
public void reception(object e, socketPlateauServReception arg)
{
Console.WriteLine("ho");
string str = (string) arg.cmd;
str.StartsWith("click");
str = str.Substring(6);
int[] xy = GetXYStr(str);
placerPion(xy[0], xy[1], pboxList[xy[0]+xy[1]*cfg.taille]);
}
public Goban(Plateau fenetre, PictureBox conteneur, int dim, int nbPionAlignn, string playerW, string playerB) : this(fenetre,conteneur, new ConfigGo_S("",playerW,playerB,dim,nbPionAlignn)) public Goban(Plateau fenetre, PictureBox conteneur, int dim, int nbPionAlignn, string playerW, string playerB) : this(fenetre,conteneur, new ConfigGo_S("",playerW,playerB,dim,nbPionAlignn))
{ {
@ -164,7 +181,7 @@ namespace go01
{ {
PictureBox pb = (PictureBox)sender; PictureBox pb = (PictureBox)sender;
String[] xy = ((PictureBox)sender).Name.Split(';'); String[] xy = ((PictureBox)sender).Name.Split(';');
Console.WriteLine($"Click sur {xy[0]};{xy[1]} par {Tour}"); Console.WriteLine($"PaintPb {xy[0]};{xy[1]}");
int x = int.Parse(xy[0]), y = int.Parse(xy[1]); int x = int.Parse(xy[0]), y = int.Parse(xy[1]);
if (tableauOccupation[x, y] != Occupant_E.Vide && !win) if (tableauOccupation[x, y] != Occupant_E.Vide && !win)
{ {
@ -185,30 +202,40 @@ namespace go01
String[] xy = ((PictureBox)sender).Name.Split(';'); String[] xy = ((PictureBox)sender).Name.Split(';');
Console.WriteLine($"Click sur {xy[0]};{xy[1]} par {Tour}"); Console.WriteLine($"Click sur {xy[0]};{xy[1]} par {Tour}");
int x = int.Parse(xy[0]), y = int.Parse(xy[1]); int x = int.Parse(xy[0]), y = int.Parse(xy[1]);
if (tableauOccupation[x, y] == Occupant_E.Vide && !win) placerPion(x, y, pb);
if (sck != null)
sck.senCmd($"click {x};{y}");
}
private void placerPion(int x, int y, PictureBox pb)
{
pb.Invoke((MethodInvoker)(() =>
{ {
tableauOccupation[x, y] = (Tour == Joueur_E.Blanc) ? Occupant_E.Blanc : Occupant_E.Noir; if (tableauOccupation[x, y] == Occupant_E.Vide && !win)
using (Graphics gr = Graphics.FromHwnd((pb.Handle)))
{
SolidBrush b = (Tour == Joueur_E.Blanc)?new SolidBrush(Color.White):new SolidBrush(Color.Black);;
if (b != null)
gr.FillEllipse(b, 1, 0, pb.ClientSize.Width - 2, pb.ClientSize.Height - 1);
else
gr.DrawImage(pb.Image, 0, 0, pb.Width, pb.Height);
}
bool fin = win = RechercheTeritoir(x, y);
if (fin)
{ {
tableauOccupation[x, y] = (Tour == Joueur_E.Blanc) ? Occupant_E.Blanc : Occupant_E.Noir;
x=((Tour == Joueur_E.Blanc)) ? ptNoir++:ptBlanc++; using (Graphics gr = Graphics.FromHwnd((pb.Handle)))
Win(true); {
MessageBox.Show("YOUR WIN" + ((Tour == Joueur_E.Blanc) ? playerWName : playerBName)); SolidBrush b = (Tour == Joueur_E.Blanc) ? new SolidBrush(Color.White) : new SolidBrush(Color.Black); ;
if (b != null)
gr.FillEllipse(b, 1, 0, pb.ClientSize.Width - 2, pb.ClientSize.Height - 1);
else
gr.DrawImage(pb.Image, 0, 0, pb.Width, pb.Height);
}
bool fin = win = RechercheTeritoir(x, y);
if (fin)
{
x = ((Tour == Joueur_E.Blanc)) ? ptNoir++ : ptBlanc++;
Win(true);
MessageBox.Show("YOUR WIN" + ((Tour == Joueur_E.Blanc) ? playerWName : playerBName));
}
Console.WriteLine(fin);
if (!win)
Tour = (Tour + 1 == Joueur_E.LAST) ? Joueur_E.Blanc : Tour + 1;
} }
Console.WriteLine(fin); }));
if(!win)
Tour = (Tour + 1 == Joueur_E.LAST) ? Joueur_E.Blanc : Tour + 1;
}
} }
private int[] GetXY(int i) private int[] GetXY(int i)
{ {
@ -392,14 +419,14 @@ namespace go01
public void GenerationPlateau(bool initTableauOccupation) public void GenerationPlateau(bool initTableauOccupation)
{ {
//label qui indique à qui le tour //label qui indique à qui le tour
LabelPlayerTurn = new System.Windows.Forms.Label();
this.LabelPlayerTurn.AutoSize = true; this.LabelPlayerTurn.AutoSize = true;
this.LabelPlayerTurn.Font = new System.Drawing.Font("Matura MT Script Capitals", 40F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.LabelPlayerTurn.Font = new System.Drawing.Font("Matura MT Script Capitals", 40F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.LabelPlayerTurn.ForeColor = System.Drawing.SystemColors.ControlText; this.LabelPlayerTurn.ForeColor = System.Drawing.SystemColors.ControlText;
this.LabelPlayerTurn.Location = new System.Drawing.Point(529, 13); this.LabelPlayerTurn.Location = new System.Drawing.Point(529, 13);
this.LabelPlayerTurn.Name = "LabelPlayerTurn"; this.LabelPlayerTurn.Name = "LabelPlayerTurn";
this.LabelPlayerTurn.TabIndex = 1; this.LabelPlayerTurn.TabIndex = 1;
this.LabelPlayerTurn.Text = "playerW"; this.LabelPlayerTurn.Text = (tour==Joueur_E.Blanc)?cfg.NomJoeurBlanc:cfg.NomJoeurNoir;
Fenetre.Controls.Add(this.LabelPlayerTurn); Fenetre.Controls.Add(this.LabelPlayerTurn);
for (int i = 0; i < cfg.taille * cfg.taille; i++) //Genération du goban for (int i = 0; i < cfg.taille * cfg.taille; i++) //Genération du goban
@ -436,15 +463,14 @@ namespace go01
if (initTableauOccupation) if (initTableauOccupation)
{ {
tableauOccupation[i % cfg.taille, i / cfg.taille] = Occupant_E.Vide; tableauOccupation[i % cfg.taille, i / cfg.taille] = Occupant_E.Vide;
} }
pboxList[i].Name = (i % cfg.taille).ToString() + ";" + (i / cfg.taille).ToString(); pboxList[i].Name = (i % cfg.taille).ToString() + ";" + (i / cfg.taille).ToString();
pboxList[i].Click += new System.EventHandler(this.pictureBox1_Click); pboxList[i].Click += new System.EventHandler(this.pictureBox1_Click);
pboxList[i].Paint += new PaintEventHandler(this.PaintPb); pboxList[i].Paint += new PaintEventHandler(this.PaintPb);
Conteneur.Controls.Add(pboxList[i]); Conteneur.Controls.Add(pboxList[i]);
pboxList[i].Invalidate(); //pboxList[i].Invalidate();
} }
Conteneur.Invalidate(); //Conteneur.Invalidate();
//redrawAllpb(); //redrawAllpb();
} }

View File

@ -18,13 +18,16 @@ namespace go01
enum CouleurVoulue { Red, Green, Blue, Black, LAST } enum CouleurVoulue { Red, Green, Blue, Black, LAST }
CouleurVoulue[,] tableauCouleur = new CouleurVoulue[19,19]; CouleurVoulue[,] tableauCouleur = new CouleurVoulue[19,19];
List<PictureBox> pboxList = new List<PictureBox>(); List<PictureBox> pboxList = new List<PictureBox>();
Goban g; Goban g=null;
socketPlateauBase sck = null;
Goban.ConfigGo_S? cfg=null;
Goban.sauvegarde_S? sauvegarde = null;
int dim; int nbPionAlign; string playerW; string playerB; int dim; int nbPionAlign; string playerW; string playerB;
public Plateau(Goban.sauvegarde_S s) public Plateau(Goban.sauvegarde_S s)
{ {
InitializeComponent(); InitializeComponent();
this.Text = $"Morpion {s.cfg.taille}x{s.cfg.taille}"; this.Text = $"Morpion {s.cfg.taille}x{s.cfg.taille}";
g = new Goban(this, pictureBox1, s); sauvegarde = s;
} }
public Plateau(int dim, int nbPionAlign, string playerW, string playerB) public Plateau(int dim, int nbPionAlign, string playerW, string playerB)
{ {
@ -34,7 +37,12 @@ int dim; int nbPionAlign; string playerW; string playerB;
this.nbPionAlign = nbPionAlign; this.nbPionAlign = nbPionAlign;
this.playerW = playerW; this.playerW = playerW;
this.playerB = playerB; this.playerB = playerB;
}
public Plateau(Goban.ConfigGo_S cfg, socketPlateauBase sck) : this(cfg.taille, cfg.pionaAligner, cfg.NomJoeurBlanc, cfg.NomJoeurNoir)
{
this.cfg = cfg;
this.sck = sck;
this.Text = $"Morpion {dim}x{dim} "+sck.SocFlag;
} }
private void pictureBox1_Click(object sender, EventArgs e) private void pictureBox1_Click(object sender, EventArgs e)
@ -62,7 +70,10 @@ int dim; int nbPionAlign; string playerW; string playerB;
private void Plateau_Load(object sender, EventArgs e) private void Plateau_Load(object sender, EventArgs e)
{ {
//base.OnLoad(e); //base.OnLoad(e);
g = new Goban(this, pictureBox1, dim, nbPionAlign, playerW, playerB); if (sck != null && cfg != null && sck != null)
g = new Goban(this, pictureBox1, cfg.Value, sck);
else
g = new Goban(this, pictureBox1, dim, nbPionAlign, playerW, playerB);
this.DoubleBuffered = true; this.DoubleBuffered = true;
} }

View File

@ -177,11 +177,16 @@ namespace go01
//################################################################################################ //################################################################################################
//################################################################################################ //################################################################################################
Console.WriteLine("CONNECTION ETABLEIE"); Console.WriteLine("CONNECTION ETABLEIE");
p = new Plateau(cfg.taille, cfg.PionAligner, cfg.NomJoeurBlanc, cfg.NomJoeurNoir); Plateau p = new Plateau(e.ConfigGo, (socketPlateauBase) o);
p.Show(); //this.Hide();
Thread t2 = new Thread(delegate ()
{
p.ShowDialog();
});
t2.Start();
//this.Hide(); //this.Hide();
// p.ShowDialog(); // p.ShowDialog();
//this.Close(); //this.Close();
} }
} }

View File

@ -52,6 +52,7 @@ namespace go01
} }
} }
protected TypeSocket_E socFlag = 0; protected TypeSocket_E socFlag = 0;
public TypeSocket_E SocFlag { get => socFlag; }
protected IPAddress adresseIpCourante; protected IPAddress adresseIpCourante;
protected int port; protected int port;
protected static Boolean connexionEtablie = false; protected static Boolean connexionEtablie = false;
@ -72,13 +73,13 @@ namespace go01
} }
#region commun #region commun
protected void InitialiserReception(Socket sArg) protected void InitialiserReceptionAsync(Socket sArg)
{ {
var netStream = new NetworkStream(sArg, true); var netStream = new NetworkStream(sArg, true);
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
CommandeSocket_S cmd = (CommandeSocket_S)binaryFormatter.Deserialize(netStream); object cmd = (object)binaryFormatter.Deserialize(netStream);
try { onReceived?.Invoke(this, new socketPlateauServReception(cmd)); } catch (Exception e) { Console.WriteLine(e); } try { onReceived?.Invoke(this, new socketPlateauServReception(cmd)); } catch (Exception e) { Console.WriteLine(e); }
InitialiserReception(sArg); InitialiserReceptionAsync(sArg);
} }
public void senCmd(object cmd) public void senCmd(object cmd)
{ {

View File

@ -44,7 +44,7 @@ namespace go01
if (sTmp.Connected) { if (sTmp.Connected) {
connexionEtablie = true; connexionEtablie = true;
try { onConnected?.Invoke(this, new socketPlateauConnected(cfgGo)); } catch (Exception e) { Console.WriteLine(e); } try { onConnected?.Invoke(this, new socketPlateauConnected(cfgGo)); } catch (Exception e) { Console.WriteLine(e); }
InitialiserReception(sTmp); InitialiserReceptionAsync(sTmp);
} }
else { MessageBox.Show("Serveur innacessible"); } else { MessageBox.Show("Serveur innacessible"); }

View File

@ -32,8 +32,8 @@ namespace go01
} }
public class socketPlateauServReception : EventArgs public class socketPlateauServReception : EventArgs
{ {
public socketPlateauBase.CommandeSocket_S cmd; public object cmd;
public socketPlateauServReception(socketPlateauBase.CommandeSocket_S cmd) public socketPlateauServReception(object cmd)
{ {
this.cmd = cmd; this.cmd = cmd;
} }

View File

@ -94,7 +94,7 @@ namespace go01
connexionEtablie = true; connexionEtablie = true;
try { onConnected?.Invoke(this, new socketPlateauConnected(cfgGo)); } catch (Exception e) { Console.WriteLine(e); } try { onConnected?.Invoke(this, new socketPlateauConnected(cfgGo)); } catch (Exception e) { Console.WriteLine(e); }
timerBroadcast.Dispose(); timerBroadcast.Dispose();
InitialiserReception(socCli); InitialiserReceptionAsync(socCli);
} }
else if (socFlag == TypeSocket_E.Serveur && connexionEtablie) else if (socFlag == TypeSocket_E.Serveur && connexionEtablie)
{ {