blob: 18cf1547b8e76960a9bb18badf149831513205ac [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.
18 */
David Reiss7f42bcf2008-01-11 20:59:12 +000019
20using System;
David Reiss7f42bcf2008-01-11 20:59:12 +000021using System.Net.Sockets;
22
23namespace Thrift.Transport
24{
25 public class TSocket : TStreamTransport
26 {
27 private TcpClient client = null;
28 private string host = null;
29 private int port = 0;
30 private int timeout = 0;
31
32 public TSocket(TcpClient client)
33 {
34 this.client = client;
35
36 if (IsOpen)
37 {
38 inputStream = client.GetStream();
39 outputStream = client.GetStream();
40 }
41 }
42
43 public TSocket(string host, int port) : this(host, port, 0)
44 {
45 }
46
47 public TSocket(string host, int port, int timeout)
48 {
49 this.host = host;
50 this.port = port;
51 this.timeout = timeout;
52
53 InitSocket();
54 }
55
56 private void InitSocket()
57 {
58 client = new TcpClient();
59 client.ReceiveTimeout = client.SendTimeout = timeout;
60 }
61
62 public int Timeout
63 {
64 set
65 {
66 client.ReceiveTimeout = client.SendTimeout = timeout = value;
67 }
68 }
69
70 public TcpClient TcpClient
71 {
72 get
73 {
74 return client;
75 }
76 }
77
David Reiss63191332009-01-06 19:49:22 +000078 public string Host
79 {
80 get
81 {
82 return host;
83 }
84 }
85
86 public int Port
87 {
88 get
89 {
90 return port;
91 }
92 }
93
David Reiss7f42bcf2008-01-11 20:59:12 +000094 public override bool IsOpen
95 {
96 get
97 {
98 if (client == null)
99 {
100 return false;
101 }
102
103 return client.Connected;
104 }
105 }
106
107 public override void Open()
108 {
109 if (IsOpen)
110 {
111 throw new TTransportException(TTransportException.ExceptionType.AlreadyOpen, "Socket already connected");
112 }
113
114 if (String.IsNullOrEmpty(host))
115 {
116 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open null host");
117 }
118
119 if (port <= 0)
120 {
121 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open without port");
122 }
123
124 if (client == null)
125 {
126 InitSocket();
127 }
128
129 client.Connect(host, port);
130 inputStream = client.GetStream();
131 outputStream = client.GetStream();
132 }
133
134 public override void Close()
135 {
136 base.Close();
137 if (client != null)
138 {
139 client.Close();
140 client = null;
141 }
142 }
143 }
144}