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 abstract class socketPlateauBase
|
|
|
|
|
{
|
|
|
|
|
#region enum
|
|
|
|
|
public enum TypeSocket_E { NONE, Serveur, Client, LAST }
|
|
|
|
|
public enum commande_E { Join, Leave, Place, Win, WhoTurn, LAST } // ConfigGo_S ; null ; Point ; Goban.PlayerTuen(string) ; Goban.Joueur_E(Tour)
|
|
|
|
|
#endregion
|
|
|
|
|
#region struct
|
|
|
|
|
[Serializable]
|
|
|
|
|
public struct CommandeSocket_S
|
|
|
|
|
{
|
|
|
|
|
public commande_E commande;
|
|
|
|
|
public object data;
|
|
|
|
|
public CommandeSocket_S(commande_E cmd, object dt)
|
|
|
|
|
{
|
|
|
|
|
commande = cmd;
|
|
|
|
|
data = dt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
#region variable
|
|
|
|
|
protected Socket socServ, socCli;
|
|
|
|
|
protected Socket SocCli
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (socFlag == TypeSocket_E.Serveur && socCli != null || socFlag == TypeSocket_E.Client)
|
|
|
|
|
{
|
|
|
|
|
return socCli;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
socCli = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
protected TypeSocket_E socFlag = 0;
|
|
|
|
|
protected IPAddress adresseIpCourante;
|
|
|
|
|
protected int port;
|
|
|
|
|
protected static Boolean connexionEtablie = false;
|
|
|
|
|
|
|
|
|
|
protected Goban plateauDeJeu;
|
2019-01-04 11:28:58 +01:00
|
|
|
|
protected Goban.ConfigGo_S cfgGo;
|
2018-12-17 15:13:05 +01:00
|
|
|
|
#endregion
|
2019-01-03 11:20:16 +01:00
|
|
|
|
public delegate void OnReceived(object myObject, socketPlateauServReception myArgs);
|
|
|
|
|
public event OnReceived onReceived;
|
|
|
|
|
|
2019-01-04 11:28:58 +01:00
|
|
|
|
public socketPlateauBase(TypeSocket_E socF, Goban.ConfigGo_S cfgGo, Goban g)
|
2018-12-17 15:13:05 +01:00
|
|
|
|
{
|
|
|
|
|
plateauDeJeu = g;
|
|
|
|
|
adresseIpCourante = cfgGo.IpServeur;
|
|
|
|
|
socFlag = socF;
|
|
|
|
|
this.port = cfgGo.port;
|
|
|
|
|
this.cfgGo = cfgGo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region commun
|
2019-01-03 11:20:16 +01:00
|
|
|
|
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);
|
2019-01-04 11:28:58 +01:00
|
|
|
|
try { onReceived?.Invoke(this, new socketPlateauServReception(cmd)); } catch (Exception e) { Console.WriteLine(e); }
|
2019-01-03 11:20:16 +01:00
|
|
|
|
InitialiserReception(sArg);
|
|
|
|
|
}
|
2019-01-04 11:28:58 +01:00
|
|
|
|
public void senCmd(object cmd)
|
2018-12-17 15:13:05 +01:00
|
|
|
|
{
|
|
|
|
|
var netStream = new NetworkStream(socCli, true);
|
|
|
|
|
|
|
|
|
|
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
|
|
|
|
|
binaryFormatter.Serialize(netStream, cmd);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2019-01-03 11:20:16 +01:00
|
|
|
|
|
|
|
|
|
#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
|
2018-12-17 15:13:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|