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.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
@ -38,19 +39,24 @@ namespace go01
var startTimeSpan = TimeSpan.Zero;
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));
serveur = new socketPlateauSRV(config, null, new socketPlateauSRV.OnJoinServ(ConnectionEtablie));
serveur = new socketPlateauSRV(config, null, new socketPlateauSRV.OnConnected(ConnectionEtablie));
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}");
serveur.senCmd(new socketPlateauBase.CommandeSocket_S(socketPlateauBase.commande_E.Join, new socketPlateauServACKArgs(true)));
//serveur.senCmd("");
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();
Thread t2 = new Thread(delegate ()
{
p.ShowDialog();
});
t2.Start();
//this.Close();
}
private void broadcast()

View File

@ -48,6 +48,7 @@ namespace go01
}
Occupant_E[,] tableauOccupation;
ConfigGo_S cfg;
private socketPlateauBase sck = null;
#region struct&enum
public enum Occupant_E { Vide, Blanc, Noir, LAST }
@ -139,7 +140,7 @@ namespace go01
fenetre.lblBlanc = playerWName + ":0";
fenetre.lblNoir = playerBName + ":0";
LabelPlayerTurn = new System.Windows.Forms.Label();
GenerationPlateau(false);
//redrawAllpb();
}
@ -154,8 +155,24 @@ namespace go01
Tour = Joueur_E.Noir;
//gen des tab
tableauOccupation = new Occupant_E[cfg.taille, cfg.taille];
LabelPlayerTurn = new System.Windows.Forms.Label();
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))
{
@ -164,7 +181,7 @@ namespace go01
{
PictureBox pb = (PictureBox)sender;
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]);
if (tableauOccupation[x, y] != Occupant_E.Vide && !win)
{
@ -185,6 +202,14 @@ namespace go01
String[] xy = ((PictureBox)sender).Name.Split(';');
Console.WriteLine($"Click sur {xy[0]};{xy[1]} par {Tour}");
int x = int.Parse(xy[0]), y = int.Parse(xy[1]);
placerPion(x, y, pb);
if (sck != null)
sck.senCmd($"click {x};{y}");
}
private void placerPion(int x, int y, PictureBox pb)
{
pb.Invoke((MethodInvoker)(() =>
{
if (tableauOccupation[x, y] == Occupant_E.Vide && !win)
{
tableauOccupation[x, y] = (Tour == Joueur_E.Blanc) ? Occupant_E.Blanc : Occupant_E.Noir;
@ -209,6 +234,8 @@ namespace go01
if (!win)
Tour = (Tour + 1 == Joueur_E.LAST) ? Joueur_E.Blanc : Tour + 1;
}
}));
}
private int[] GetXY(int i)
{
@ -392,14 +419,14 @@ namespace go01
public void GenerationPlateau(bool initTableauOccupation)
{
//label qui indique à qui le tour
LabelPlayerTurn = new System.Windows.Forms.Label();
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.ForeColor = System.Drawing.SystemColors.ControlText;
this.LabelPlayerTurn.Location = new System.Drawing.Point(529, 13);
this.LabelPlayerTurn.Name = "LabelPlayerTurn";
this.LabelPlayerTurn.TabIndex = 1;
this.LabelPlayerTurn.Text = "playerW";
this.LabelPlayerTurn.Text = (tour==Joueur_E.Blanc)?cfg.NomJoeurBlanc:cfg.NomJoeurNoir;
Fenetre.Controls.Add(this.LabelPlayerTurn);
for (int i = 0; i < cfg.taille * cfg.taille; i++) //Genération du goban
@ -436,15 +463,14 @@ namespace go01
if (initTableauOccupation)
{
tableauOccupation[i % cfg.taille, i / cfg.taille] = Occupant_E.Vide;
}
pboxList[i].Name = (i % cfg.taille).ToString() + ";" + (i / cfg.taille).ToString();
pboxList[i].Click += new System.EventHandler(this.pictureBox1_Click);
pboxList[i].Paint += new PaintEventHandler(this.PaintPb);
Conteneur.Controls.Add(pboxList[i]);
pboxList[i].Invalidate();
//pboxList[i].Invalidate();
}
Conteneur.Invalidate();
//Conteneur.Invalidate();
//redrawAllpb();
}

View File

@ -18,13 +18,16 @@ namespace go01
enum CouleurVoulue { Red, Green, Blue, Black, LAST }
CouleurVoulue[,] tableauCouleur = new CouleurVoulue[19,19];
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;
public Plateau(Goban.sauvegarde_S s)
{
InitializeComponent();
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)
{
@ -34,7 +37,12 @@ int dim; int nbPionAlign; string playerW; string playerB;
this.nbPionAlign = nbPionAlign;
this.playerW = playerW;
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)
@ -62,6 +70,9 @@ int dim; int nbPionAlign; string playerW; string playerB;
private void Plateau_Load(object sender, EventArgs e)
{
//base.OnLoad(e);
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;
}

View File

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

View File

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

View File

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

View File

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

View File

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