]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | // Licensed to the Apache Software Foundation(ASF) under one |
2 | // or more contributor license agreements.See the NOTICE file | |
3 | // distributed with this work for additional information | |
4 | // regarding copyright ownership.The ASF licenses this file | |
5 | // to you under the Apache License, Version 2.0 (the | |
6 | // "License"); you may not use this file except in compliance | |
7 | // with the License. You may obtain a copy of the License at | |
8 | // | |
9 | // http://www.apache.org/licenses/LICENSE-2.0 | |
10 | // | |
11 | // Unless required by applicable law or agreed to in writing, | |
12 | // software distributed under the License is distributed on an | |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | // KIND, either express or implied. See the License for the | |
15 | // specific language governing permissions and limitations | |
16 | // under the License. | |
17 | ||
18 | using System; | |
19 | using System.Net; | |
20 | using System.Net.Sockets; | |
21 | using System.Threading; | |
22 | using System.Threading.Tasks; | |
23 | ||
24 | namespace Thrift.Transports.Client | |
25 | { | |
26 | // ReSharper disable once InconsistentNaming | |
27 | public class TSocketClientTransport : TStreamClientTransport | |
28 | { | |
29 | private bool _isDisposed; | |
30 | ||
31 | public TSocketClientTransport(TcpClient client) | |
32 | { | |
33 | TcpClient = client ?? throw new ArgumentNullException(nameof(client)); | |
34 | ||
35 | if (IsOpen) | |
36 | { | |
37 | InputStream = client.GetStream(); | |
38 | OutputStream = client.GetStream(); | |
39 | } | |
40 | } | |
41 | ||
42 | public TSocketClientTransport(IPAddress host, int port) | |
43 | : this(host, port, 0) | |
44 | { | |
45 | } | |
46 | ||
47 | public TSocketClientTransport(IPAddress host, int port, int timeout) | |
48 | { | |
49 | Host = host; | |
50 | Port = port; | |
51 | ||
52 | TcpClient = new TcpClient(); | |
53 | TcpClient.ReceiveTimeout = TcpClient.SendTimeout = timeout; | |
54 | TcpClient.Client.NoDelay = true; | |
55 | } | |
56 | ||
57 | public TcpClient TcpClient { get; private set; } | |
58 | public IPAddress Host { get; } | |
59 | public int Port { get; } | |
60 | ||
61 | public int Timeout | |
62 | { | |
63 | set | |
64 | { | |
65 | if (TcpClient != null) | |
66 | { | |
67 | TcpClient.ReceiveTimeout = TcpClient.SendTimeout = value; | |
68 | } | |
69 | } | |
70 | } | |
71 | ||
72 | public override bool IsOpen | |
73 | { | |
74 | get | |
75 | { | |
76 | if (TcpClient == null) | |
77 | { | |
78 | return false; | |
79 | } | |
80 | ||
81 | return TcpClient.Connected; | |
82 | } | |
83 | } | |
84 | ||
85 | public override async Task OpenAsync(CancellationToken cancellationToken) | |
86 | { | |
87 | if (cancellationToken.IsCancellationRequested) | |
88 | { | |
89 | await Task.FromCanceled(cancellationToken); | |
90 | } | |
91 | ||
92 | if (IsOpen) | |
93 | { | |
94 | throw new TTransportException(TTransportException.ExceptionType.AlreadyOpen, "Socket already connected"); | |
95 | } | |
96 | ||
97 | if (Port <= 0) | |
98 | { | |
99 | throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open without port"); | |
100 | } | |
101 | ||
102 | if (TcpClient == null) | |
103 | { | |
104 | throw new InvalidOperationException("Invalid or not initialized tcp client"); | |
105 | } | |
106 | ||
107 | await TcpClient.ConnectAsync(Host, Port); | |
108 | ||
109 | InputStream = TcpClient.GetStream(); | |
110 | OutputStream = TcpClient.GetStream(); | |
111 | } | |
112 | ||
113 | public override void Close() | |
114 | { | |
115 | base.Close(); | |
116 | ||
117 | if (TcpClient != null) | |
118 | { | |
119 | TcpClient.Dispose(); | |
120 | TcpClient = null; | |
121 | } | |
122 | } | |
123 | ||
124 | // IDisposable | |
125 | protected override void Dispose(bool disposing) | |
126 | { | |
127 | if (!_isDisposed) | |
128 | { | |
129 | if (disposing) | |
130 | { | |
131 | TcpClient?.Dispose(); | |
132 | ||
133 | base.Dispose(disposing); | |
134 | } | |
135 | } | |
136 | _isDisposed = true; | |
137 | } | |
138 | } | |
139 | } |