195 lines
6.5 KiB
C#
195 lines
6.5 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 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;
|
|
}
|
|
}
|
|
[Serializable]
|
|
public struct ConfigGo_S
|
|
{
|
|
private string uid;
|
|
public string Uid { get { return uid; } }
|
|
private string partName;
|
|
public string PartName { get { return partName; } }
|
|
public string NomJoeurClient;
|
|
private string nomJoeurServeur;
|
|
public string NomJoeurServeur { get { return nomJoeurServeur; } }
|
|
private int taille;
|
|
public int Taille { get { return taille; } }
|
|
private int pionaAligner;
|
|
public int PionAligner { get { return pionaAligner; } }
|
|
private IPAddress ipServeur;
|
|
public IPAddress IpServeur { get => ipServeur; }
|
|
public readonly int port;
|
|
|
|
public ConfigGo_S(string Uid, string PartName, string NomJoeurClient, string NomJoeurServeur, int Taille, int PionaAligner, IPAddress ipServeur, int port)
|
|
{
|
|
this.uid = Uid;
|
|
this.partName = PartName;
|
|
this.NomJoeurClient = NomJoeurClient;
|
|
this.nomJoeurServeur = NomJoeurServeur;
|
|
this.taille = Taille;
|
|
this.pionaAligner = PionaAligner;
|
|
this.ipServeur = ipServeur;
|
|
this.port = port;
|
|
}
|
|
public override string ToString()
|
|
{
|
|
return partName;
|
|
}
|
|
public static implicit operator string(ConfigGo_S part)
|
|
{
|
|
return part.ToString();
|
|
}
|
|
}
|
|
#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;
|
|
protected ConfigGo_S cfgGo;
|
|
#endregion
|
|
public delegate void OnReceived(object myObject, socketPlateauServReception myArgs);
|
|
public event OnReceived onReceived;
|
|
|
|
|
|
public socketPlateauBase(TypeSocket_E socF, ConfigGo_S cfgGo, Goban g)
|
|
{
|
|
plateauDeJeu = g;
|
|
adresseIpCourante = cfgGo.IpServeur;
|
|
socFlag = socF;
|
|
this.port = cfgGo.port;
|
|
this.cfgGo = cfgGo;
|
|
}
|
|
|
|
#region commun
|
|
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);
|
|
try { onReceived(this, new socketPlateauServReception(cmd)); } catch { }
|
|
switch (cmd.commande)
|
|
{
|
|
case commande_E.Join:
|
|
break;
|
|
case commande_E.Leave:
|
|
break;
|
|
case commande_E.Place:
|
|
break;
|
|
case commande_E.WhoTurn:
|
|
break;
|
|
case commande_E.Win:
|
|
break;
|
|
}
|
|
Received(cmd);
|
|
InitialiserReception(sArg);
|
|
}
|
|
protected abstract void Received(CommandeSocket_S cmd);
|
|
public void senCmd(CommandeSocket_S cmd)
|
|
{
|
|
var netStream = new NetworkStream(socCli, true);
|
|
|
|
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
|
|
binaryFormatter.Serialize(netStream, cmd);
|
|
}
|
|
#endregion
|
|
|
|
#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
|
|
}
|
|
}
|