public void Connect(string ip, int port) { // not if already started if (Connecting || Connected) { return; } // We are connecting from now until Connect succeeds or fails _Connecting = true; // TcpClient can only be used once. need to create a new one each // time. client = new TcpClient(); client.NoDelay = NoDelay; client.SendTimeout = SendTimeout; // clear old messages in queue, just to be sure that the caller // doesn't receive data from last time and gets out of sync. // -> calling this in Disconnect isn't smart because the caller may // still want to process all the latest messages afterwards receiveQueue = new ConcurrentQueue <Message>(); sendQueue.Clear(); // client.Connect(ip, port) is blocking. let's call it in the thread // and return immediately. // -> this way the application doesn't hang for 30s if connect takes // too long, which is especially good in games // -> this way we don't async client.BeginConnect, which seems to // fail sometimes if we connect too many clients too fast receiveThread = new Thread(() => { ReceiveThreadFunction(ip, port); }); receiveThread.IsBackground = true; receiveThread.Start(); }
public void Connect(string ip, int port) { // not if already started if (Connecting || Connected) { Logger.LogWarning("Telepathy Client can not create connection because an existing connection is connecting or connected"); return; } // We are connecting from now until Connect succeeds or fails _Connecting = true; // create a TcpClient with perfect IPv4, IPv6 and hostname resolving // support. // // * TcpClient(hostname, port): works but would connect (and block) // already // * TcpClient(AddressFamily.InterNetworkV6): takes Ipv4 and IPv6 // addresses but only connects to IPv6 servers (e.g. Telepathy). // does NOT connect to IPv4 servers (e.g. Mirror Booster), even // with DualMode enabled. // * TcpClient(): creates IPv4 socket internally, which would force // Connect() to only use IPv4 sockets. // // => the trick is to clear the internal IPv4 socket so that Connect // resolves the hostname and creates either an IPv4 or an IPv6 // socket as needed (see TcpClient source) #if false // creates IPv4 socket client = new TcpClient(); // clear internal IPv4 socket until Connect() client.Client = null; #endif client = null; // Will construct in thread abortConnect = false; // clear old messages in queue, just to be sure that the caller // doesn't receive data from last time and gets out of sync. // -> calling this in Disconnect isn't smart because the caller may // still want to process all the latest messages afterwards receiveQueue = new ConcurrentQueue <Message>(); sendQueue.Clear(); // client.Connect(ip, port) is blocking. let's call it in the thread // and return immediately. // -> this way the application doesn't hang for 30s if connect takes // too long, which is especially good in games // -> this way we don't async client.BeginConnect, which seems to // fail sometimes if we connect too many clients too fast receiveThread = new Thread(() => { ReceiveThreadFunction(ip, port); }); receiveThread.IsBackground = true; receiveThread.Start(); }
public void Connect(string ip, int port) { if (Connecting || Connected) { return; } _Connecting = true; client = new TcpClient(); client.NoDelay = NoDelay; client.SendTimeout = SendTimeout; receiveQueue = new ConcurrentQueue <Message>(); sendQueue.Clear(); receiveThread = new Thread(() => { ReceiveThreadFunction(ip, port); }); receiveThread.IsBackground = true; receiveThread.Start(); }