三国卡牌客户端基础资源仓库
hch
2025-06-20 4841e82bd5e399c4fc39313bbc93c6fc1bb12b2a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
 
namespace Microsoft.Unity.VisualStudio.Editor.Messaging
{
    internal class TcpClient
    {
        private const int ConnectOrReadTimeoutMilliseconds = 5000;
 
        private class State
        {
            public System.Net.Sockets.TcpClient TcpClient;
            public NetworkStream NetworkStream;
            public byte[] Buffer;
            public Action<byte[]> OnBufferAvailable;
        }
 
        public static void Queue(IPAddress address, int port, int bufferSize, Action<byte[]> onBufferAvailable)
        {
            var client = new System.Net.Sockets.TcpClient();
            var state = new State {OnBufferAvailable = onBufferAvailable, TcpClient = client, Buffer = new byte[bufferSize]};
 
            try
            {
                ThreadPool.QueueUserWorkItem(_ =>
                {
                    var handle = client.BeginConnect(address, port, OnClientConnected, state);
                    if (!handle.AsyncWaitHandle.WaitOne(ConnectOrReadTimeoutMilliseconds))
                        Cleanup(state);
                });
            }
            catch (Exception)
            {
                Cleanup(state);
            }
        }
 
        private static void OnClientConnected(IAsyncResult result)
        {
            var state = (State)result.AsyncState;
 
            try
            {
                state.TcpClient.EndConnect(result);
                state.NetworkStream = state.TcpClient.GetStream();
                var handle = state.NetworkStream.BeginRead(state.Buffer, 0, state.Buffer.Length, OnEndRead, state);
                if (!handle.AsyncWaitHandle.WaitOne(ConnectOrReadTimeoutMilliseconds))
                    Cleanup(state);
            }
            catch (Exception)
            {
                Cleanup(state);
            }
        }
 
        private static void OnEndRead(IAsyncResult result)
        {
            var state = (State)result.AsyncState;
 
            try
            {
                var count = state.NetworkStream.EndRead(result);
                if (count == state.Buffer.Length)
                    state.OnBufferAvailable?.Invoke(state.Buffer);
            }
            catch (Exception)
            {
                // Ignore and cleanup
            }
            finally
            {
                Cleanup(state);
            }
        }
 
        private static void Cleanup(State state)
        {
            state.NetworkStream?.Dispose();
            state.TcpClient?.Close();
 
            state.NetworkStream = null;
            state.TcpClient = null;
            state.Buffer = null;
            state.OnBufferAvailable = null;
        }
    }
}