David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1 | // |
| 2 | // TServerTransport.cs |
| 3 | // |
| 4 | // Begin: Dec 3, 2007 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 5 | // Authors: |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 6 | // Will Palmeri <wpalmeri@imeem.com> |
| 7 | // |
| 8 | // Copyright (C) 2007 imeem, inc. <http://www.imeem.com> |
| 9 | // All rights reserved. |
| 10 | |
| 11 | using System; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 12 | using System.Net.Sockets; |
| 13 | |
| 14 | |
| 15 | namespace 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 Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 35 | * Whether or not to wrap new TSocket connections in buffers |
| 36 | */ |
| 37 | private bool useBufferedSockets = false; |
| 38 | |
| 39 | /** |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 40 | * 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 Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 68 | :this(port, clientTimeout, false) |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | public TServerSocket(int port, int clientTimeout, bool useBufferedSockets) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 73 | { |
| 74 | this.port = port; |
| 75 | this.clientTimeout = clientTimeout; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 76 | this.useBufferedSockets = useBufferedSockets; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 77 | try |
| 78 | { |
| 79 | // Make server socket |
| 80 | server = new TcpListener(System.Net.IPAddress.Any, this.port); |
| 81 | } |
David Reiss | cee1e08 | 2008-04-02 22:10:09 +0000 | [diff] [blame] | 82 | catch (Exception) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 83 | { |
| 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 Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 100 | throw new TTransportException("Could not accept on listening socket: " + sx.Message); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 101 | } |
| 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 Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 116 | if (useBufferedSockets) |
| 117 | { |
| 118 | TBufferedTransport result3 = new TBufferedTransport(result2); |
| 119 | return result3; |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | return result2; |
| 124 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 125 | } |
| 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 Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 142 | throw new TTransportException("WARNING: Could not close server socket: " + ex); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 143 | } |
| 144 | server = null; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |