コード例 #1
0
    public void Close()
    {
        try
        {
            if (listenThread != null)
            {
                listenThread.WaitForEnd();
            }

            if (tcpListener != null)
            {
                ModLoging.Log("MTP: TCP Listener was still open and will be closed", ModLoging.eTyp.Warning);
                closeTcpListener();
            }

            ModProtocol[] connectionArr;
            lock (clients)
            {
                connectionArr = clients.ToArray();      // need to copy this else the disconnected callback will remove it from list as well (and we get a thread lock)
            }
            foreach (ModProtocol con in connectionArr)
            {
                con.Close();
            }
        }
        catch (Exception e)
        {
            ModLoging.Log_Exception(e, "MTP: Close");
        }
    }
コード例 #2
0
    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");
        }
    }
コード例 #3
0
 public void StartListen(string ip, int port, ModProtocol.DelegatePackageReceived nPackageReceivedDelegate)
 {
     try
     {
         packageReceivedDelegate = nPackageReceivedDelegate;
         tcpListener             = new TcpListener(IPAddress.Parse(ip), port);
         listenThread            = ModThreadHelper.StartThread(ListenForConnections, System.Threading.ThreadPriority.Lowest);
         gameAPI.Console_Write("Now listening on port " + port);
     }
     catch (Exception e)
     {
         ModLoging.Log_Exception(e, "MTP: StartListen");
     }
 }