r1460@opsdev009 (orig r77478): dreiss | 2008-01-11 12:59:12 -0800
[amiethrift.git] / lib / csharp / src / Transport / TSocket.cs
blobc05d587127983eee7319f583c91f6aacd805f441
1 //
2 // TSocket.cs
3 //
4 // Begin: Aug 19, 2007
5 // Authors:
6 // Todd Berman <tberman@imeem.com>
7 //
8 // Copyright (C) 2007 imeem, inc. <http://www.imeem.com>
9 // All rights reserved.
12 using System;
13 using System.Collections.Generic;
14 using System.Text;
15 using System.Net.Sockets;
17 namespace Thrift.Transport
19 public class TSocket : TStreamTransport
21 private TcpClient client = null;
22 private string host = null;
23 private int port = 0;
24 private int timeout = 0;
26 public TSocket(TcpClient client)
28 this.client = client;
30 if (IsOpen)
32 inputStream = client.GetStream();
33 outputStream = client.GetStream();
37 public TSocket(string host, int port) : this(host, port, 0)
41 public TSocket(string host, int port, int timeout)
43 this.host = host;
44 this.port = port;
45 this.timeout = timeout;
47 InitSocket();
50 private void InitSocket()
52 client = new TcpClient();
53 client.ReceiveTimeout = client.SendTimeout = timeout;
56 public int Timeout
58 set
60 client.ReceiveTimeout = client.SendTimeout = timeout = value;
64 public TcpClient TcpClient
66 get
68 return client;
72 public override bool IsOpen
74 get
76 if (client == null)
78 return false;
81 return client.Connected;
85 public override void Open()
87 if (IsOpen)
89 throw new TTransportException(TTransportException.ExceptionType.AlreadyOpen, "Socket already connected");
92 if (String.IsNullOrEmpty(host))
94 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open null host");
97 if (port <= 0)
99 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "Cannot open without port");
102 if (client == null)
104 InitSocket();
107 client.Connect(host, port);
108 inputStream = client.GetStream();
109 outputStream = client.GetStream();
112 public override void Close()
114 base.Close();
115 if (client != null)
117 client.Close();
118 client = null;