// Called in a thread! private void PackageReceivedDelegate(ModProtocol con, ModProtocol.Package p) { GameAPI.Console_Write(string.Format("Package received, id: {0}, type: {1}", p.cmd, Enum.GetName(cmdType, p.cmd))); lock (receivedPackages) { receivedPackages.Add(p); } }
// Called from thread! private void disconnectedDelegate(ModProtocol con) { // Remove from list if (clients != null) { lock (clients) { clients.Remove(con); } } // tbd }
private void ListenForConnections(ModThreadHelper.Info ti) { try { if (tcpListener == null) { return; } ; tcpListener.Start(); while (!ti.eventRunning.WaitOne(0)) { try { // Step 0: Client connection if (!tcpListener.Pending()) { Thread.Sleep(100); // choose a number (in milliseconds) that makes sense continue; } TcpClient tcpClient = tcpListener.AcceptTcpClient(); // blocks until a client connects tcpClient.ReceiveBufferSize = 10 * 1024 * 1024; tcpClient.SendBufferSize = 10 * 1024 * 1024; Console.WriteLine("New connection"); if (packageReceivedDelegate != null) { ModProtocol con = new ModProtocol(tcpClient, packageReceivedDelegate, disconnectedDelegate); lock (clients) { clients.Add(con); } } } catch (Exception e) { Console.WriteLine("Error in ListenForConnections: " + e.Message); ModLoging.Log_Exception(e, "MTP: ListenForConnections"); Console.WriteLine(e.Message); } } closeTcpListener(); } catch (Exception e) { ModLoging.Log_Exception(e, "MTP: ListenForConnections Outer"); } }