Fix due to the moved some methods from WebSocketServer to WebSocketService

This commit is contained in:
sta
2012-08-24 12:07:58 +09:00
parent 5f4f7485aa
commit b3dc165d83
50 changed files with 143 additions and 226 deletions

View File

@@ -1,49 +0,0 @@
#region MIT License
/**
* IWebSocketServer.cs
*
* The MIT License
*
* Copyright (c) 2012 sta.blockhead
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#endregion
using System;
using System.Collections.Generic;
using WebSocketSharp.Frame;
namespace WebSocketSharp.Server
{
public interface IWebSocketServer
{
WsServerState State { get; }
void AddService(string id, WebSocketService service);
Dictionary<string, bool> PingAround(string data);
void Publish<TData>(TData data);
void RemoveService(string id);
void SendTo<TData>(string id, TData data);
void SendTo<TData>(IEnumerable<string> group, TData data);
void Start();
void Stop();
void StopServices(CloseStatusCode code, string reason);
}
}

View File

@@ -34,20 +34,17 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using WebSocketSharp.Frame;
namespace WebSocketSharp.Server
{
public class WebSocketServer<T> : IWebSocketServer
public class WebSocketServer<T>
where T : WebSocketService, new()
{
#region Private Fields
private object _forServices;
private Dictionary<string, WebSocketService> _services;
private WsServerState _state;
private TcpListener _tcpListener;
private Uri _uri;
@@ -70,11 +67,6 @@ namespace WebSocketSharp.Server
get { return Endpoint.Port; }
}
public WsServerState State
{
get { return _state; }
}
public string Url
{
get { return _uri.ToString(); }
@@ -116,9 +108,7 @@ namespace WebSocketSharp.Server
}
_tcpListener = new TcpListener(IPAddress.Any, port);
_forServices = new object();
_services = new Dictionary<string, WebSocketService>();
_state = WsServerState.READY;
}
#endregion
@@ -139,7 +129,7 @@ namespace WebSocketSharp.Server
TcpClient client = listener.EndAcceptTcpClient(ar);
WebSocket socket = new WebSocket(_uri, client);
T service = new T();
service.Bind(this, socket);
service.Bind(socket, _services);
service.Start();
}
catch (ObjectDisposedException)
@@ -180,116 +170,16 @@ namespace WebSocketSharp.Server
#region Public Methods
public void AddService(string id, WebSocketService service)
{
lock (_forServices)
{
_services.Add(id, service);
}
}
public Dictionary<string, bool> PingAround()
{
return PingAround(String.Empty);
}
public Dictionary<string, bool> PingAround(string data)
{
var result = new Dictionary<string, bool>();
lock (_forServices)
{
foreach (WebSocketService service in _services.Values)
{
result.Add(service.ID, service.Ping(data));
}
}
return result;
}
public void Publish<TData>(TData data)
{
WaitCallback broadcast = (state) =>
{
lock (_forServices)
{
SendTo(_services.Keys, data);
}
};
ThreadPool.QueueUserWorkItem(broadcast);
}
public void RemoveService(string id)
{
lock (_forServices)
{
_services.Remove(id);
}
}
public void SendTo<TData>(string id, TData data)
{
if (typeof(TData) != typeof(string) &&
typeof(TData) != typeof(byte[]))
{
var msg = "Type of data must be string or byte[].";
throw new ArgumentException(msg);
}
lock (_forServices)
{
WebSocketService service;
if (_services.TryGetValue(id, out service))
{
if (typeof(TData) == typeof(string))
{
string data_ = (string)(object)data;
service.Send(data_);
}
else if (typeof(TData) == typeof(byte[]))
{
byte[] data_ = (byte[])(object)data;
service.Send(data_);
}
}
}
}
public void SendTo<TData>(IEnumerable<string> group, TData data)
{
if (typeof(TData) != typeof(string) &&
typeof(TData) != typeof(byte[]))
{
var msg = "Type of data must be string or byte[].";
throw new ArgumentException(msg);
}
lock (_forServices)
{
foreach (string id in group)
{
SendTo(id, data);
}
}
}
public void Start()
{
_tcpListener.Start();
_tcpListener.BeginAcceptTcpClient(acceptClient, _tcpListener);
_state = WsServerState.START;
}
public void Stop()
{
_state = WsServerState.SHUTDOWN;
_tcpListener.Stop();
StopServices();
_state = WsServerState.STOP;
}
public void StopServices()
@@ -299,7 +189,7 @@ namespace WebSocketSharp.Server
public void StopServices(CloseStatusCode code, string reason)
{
lock (_forServices)
lock (((ICollection)_services).SyncRoot)
{
foreach (WebSocketService service in _services.Values)
{

View File

@@ -27,7 +27,9 @@
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using WebSocketSharp.Frame;
namespace WebSocketSharp.Server
@@ -36,8 +38,8 @@ namespace WebSocketSharp.Server
{
#region Private Fields
private IWebSocketServer _server;
private WebSocket _socket;
private Dictionary<string, WebSocketService> _services;
private WebSocket _socket;
#endregion
@@ -45,6 +47,7 @@ namespace WebSocketSharp.Server
public string ID { get; private set; }
public bool IsBound { get; private set; }
public bool IsStop { get; private set; }
#endregion
@@ -54,12 +57,21 @@ namespace WebSocketSharp.Server
{
ID = String.Empty;
IsBound = false;
IsStop = false;
}
#endregion
#region Private Method
private void addService(string id, WebSocketService service)
{
lock (((ICollection)_services).SyncRoot)
{
_services.Add(id, service);
}
}
private string getNewID()
{
return Guid.NewGuid().ToString("N");
@@ -70,18 +82,26 @@ namespace WebSocketSharp.Server
_socket.OnOpen += (sender, e) =>
{
ID = getNewID();
_server.AddService(ID, this);
addService(ID, this);
};
_socket.OnClose += (sender, e) =>
{
if (_server.State == WsServerState.START)
if (!IsStop)
{
_server.RemoveService(ID);
removeService(ID);
}
};
}
private void removeService(string id)
{
lock (((ICollection)_services).SyncRoot)
{
_services.Remove(id);
}
}
#endregion
#region Protected Methods
@@ -106,10 +126,10 @@ namespace WebSocketSharp.Server
#region Public Methods
public void Bind(IWebSocketServer server, WebSocket socket)
public void Bind(WebSocket socket, Dictionary<string, WebSocketService> services)
{
_server = server;
_socket = socket;
_socket = socket;
_services = services;
defaultBind();
_socket.OnOpen += onOpen;
@@ -120,6 +140,18 @@ namespace WebSocketSharp.Server
IsBound = true;
}
public bool Ping()
{
return Ping(String.Empty);
}
public bool Ping(string data)
{
return IsBound
? _socket.Ping(data)
: false;
}
public Dictionary<string, bool> PingAround()
{
return PingAround(String.Empty);
@@ -127,25 +159,67 @@ namespace WebSocketSharp.Server
public Dictionary<string, bool> PingAround(string data)
{
if (IsBound) return _server.PingAround(data);
return null;
if (!IsBound) return null;
lock (((ICollection)_services).SyncRoot)
{
return PingTo(_services.Keys, data);
}
}
public bool Ping()
public bool PingTo(string id)
{
if (IsBound) return _socket.Ping();
return false;
return PingTo(id, String.Empty);
}
public bool Ping(string data)
public Dictionary<string, bool> PingTo(IEnumerable<string> group)
{
if (IsBound) return _socket.Ping(data);
return false;
return PingTo(group, String.Empty);
}
public bool PingTo(string id, string data)
{
if (!IsBound) return false;
lock (((ICollection)_services).SyncRoot)
{
WebSocketService service;
return _services.TryGetValue(id, out service)
? service.Ping(data)
: false;
}
}
public Dictionary<string, bool> PingTo(IEnumerable<string> group, string data)
{
if (!IsBound) return null;
var result = new Dictionary<string, bool>();
lock (((ICollection)_services).SyncRoot)
{
foreach (string id in group)
{
result.Add(id, PingTo(id, data));
}
}
return result;
}
public void Publish<TData>(TData data)
{
if (IsBound) _server.Publish(data);
if (!IsBound) return;
WaitCallback broadcast = (state) =>
{
lock (((ICollection)_services).SyncRoot)
{
SendTo(_services.Keys, data);
}
};
ThreadPool.QueueUserWorkItem(broadcast);
}
public void Send(byte[] data)
@@ -160,12 +234,53 @@ namespace WebSocketSharp.Server
public void SendTo<TData>(string id, TData data)
{
if (IsBound) _server.SendTo(id, data);
if (!IsBound) return;
if (typeof(TData) != typeof(string) &&
typeof(TData) != typeof(byte[]))
{
var msg = "Type of data must be string or byte[].";
throw new ArgumentException(msg);
}
lock (((ICollection)_services).SyncRoot)
{
WebSocketService service;
if (_services.TryGetValue(id, out service))
{
if (typeof(TData) == typeof(string))
{
string data_ = (string)(object)data;
service.Send(data_);
}
else if (typeof(TData) == typeof(byte[]))
{
byte[] data_ = (byte[])(object)data;
service.Send(data_);
}
}
}
}
public void SendTo<TData>(IEnumerable<string> group, TData data)
{
if (IsBound) _server.SendTo(group, data);
if (!IsBound) return;
if (typeof(TData) != typeof(string) &&
typeof(TData) != typeof(byte[]))
{
var msg = "Type of data must be string or byte[].";
throw new ArgumentException(msg);
}
lock (((ICollection)_services).SyncRoot)
{
foreach (string id in group)
{
SendTo(id, data);
}
}
}
public void Start()
@@ -175,12 +290,15 @@ namespace WebSocketSharp.Server
public void Stop()
{
if (IsBound) _socket.Close();
Stop(CloseStatusCode.NORMAL, String.Empty);
}
public void Stop(CloseStatusCode code, string reason)
{
if (IsBound) _socket.Close(code, reason);
if (!IsBound || IsStop) return;
IsStop = true;
_socket.Close(code, reason);
}
#endregion

View File

@@ -1,40 +0,0 @@
#region MIT License
/**
* WsServerState.cs
*
* The MIT License
*
* Copyright (c) 2012 sta.blockhead
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#endregion
using System;
namespace WebSocketSharp.Server
{
public enum WsServerState
{
READY,
START,
SHUTDOWN,
STOP
}
}

View File

@@ -74,10 +74,8 @@
<Compile Include="Frame\Rsv.cs" />
<Compile Include="ErrorEventArgs.cs" />
<Compile Include="WebSocket.cs" />
<Compile Include="Server\IWebSocketServer.cs" />
<Compile Include="Server\WebSocketServer.cs" />
<Compile Include="Server\WebSocketService.cs" />
<Compile Include="Server\WsServerState.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>

Binary file not shown.