]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /** |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | * | |
19 | * Contains some contributions under the Thrift Software License. | |
20 | * Please see doc/old-thrift-license.txt in the Thrift distribution for | |
21 | * details. | |
22 | */ | |
23 | ||
24 | using System; | |
25 | using System.IO.Pipes; | |
26 | using System.Threading; | |
27 | ||
28 | namespace Thrift.Transport | |
29 | { | |
30 | public class TNamedPipeClientTransport : TTransport | |
31 | { | |
32 | private NamedPipeClientStream client; | |
33 | private string ServerName; | |
34 | private string PipeName; | |
35 | private int ConnectTimeout; | |
36 | ||
37 | public TNamedPipeClientTransport(string pipe, int timeout = Timeout.Infinite) | |
38 | { | |
39 | ServerName = "."; | |
40 | PipeName = pipe; | |
41 | ConnectTimeout = timeout; | |
42 | } | |
43 | ||
44 | public TNamedPipeClientTransport(string server, string pipe, int timeout = Timeout.Infinite) | |
45 | { | |
46 | ServerName = (server != "") ? server : "."; | |
47 | PipeName = pipe; | |
48 | ConnectTimeout = timeout; | |
49 | } | |
50 | ||
51 | public override bool IsOpen | |
52 | { | |
53 | get { return client != null && client.IsConnected; } | |
54 | } | |
55 | ||
56 | public override void Open() | |
57 | { | |
58 | if (IsOpen) | |
59 | { | |
60 | throw new TTransportException(TTransportException.ExceptionType.AlreadyOpen); | |
61 | } | |
62 | client = new NamedPipeClientStream(ServerName, PipeName, PipeDirection.InOut, PipeOptions.None); | |
63 | client.Connect(ConnectTimeout); | |
64 | } | |
65 | ||
66 | public override void Close() | |
67 | { | |
68 | if (client != null) | |
69 | { | |
70 | client.Close(); | |
71 | client = null; | |
72 | } | |
73 | } | |
74 | ||
75 | public override int Read(byte[] buf, int off, int len) | |
76 | { | |
77 | if (client == null) | |
78 | { | |
79 | throw new TTransportException(TTransportException.ExceptionType.NotOpen); | |
80 | } | |
81 | ||
82 | return client.Read(buf, off, len); | |
83 | } | |
84 | ||
85 | public override void Write(byte[] buf, int off, int len) | |
86 | { | |
87 | if (client == null) | |
88 | { | |
89 | throw new TTransportException(TTransportException.ExceptionType.NotOpen); | |
90 | } | |
91 | ||
92 | // if necessary, send the data in chunks | |
93 | // there's a system limit around 0x10000 bytes that we hit otherwise | |
94 | // MSDN: "Pipe write operations across a network are limited to 65,535 bytes per write. For more information regarding pipes, see the Remarks section." | |
95 | var nBytes = Math.Min(len, 15 * 4096); // 16 would exceed the limit | |
96 | while (nBytes > 0) | |
97 | { | |
98 | client.Write(buf, off, nBytes); | |
99 | ||
100 | off += nBytes; | |
101 | len -= nBytes; | |
102 | nBytes = Math.Min(len, nBytes); | |
103 | } | |
104 | } | |
105 | ||
106 | protected override void Dispose(bool disposing) | |
107 | { | |
108 | client.Dispose(); | |
109 | } | |
110 | } | |
111 | } |