blob: 3559ce722d2054f8f637758b5bf1670fc94f7043 [file] [log] [blame]
David Reiss7f42bcf2008-01-11 20:59:12 +00001//
2// TServerTransport.cs
3//
4// Begin: Dec 3, 2007
David Reiss0c90f6f2008-02-06 22:18:40 +00005// Authors:
David Reiss7f42bcf2008-01-11 20:59:12 +00006// Will Palmeri <wpalmeri@imeem.com>
7//
8// Copyright (C) 2007 imeem, inc. <http://www.imeem.com>
9// All rights reserved.
10
11using System;
David Reiss7f42bcf2008-01-11 20:59:12 +000012using System.Net.Sockets;
13
14
15namespace Thrift.Transport
16{
17 public class TServerSocket : TServerTransport
18 {
19 /**
20 * Underlying server with socket
21 */
22 private TcpListener server = null;
23
24 /**
25 * Port to listen on
26 */
27 private int port = 0;
28
29 /**
30 * Timeout for client sockets from accept
31 */
32 private int clientTimeout = 0;
33
34 /**
David Reiss63191332009-01-06 19:49:22 +000035 * Whether or not to wrap new TSocket connections in buffers
36 */
37 private bool useBufferedSockets = false;
38
39 /**
David Reiss7f42bcf2008-01-11 20:59:12 +000040 * Creates a server socket from underlying socket object
41 */
42 public TServerSocket(TcpListener listener)
43 :this(listener, 0)
44 {
45 }
46
47 /**
48 * Creates a server socket from underlying socket object
49 */
50 public TServerSocket(TcpListener listener, int clientTimeout)
51 {
52 this.server = listener;
53 this.clientTimeout = clientTimeout;
54 }
55
56 /**
57 * Creates just a port listening server socket
58 */
59 public TServerSocket(int port)
60 : this(port, 0)
61 {
62 }
63
64 /**
65 * Creates just a port listening server socket
66 */
67 public TServerSocket(int port, int clientTimeout)
David Reiss63191332009-01-06 19:49:22 +000068 :this(port, clientTimeout, false)
69 {
70 }
71
72 public TServerSocket(int port, int clientTimeout, bool useBufferedSockets)
David Reiss7f42bcf2008-01-11 20:59:12 +000073 {
74 this.port = port;
75 this.clientTimeout = clientTimeout;
David Reiss63191332009-01-06 19:49:22 +000076 this.useBufferedSockets = useBufferedSockets;
David Reiss7f42bcf2008-01-11 20:59:12 +000077 try
78 {
79 // Make server socket
80 server = new TcpListener(System.Net.IPAddress.Any, this.port);
81 }
David Reisscee1e082008-04-02 22:10:09 +000082 catch (Exception)
David Reiss7f42bcf2008-01-11 20:59:12 +000083 {
84 server = null;
85 throw new TTransportException("Could not create ServerSocket on port " + port + ".");
86 }
87 }
88
89 public override void Listen()
90 {
91 // Make sure not to block on accept
92 if (server != null)
93 {
94 try
95 {
96 server.Start();
97 }
98 catch (SocketException sx)
99 {
David Reiss63191332009-01-06 19:49:22 +0000100 throw new TTransportException("Could not accept on listening socket: " + sx.Message);
David Reiss7f42bcf2008-01-11 20:59:12 +0000101 }
102 }
103 }
104
105 protected override TTransport AcceptImpl()
106 {
107 if (server == null)
108 {
109 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
110 }
111 try
112 {
113 TcpClient result = server.AcceptTcpClient();
114 TSocket result2 = new TSocket(result);
115 result2.Timeout = clientTimeout;
David Reiss63191332009-01-06 19:49:22 +0000116 if (useBufferedSockets)
117 {
118 TBufferedTransport result3 = new TBufferedTransport(result2);
119 return result3;
120 }
121 else
122 {
123 return result2;
124 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000125 }
126 catch (Exception ex)
127 {
128 throw new TTransportException(ex.ToString());
129 }
130 }
131
132 public override void Close()
133 {
134 if (server != null)
135 {
136 try
137 {
138 server.Stop();
139 }
140 catch (Exception ex)
141 {
David Reiss63191332009-01-06 19:49:22 +0000142 throw new TTransportException("WARNING: Could not close server socket: " + ex);
David Reiss7f42bcf2008-01-11 20:59:12 +0000143 }
144 server = null;
145 }
146 }
147 }
148}