/// <summary> /// Initialize new <see cref="Libuv2kConnection"/>. /// </summary> /// <param name="noDelay"></param> /// <param name="client"></param> public Libuv2kConnection(bool noDelay, TcpStream client = null) { _cancellationToken = new CancellationTokenSource(); if (client == null) { _clientLoop = new Loop(); _client = new TcpStream(_clientLoop); _ = UniTask.RunOnThreadPool(Tick, false, _cancellationToken.Token); } else { _client = client; // setup callbacks client.onMessage = OnLibuvClientMessage; client.onError = OnLibuvClientError; client.onClosed = OnLibuvClientClosed; } _ = UniTask.RunOnThreadPool(ProcessOutgoingMessages, false, _cancellationToken.Token); _client.NoDelay(noDelay); }
void ClientConnect(string hostname, ushort port) { if (client != null) { return; } // libuv doesn't resolve host name, and it needs ipv4. if (LibuvUtils.ResolveToIPV4(hostname, out IPAddress address)) { // connect client IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, IPEndPoint.MinPort); IPEndPoint remoteEndPoint = new IPEndPoint(address, port); Debug.Log("Libuv connecting to: " + address + ":" + port); client = new TcpStream(clientLoop); client.NoDelay(NoDelay); client.onClientConnect = OnLibuvClientConnected; client.onMessage = OnLibuvClientMessage; client.onError = OnLibuvClientError; client.onClosed = OnLibuvClientClosed; client.ConnectTo(localEndPoint, remoteEndPoint); } else { Debug.LogWarning("Libuv Connect: no IPv4 found for hostname: " + hostname); } }