2018-12-17 15:13:05 +01:00
|
|
|
|
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;
|
2019-01-03 11:20:16 +01:00
|
|
|
|
public delegate void OnConnected(object myObject, socketPlateauConnected myArgs);
|
|
|
|
|
public event OnConnected onConnected;
|
2018-12-17 15:13:05 +01:00
|
|
|
|
#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)
|
|
|
|
|
{
|
2019-01-03 11:20:16 +01:00
|
|
|
|
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;
|
2018-12-17 15:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
{
|
2019-01-03 11:20:16 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
onDemandeCo(this, new socketPlateauServDemandeCo());
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2018-12-17 15:13:05 +01:00
|
|
|
|
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;
|
2019-01-03 11:20:16 +01:00
|
|
|
|
try { onConnected(this, new socketPlateauConnected(cfgGo)); } catch { }
|
2018-12-17 15:13:05 +01:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-03 11:20:16 +01:00
|
|
|
|
|
|
|
|
|
protected override void Received(CommandeSocket_S cmd)
|
2018-12-17 15:13:05 +01:00
|
|
|
|
{
|
2019-01-03 11:20:16 +01:00
|
|
|
|
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;
|
|
|
|
|
}
|
2018-12-17 15:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|