Added the ReuseAddress property to the HttpServer class (Associated with pull request #73)

This commit is contained in:
sta 2014-10-20 16:00:57 +09:00
parent 4cfdd7e793
commit 7a8967bdcd
5 changed files with 50 additions and 3 deletions

View File

@ -72,6 +72,9 @@ namespace Example3
// Not to remove the inactive WebSocket sessions periodically.
//httpsv.KeepClean = false;
// To resolve to wait for socket in TIME_WAIT state.
//httpsv.ReuseAddress = true;
// Add the WebSocket services.
httpsv.AddWebSocketService<Echo> ("/Echo");
httpsv.AddWebSocketService<Chat> ("/Chat");

View File

@ -83,7 +83,8 @@ namespace WebSocketSharp.Net
int port,
bool secure,
string certificateFolderPath,
X509Certificate2 defaultCertificate)
X509Certificate2 defaultCertificate,
bool reuseAddress)
{
if (secure) {
_secure = secure;
@ -92,13 +93,16 @@ namespace WebSocketSharp.Net
throw new ArgumentException ("No server certificate could be found.");
}
_endpoint = new IPEndPoint (address, port);
_prefixes = new Dictionary<ListenerPrefix, HttpListener> ();
_unregistered = new Dictionary<HttpConnection, HttpConnection> ();
_unregisteredSync = ((ICollection) _unregistered).SyncRoot;
_socket = new Socket (address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
if (reuseAddress)
_socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
_endpoint = new IPEndPoint (address, port);
_socket.Bind (_endpoint);
_socket.Listen (500);

View File

@ -107,7 +107,8 @@ namespace WebSocketSharp.Net
port,
secure,
httpListener.CertificateFolderPath,
httpListener.DefaultCertificate);
httpListener.DefaultCertificate,
httpListener.ReuseAddress);
eps[port] = epl;
}

View File

@ -70,6 +70,7 @@ namespace WebSocketSharp.Net
private bool _listening;
private HttpListenerPrefixCollection _prefixes;
private string _realm;
private bool _reuseAddress;
private List<ListenerAsyncResult> _waitQueue;
private object _waitQueueSync;
@ -109,6 +110,16 @@ namespace WebSocketSharp.Net
}
}
internal bool ReuseAddress {
get {
return _reuseAddress;
}
set {
_reuseAddress = value;
}
}
#endregion
#region Public Properties

View File

@ -306,6 +306,34 @@ namespace WebSocketSharp.Server
}
}
/// <summary>
/// Gets or sets a value indicating whether the server is allowed to be bound to an address
/// that is already in use.
/// </summary>
/// <remarks>
/// If you would like to resolve to wait for socket in <c>TIME_WAIT</c> state, you should set
/// this property to <c>true</c>.
/// </remarks>
/// <value>
/// <c>true</c> if the server is allowed to be bound to an address that is already in use;
/// otherwise, <c>false</c>. The default value is <c>false</c>.
/// </value>
public bool ReuseAddress {
get {
return _listener.ReuseAddress;
}
set {
var msg = _state.CheckIfStartable ();
if (msg != null) {
_logger.Error (msg);
return;
}
_listener.ReuseAddress = value;
}
}
/// <summary>
/// Gets or sets the document root path of the server.
/// </summary>