/// <summary> /// Events the TCP client accept. /// </summary> /// <param name="ar">The async result.</param> private void EventTcpClientAccept(IAsyncResult ar) { try { TcpListener listener = (TcpListener)ar.AsyncState; TcpClient newClient = listener.EndAcceptTcpClient(ar); newClient.ReceiveTimeout = 0; Thread t = new Thread(new ParameterizedThreadStart(HandleClient)); t.IsBackground = true; int uniqueID = CreateUniqueClientID(); ClientConnection sC = new ClientConnection(); sC.ThisID = uniqueID; sC.ThisClient = newClient; sC.ThisThread = t; switch (ConnectionType) { case EConnectionType.SERVER: sC.ConnectionType = EConnectionType.CLIENT; break; } Clients.Add(uniqueID, sC); Clients[uniqueID].ThisThread.Start(Clients[uniqueID]); ConsoleLogger.WriteToLog("Client ID: " + uniqueID + " Connected.", true); PacketDesc_SetClientID sidpkg = new PacketDesc_SetClientID(); sidpkg.PacketTarget = sC.ConnectionType; sidpkg.Id = uniqueID; sidpkg.SenderID = UniqueID; sidpkg.PacketOriginClientID = UniqueID; SendPacketToClient(sidpkg, uniqueID); PacketDesc_ClientConnected copkg = new PacketDesc_ClientConnected(); copkg.PacketTarget = sC.ConnectionType; copkg.OtherClientID = uniqueID; copkg.PacketOriginClientID = UniqueID; SendPacketToAllClients(copkg, uniqueID); UpdateTitle(); TcpClientConnected.Set(); if (OnClientAccepted != null) { OnClientAccepted(uniqueID); } } catch (ObjectDisposedException) { return; } }
/// <summary> /// Recieved the client connected packet. /// </summary> /// <param name="conPkt">The con PKT.</param> /// <returns>use as needed</returns> protected virtual bool ReceivedPacket_ClientConnected(KeyValuePair <ClientConnection, object> conPkt) { PacketDesc_ClientConnected packet = (PacketDesc_ClientConnected)conPkt.Value; Console.WriteLine("Recieved ClientConnected Packet! Client ID: " + conPkt.Key.ThisID + ", Data: connected client ID " + packet.OtherClientID); if (OnClientConnectedReceived != null) { OnClientConnectedReceived(conPkt); } return(true); }