blob: a6caf90c0e63a1a74addec0888b89023e2320e2f [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;
12using System.Collections.Generic;
13using System.Text;
14using System.Net.Sockets;
15
16
17namespace Thrift.Transport
18{
19 public class TServerSocket : TServerTransport
20 {
21 /**
22 * Underlying server with socket
23 */
24 private TcpListener server = null;
25
26 /**
27 * Port to listen on
28 */
29 private int port = 0;
30
31 /**
32 * Timeout for client sockets from accept
33 */
34 private int clientTimeout = 0;
35
36 /**
David Reiss63191332009-01-06 19:49:22 +000037 * Whether or not to wrap new TSocket connections in buffers
38 */
39 private bool useBufferedSockets = false;
40
41 /**
David Reiss7f42bcf2008-01-11 20:59:12 +000042 * Creates a server socket from underlying socket object
43 */
44 public TServerSocket(TcpListener listener)
45 :this(listener, 0)
46 {
47 }
48
49 /**
50 * Creates a server socket from underlying socket object
51 */
52 public TServerSocket(TcpListener listener, int clientTimeout)
53 {
54 this.server = listener;
55 this.clientTimeout = clientTimeout;
56 }
57
58 /**
59 * Creates just a port listening server socket
60 */
61 public TServerSocket(int port)
62 : this(port, 0)
63 {
64 }
65
66 /**
67 * Creates just a port listening server socket
68 */
69 public TServerSocket(int port, int clientTimeout)
David Reiss63191332009-01-06 19:49:22 +000070 :this(port, clientTimeout, false)
71 {
72 }
73
74 public TServerSocket(int port, int clientTimeout, bool useBufferedSockets)
David Reiss7f42bcf2008-01-11 20:59:12 +000075 {
76 this.port = port;
77 this.clientTimeout = clientTimeout;
David Reiss63191332009-01-06 19:49:22 +000078 this.useBufferedSockets = useBufferedSockets;
David Reiss7f42bcf2008-01-11 20:59:12 +000079 try
80 {
81 // Make server socket
82 server = new TcpListener(System.Net.IPAddress.Any, this.port);
83 }
David Reisscee1e082008-04-02 22:10:09 +000084 catch (Exception)
David Reiss7f42bcf2008-01-11 20:59:12 +000085 {
86 server = null;
87 throw new TTransportException("Could not create ServerSocket on port " + port + ".");
88 }
89 }
90
91 public override void Listen()
92 {
93 // Make sure not to block on accept
94 if (server != null)
95 {
96 try
97 {
98 server.Start();
99 }
100 catch (SocketException sx)
101 {
David Reiss63191332009-01-06 19:49:22 +0000102 throw new TTransportException("Could not accept on listening socket: " + sx.Message);
David Reiss7f42bcf2008-01-11 20:59:12 +0000103 }
104 }
105 }
106
107 protected override TTransport AcceptImpl()
108 {
109 if (server == null)
110 {
111 throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket.");
112 }
113 try
114 {
115 TcpClient result = server.AcceptTcpClient();
116 TSocket result2 = new TSocket(result);
117 result2.Timeout = clientTimeout;
David Reiss63191332009-01-06 19:49:22 +0000118 if (useBufferedSockets)
119 {
120 TBufferedTransport result3 = new TBufferedTransport(result2);
121 return result3;
122 }
123 else
124 {
125 return result2;
126 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000127 }
128 catch (Exception ex)
129 {
130 throw new TTransportException(ex.ToString());
131 }
132 }
133
134 public override void Close()
135 {
136 if (server != null)
137 {
138 try
139 {
140 server.Stop();
141 }
142 catch (Exception ex)
143 {
David Reiss63191332009-01-06 19:49:22 +0000144 throw new TTransportException("WARNING: Could not close server socket: " + ex);
David Reiss7f42bcf2008-01-11 20:59:12 +0000145 }
146 server = null;
147 }
148 }
149 }
150}