using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace go01 { public partial class RejoindrePartieReseau_client : Form { Goban.ConfigGo_S cfg; Thread ecouteThread; System.Threading.Timer timerTTLdtListPart; private DataTable dtListPart; System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); Plateau p; /*public struct partie_S { public string uid, partName, joueurAdverseName, ip; public int taille, pionAaligner; public partie_S(string uid, string partName, string joueurAdverseName, string ip, int taille, int pionAaligner) { this.uid = uid; this.partName = partName; this.joueurAdverseName = joueurAdverseName; this.taille = taille; this.pionAaligner = pionAaligner; this.ip = ip; } public partie_S(string[] s, string ip) { this.uid = s[0]; this.partName = s[1]; this.joueurAdverseName = s[2]; this.taille = int.Parse(s[3]); this.pionAaligner = int.Parse(s[4]); this.ip = ip; } public override string ToString() { return uid; } public static implicit operator string(partie_S part) { return part.ToString(); } }*/ public RejoindrePartieReseau_client() { InitializeComponent(); dtListPart = new DataTable(); dtListPart.Columns.Add("uid", typeof(string)); dtListPart.Columns.Add("partName", typeof(Goban.ConfigGo_S)); dtListPart.Columns.Add("playerName", typeof(string)); dtListPart.Columns.Add("ip", typeof(string)); dtListPart.Columns.Add("port", typeof(int));//timeStamp dtListPart.Columns.Add("timeStamp", typeof(DateTime)); ecouteThread = new Thread(() => { Thread.CurrentThread.IsBackground = true; Listen(); }); ecouteThread.Start(); var startTimeSpan = TimeSpan.Zero; var periodTimeSpan = TimeSpan.FromSeconds(1); timerTTLdtListPart = new System.Threading.Timer((ee) => { TTLdtListPart(); }, null, startTimeSpan, periodTimeSpan); } private void Listen() { var Server = new UdpClient(8888); while (1==1) { var ClientEp = new IPEndPoint(IPAddress.Any, 0); byte[] ClientRequestData = Server.Receive(ref ClientEp); using (MemoryStream ms = new MemoryStream(ClientRequestData)) { Goban.ConfigGo_S cfgGo = (Goban.ConfigGo_S)binaryFormatter.Deserialize(ms); Invoke(new Action(() => { ajoutPartie(cfgGo, ClientEp.Address.ToString()); })); } string ClientRequest = Encoding.ASCII.GetString(ClientRequestData); Console.WriteLine("Recived {0} from {1}, sending response", ClientRequest, ClientEp.Address.ToString()); } } public void ajoutPartie(Goban.ConfigGo_S cfgGo, string ip) { if(dtListPart.Select($"uid = '{cfgGo.Uid}'").Length==0) dtListPart.Rows.Add(cfgGo.Uid, cfgGo, cfgGo.NomJoeurBlanc==""? cfgGo.NomJoeurNoir: cfgGo.NomJoeurBlanc, cfgGo.IpServeur, cfgGo.port, DateTime.Now); //lock (DgvPart) ; dtListPart.Select($"uid = '{cfgGo.Uid}'")[0]["timeStamp"] = DateTime.Now; DgvPart.DataSource = dtListPart; } public void TTLdtListPart() { foreach(DataGridViewRow row in dgvPart.Rows) { if(((DateTime)row.Cells["timeStamp"].Value).AddSeconds(10) <= DateTime.Now) { Invoke(new Action(() => { dgvPart.Rows.Remove(row); dgvPart.Refresh(); })); } } } private void RejoindrePartieReseau_client_Load(object sender, EventArgs e) { } private void RejoindrePartieReseau_client_FormClosing(object sender, FormClosingEventArgs e) { ecouteThread.Abort(); } public DataGridView DgvPart { get { return this.dgvPart; } set { } } private void dgvPart_Click(object sender, EventArgs e) { if (dgvPart.SelectedRows.Count <= 0) return; cfg = (Goban.ConfigGo_S)dgvPart.SelectedRows[0].Cells["partName"].Value; tbIpServ.Text = cfg.IpServeur.ToString(); tbPort.Text = cfg.port.ToString(); } private void btnJoin_Click(object sender, EventArgs e) { if (dgvPart.SelectedRows.Count <= 0) return; cfg = (Goban.ConfigGo_S)dgvPart.SelectedRows[0].Cells["partName"].Value; if(cfg.NomJoeurBlanc == "") cfg.NomJoeurBlanc = tbPlayername.Text; else { cfg.NomJoeurBlanc = tbPlayername.Text; } socketPlateauCLI sc = new socketPlateauCLI(cfg, null, new socketPlateauCLI.OnConnected(SocketCoonected)); /*sc. sc.senCmd(new socketPlateauBase.CommandeSocket_S(socketPlateauBase.commande_E.Join, cfg));*/ } private void SocketCoonected(object o, socketPlateauConnected e) { //################################################################################################ //################################################################################################ //################################################################################################ //################################################################################################ //################################################################################################ //################################################################################################ Console.WriteLine("CONNECTION ETABLEIE"); Plateau p = new Plateau(e.ConfigGo, (socketPlateauBase) o); //this.Hide(); Thread t2 = new Thread(delegate () { p.ShowDialog(); }); t2.Start(); //this.Hide(); // p.ShowDialog(); //this.Close(); } } }