blob: 4c64a3607aed08bce2b378788c6c3c296f4ec38c [file] [log] [blame]
Kevin Clarkab4460d2009-03-20 02:28:41 +00001/**
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.
Todd Lipcon53ae9f32009-12-07 00:42:38 +000018 *
19 * Contains some contributions under the Thrift Software License.
20 * Please see doc/old-thrift-license.txt in the Thrift distribution for
21 * details.
Kevin Clarkab4460d2009-03-20 02:28:41 +000022 */
David Reiss7f42bcf2008-01-11 20:59:12 +000023
24using System;
David Reiss7f42bcf2008-01-11 20:59:12 +000025using System.Net.Sockets;
26
27namespace Thrift.Transport
28{
29 public class TSocket : TStreamTransport
30 {
31 private TcpClient client = null;
32 private string host = null;
33 private int port = 0;
34 private int timeout = 0;
35
36 public TSocket(TcpClient client)
37 {
38 this.client = client;
39
40 if (IsOpen)
41 {
42 inputStream = client.GetStream();
43 outputStream = client.GetStream();
44 }
45 }
46
47 public TSocket(string host, int port) : this(host, port, 0)
48 {
49 }
50
51 public TSocket(string host, int port, int timeout)
52 {
53 this.host = host;
54 this.port = port;
55 this.timeout = timeout;
56
57 InitSocket();
58 }
59
60 private void InitSocket()
61 {
62 client = new TcpClient();
63 client.ReceiveTimeout = client.SendTimeout = timeout;
64 }
65
66 public int Timeout
67 {
68 set
69 {
70 client.ReceiveTimeout = client.SendTimeout = timeout = value;
71 }
72 }
73
74 public TcpClient TcpClient
75 {
76 get
77 {
78 return client;
79 }
80 }
81
David Reiss63191332009-01-06 19:49:22 +000082 public string Host
83 {
84 get
85 {
86 return host;
87 }
88 }
89
90 public int Port
91 {
92 get
93 {
94 return port;
95 }
96 }
97
David Reiss7f42bcf2008-01-11 20:59:12 +000098 public override bool IsOpen
99 {
100 get
101 {
102 if (client == null)
103 {
104 return false;
105 }
106
107 return client.Connected;
108 }
109 }
110
111 public override void Open()
112 {
113 if (IsOpen)
114 {
115 throw new TTransportException(TTransportException.ExceptionType.AlreadyOpen, "Socket already connected");
116 }
117
118 if (String.IsNullOrEmpty(host))
119 {
120 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open null host");
121 }
122
123 if (port <= 0)
124 {
125 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open without port");
126 }
127
128 if (client == null)
129 {
130 InitSocket();
131 }
132
133 client.Connect(host, port);
134 inputStream = client.GetStream();
135 outputStream = client.GetStream();
136 }
137
138 public override void Close()
139 {
140 base.Close();
141 if (client != null)
142 {
143 client.Close();
144 client = null;
145 }
146 }
147 }
148}