morpionReseau/go01/socketPlateauBase.cs

122 lines
3.9 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 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 abstract void InitialiserReception(Socket sArg);
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
}
}