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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Contains some contributions under the Thrift Software License.
20 * Please see doc/old-thrift-license.txt in the Thrift distribution for
25 using System.IO.Pipes;
26 using System.Threading;
28 namespace Thrift.Transport
30 public class TNamedPipeClientTransport : TTransport
32 private NamedPipeClientStream client;
33 private string ServerName;
34 private string PipeName;
35 private int ConnectTimeout;
37 public TNamedPipeClientTransport(string pipe, int timeout = Timeout.Infinite)
41 ConnectTimeout = timeout;
44 public TNamedPipeClientTransport(string server, string pipe, int timeout = Timeout.Infinite)
46 ServerName = (server != "") ? server : ".";
48 ConnectTimeout = timeout;
51 public override bool IsOpen
53 get { return client != null && client.IsConnected; }
56 public override void Open()
60 throw new TTransportException(TTransportException.ExceptionType.AlreadyOpen);
62 client = new NamedPipeClientStream(ServerName, PipeName, PipeDirection.InOut, PipeOptions.None);
63 client.Connect(ConnectTimeout);
66 public override void Close()
75 public override int Read(byte[] buf, int off, int len)
79 throw new TTransportException(TTransportException.ExceptionType.NotOpen);
82 return client.Read(buf, off, len);
85 public override void Write(byte[] buf, int off, int len)
89 throw new TTransportException(TTransportException.ExceptionType.NotOpen);
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
98 client.Write(buf, off, nBytes);
102 nBytes = Math.Min(len, nBytes);
106 protected override void Dispose(bool disposing)