commit maj ?

This commit is contained in:
Adrien VAN DAMME 2019-01-03 11:20:16 +01:00
parent 38934283ec
commit f88809a50a
8 changed files with 190 additions and 39 deletions

View File

@ -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()
{

View File

@ -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)
{

View File

@ -19,7 +19,7 @@ namespace go01
CouleurVoulue[,] tableauCouleur = new CouleurVoulue[19,19];
List<PictureBox> pboxList = new List<PictureBox>();
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)
{
}
}
}

View File

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

View File

@ -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<String> values = new List<String>();
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
}
}

View File

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

View File

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

View File

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