morpionReseau/go01/socketPlateauSRV.cs

102 lines
4.0 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 OnReceived(object myObject, socketPlateauServReception myArgs);
public event OnReceived onReceived;
#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)
{
this.onJoinACK += onJoinACK;
}
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)
{
onDemandeCo(this, new socketPlateauServDemandeCo());
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;
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();
}
}
protected override 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);
onReceived(this, new socketPlateauServReception(cmd));
}
}
}