/// <summary> /// Triggered when a Client attempts to Connect. Accepts the connection and begins waiting for Data /// </summary> /// <param name="asyn">Associated IAsyncResult object</param> private static void OnClientConnect(IAsyncResult asyn) { ClientInfo tempClientInfo = new ClientInfo(listeningSock.EndAccept(asyn), SocketCounter); clientInfoList.Add(tempClientInfo); WaitForData(tempClientInfo); Interlocked.Increment(ref SocketCounter); Log.AddClientText("Info client has connected.", tempClientInfo.Index); listeningSock.BeginAccept(new AsyncCallback(OnClientConnect), null); }
/// <summary> /// Constructor which starts a SocketPacket based on a ClientInfo object. /// </summary> /// <param name="iServer">Associated ClientInfo object</param> public SocketPacket(ClientInfo iClientInfo) { Socket = iClientInfo.Socket; ClientIndex = iClientInfo.Index; ClientInfo = iClientInfo; }
/// <summary> /// An always-active function that waits for the client to send data to the server. Eventually trigers OnDataReceived(). /// </summary> /// <param name="waitClientInfo">Associated ClientInfo object</param> private static void WaitForData(ClientInfo waitClientInfo) { if (asyncWorkerCallBack == null) { asyncWorkerCallBack = new AsyncCallback(OnDataReceived); } SocketPacket tempSocketPacket = new SocketPacket(waitClientInfo); waitClientInfo.Socket.BeginReceive( tempSocketPacket.DataBuffer, 0, tempSocketPacket.DataBuffer.Length, SocketFlags.None, asyncWorkerCallBack, tempSocketPacket ); }