[banshee] Catch SocketException to prevent crash
- From: Gabriel Burt <gburt src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [banshee] Catch SocketException to prevent crash
- Date: Fri, 20 Nov 2009 19:41:54 +0000 (UTC)
commit bfb7522925c2189c4ee8231aebeed951d9871093
Author: Neil Loknath <neil loknath gmail com>
Date: Sat Nov 14 23:47:50 2009 -0700
Catch SocketException to prevent crash
.../Banshee.Services/Banshee.Web/BaseHttpServer.cs | 50 +++++++++++++++-----
.../Banshee.Daap/DaapProxyWebServer.cs | 15 ++++--
2 files changed, 48 insertions(+), 17 deletions(-)
---
diff --git a/src/Core/Banshee.Services/Banshee.Web/BaseHttpServer.cs b/src/Core/Banshee.Services/Banshee.Web/BaseHttpServer.cs
index ee90c6b..ae8e2c3 100644
--- a/src/Core/Banshee.Services/Banshee.Web/BaseHttpServer.cs
+++ b/src/Core/Banshee.Services/Banshee.Web/BaseHttpServer.cs
@@ -39,11 +39,14 @@ using System.Threading;
using System.Collections;
using System.Collections.Generic;
+using Hyena;
+
namespace Banshee.Web
{
public abstract class BaseHttpServer
{
protected Socket server;
+ private bool running;
private int backlog;
private ushort port;
@@ -65,6 +68,14 @@ namespace Banshee.Web
get { return name; }
}
+ public bool IsBound {
+ get { return server != null && server.IsBound; }
+ }
+
+ public bool IsRunning {
+ get { return running; }
+ }
+
private EndPoint end_point = new IPEndPoint (IPAddress.Any, 80);
protected EndPoint EndPoint {
get { return end_point; }
@@ -72,19 +83,13 @@ namespace Banshee.Web
if (value == null) {
throw new ArgumentNullException ("end_point");
}
- if (running) {
+ if (IsBound) {
throw new InvalidOperationException ("Cannot set EndPoint while running.");
}
end_point = value;
}
}
- private bool running;
- public bool Running {
- get { return running; }
- protected set { running = value; }
- }
-
private int chunk_length = 8192;
public int ChunkLength {
get { return chunk_length; }
@@ -99,7 +104,7 @@ namespace Banshee.Web
Start (10);
}
- public virtual void Start (int backlog)
+ public void Start (int backlog)
{
if (backlog < 0) {
throw new ArgumentOutOfRangeException ("backlog");
@@ -133,11 +138,19 @@ namespace Banshee.Web
private void ServerLoop ()
{
- server = new Socket (this.EndPoint.AddressFamily, SocketType.Stream, ProtocolType.IP);
- server.Bind (this.EndPoint);
+ if (!BindServerSocket ()) {
+ running = false;
+ return;
+ }
+
server.Listen (backlog);
- port = (ushort)(server.LocalEndPoint as IPEndPoint).Port;
+ IPEndPoint ip_endpoint;
+ if ((ip_endpoint = server.LocalEndPoint as IPEndPoint) != null) {
+ port = (ushort) ip_endpoint.Port;
+ }
+
+ Log.DebugFormat ("{0} listening for connections on port {1}", name, port);
while (true) {
try {
@@ -162,13 +175,26 @@ namespace Banshee.Web
while (HandleRequest(client));
} catch (IOException) {
} catch (Exception e) {
- Hyena.Log.Exception (e);
+ Log.Exception (e);
} finally {
clients.Remove (client);
client.Close ();
}
}
+ protected virtual bool BindServerSocket ()
+ {
+ server = new Socket (this.EndPoint.AddressFamily, SocketType.Stream, ProtocolType.IP);
+ try {
+ server.Bind (this.EndPoint);
+ } catch (System.Net.Sockets.SocketException e) {
+ Log.Exception (e);
+ return false;
+ }
+
+ return true;
+ }
+
protected virtual long ParseRangeRequest (string line)
{
long offset = 0;
diff --git a/src/Extensions/Banshee.Daap/Banshee.Daap/DaapProxyWebServer.cs b/src/Extensions/Banshee.Daap/Banshee.Daap/DaapProxyWebServer.cs
index 2f25f59..6b49587 100644
--- a/src/Extensions/Banshee.Daap/Banshee.Daap/DaapProxyWebServer.cs
+++ b/src/Extensions/Banshee.Daap/Banshee.Daap/DaapProxyWebServer.cs
@@ -53,14 +53,19 @@ namespace Banshee.Daap
{
}
- public override void Start (int backlog)
+ protected override bool BindServerSocket ()
{
- try {
- base.Start (backlog);
- } catch (System.Net.Sockets.SocketException) {
+ if (!base.BindServerSocket ()) {
EndPoint = new IPEndPoint(IPAddress.Any, 0);
- base.Start (backlog);
+ try {
+ server.Bind (EndPoint);
+ } catch (System.Net.Sockets.SocketException e) {
+ Hyena.Log.Exception (e);
+ return false;
+ }
}
+
+ return true;
}
public void RegisterDatabase(DAAP.Database database)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]