morpionReseau/go01/socketPlateauSRV.cs

109 lines
4.3 KiB
C#

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;
public delegate void OnConnected(object myObject, socketPlateauConnected myArgs);
public event OnConnected onConnected;
public event EventHandler onReceive;
#endregion
public socketPlateauSRV(Goban.ConfigGo_S cfgGo, Goban g) : base(TypeSocket_E.Serveur, cfgGo, g)
{
}
public socketPlateauSRV(Goban.ConfigGo_S cfgGo, Goban g, OnJoinACK onJoinACK) : this(cfgGo, g)
{
this.onJoinACK = onJoinACK;
}
public socketPlateauSRV(Goban.ConfigGo_S cfgGo, Goban g, OnConnected onConnected) : this(cfgGo, g)
{
this.onConnected = onConnected;
}
public socketPlateauSRV(Goban.ConfigGo_S cfgGo, Goban g, OnJoinServ onJoinServ) : this(cfgGo, g)
{
this.onJoinServ = onJoinServ;
}
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)
{
try
{
onDemandeCo?.Invoke(this, new socketPlateauServDemandeCo());
}
catch (Exception e) { Console.WriteLine(e); }
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;
try { onConnected?.Invoke(this, new socketPlateauConnected(cfgGo)); } catch (Exception e) { Console.WriteLine(e); }
timerBroadcast.Dispose();
InitialiserReceptionAsync(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();
}
}
}
}