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; |
| 12 | using System.Collections.Generic; |
| 13 | using System.Text; |
| 14 | using System.Net.Sockets; |
| 15 | |
| 16 | |
| 17 | namespace 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 | /** |
| 37 | * Creates a server socket from underlying socket object |
| 38 | */ |
| 39 | public TServerSocket(TcpListener listener) |
| 40 | :this(listener, 0) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Creates a server socket from underlying socket object |
| 46 | */ |
| 47 | public TServerSocket(TcpListener listener, int clientTimeout) |
| 48 | { |
| 49 | this.server = listener; |
| 50 | this.clientTimeout = clientTimeout; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Creates just a port listening server socket |
| 55 | */ |
| 56 | public TServerSocket(int port) |
| 57 | : this(port, 0) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Creates just a port listening server socket |
| 63 | */ |
| 64 | public TServerSocket(int port, int clientTimeout) |
| 65 | { |
| 66 | this.port = port; |
| 67 | this.clientTimeout = clientTimeout; |
| 68 | try |
| 69 | { |
| 70 | // Make server socket |
| 71 | server = new TcpListener(System.Net.IPAddress.Any, this.port); |
| 72 | } |
David Reiss | cee1e08 | 2008-04-02 22:10:09 +0000 | [diff] [blame] | 73 | catch (Exception) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 74 | { |
| 75 | server = null; |
| 76 | throw new TTransportException("Could not create ServerSocket on port " + port + "."); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | public override void Listen() |
| 81 | { |
| 82 | // Make sure not to block on accept |
| 83 | if (server != null) |
| 84 | { |
| 85 | try |
| 86 | { |
| 87 | server.Start(); |
| 88 | } |
| 89 | catch (SocketException sx) |
| 90 | { |
| 91 | Console.Error.WriteLine(sx); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | protected override TTransport AcceptImpl() |
| 97 | { |
| 98 | if (server == null) |
| 99 | { |
| 100 | throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No underlying server socket."); |
| 101 | } |
| 102 | try |
| 103 | { |
| 104 | TcpClient result = server.AcceptTcpClient(); |
| 105 | TSocket result2 = new TSocket(result); |
| 106 | result2.Timeout = clientTimeout; |
| 107 | return result2; |
| 108 | } |
| 109 | catch (Exception ex) |
| 110 | { |
| 111 | throw new TTransportException(ex.ToString()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | public override void Close() |
| 116 | { |
| 117 | if (server != null) |
| 118 | { |
| 119 | try |
| 120 | { |
| 121 | server.Stop(); |
| 122 | } |
| 123 | catch (Exception ex) |
| 124 | { |
| 125 | Console.Error.WriteLine("WARNING: Could not close server socket: " + ex); |
| 126 | } |
| 127 | server = null; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | } |