morpionReseau/go01/socketPlateauCLI.cs

55 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace go01
{
class socketPlateauCLI : socketPlateauBase
{
public delegate void OnConnected(object myObject, socketPlateauConnected myArgs);
public event OnConnected onConnected;
public delegate void OnJoinACK(object myObject, socketPlateauServACKArgs myArgs);
public event OnJoinACK onJoinACK;
public socketPlateauCLI(Goban.ConfigGo_S cfgGo, Goban g) : base(TypeSocket_E.Client, cfgGo, g)
{
try
{
socCli = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socCli.BeginConnect(new IPEndPoint(cfgGo.IpServeur, port), new AsyncCallback(SurConnection), socCli);
}
catch (Exception ee)
{
MessageBox.Show("Connexion impossible : " + ee.Message);
}
}
public socketPlateauCLI(Goban.ConfigGo_S cfgGo, Goban g, OnJoinACK onJoinACK) : this(cfgGo, g)
{
this.onJoinACK = onJoinACK;
}
public socketPlateauCLI(Goban.ConfigGo_S cfgGo, Goban g, OnConnected onConnected) : this(cfgGo, g)
{
this.onConnected = onConnected;
}
private void SurConnection(IAsyncResult iAR)
{
Socket sTmp = (Socket)iAR.AsyncState;
if (sTmp.Connected) {
connexionEtablie = true;
try { onConnected?.Invoke(this, new socketPlateauConnected(cfgGo)); } catch (Exception e) { Console.WriteLine(e); }
InitialiserReception(sTmp);
}
else { MessageBox.Show("Serveur innacessible"); }
}
}
}