Fix due to the added WsServerState.cs

This commit is contained in:
sta
2012-08-13 11:30:35 +09:00
parent b76418c482
commit 83730f913a
50 changed files with 99 additions and 29 deletions

View File

@@ -33,6 +33,8 @@ namespace WebSocketSharp.Server
{
public interface IWebSocketServer
{
WsServerState State { get; }
void AddService(WebSocketService service);
void CloseServices(CloseStatusCode code, string reason);
void Ping(string data);

View File

@@ -45,6 +45,7 @@ namespace WebSocketSharp.Server
#region Private Fields
private SynchronizedCollection<WebSocketService> _services;
private WsServerState _state;
private TcpListener _tcpListener;
private Uri _uri;
@@ -67,6 +68,11 @@ namespace WebSocketSharp.Server
get { return Endpoint.Port; }
}
public WsServerState State
{
get { return _state; }
}
public string Url
{
get { return _uri.ToString(); }
@@ -107,6 +113,7 @@ namespace WebSocketSharp.Server
_tcpListener = new TcpListener(IPAddress.Any, port);
_services = new SynchronizedCollection<WebSocketService>();
_state = WsServerState.READY;
}
#endregion
@@ -248,12 +255,17 @@ namespace WebSocketSharp.Server
{
_tcpListener.Start();
_tcpListener.BeginAcceptTcpClient(acceptClient, _tcpListener);
_state = WsServerState.START;
}
public void Stop()
{
_state = WsServerState.SHUTDOWN;
_tcpListener.Stop();
CloseServices();
_state = WsServerState.STOP;
}
#endregion

View File

@@ -42,7 +42,7 @@ namespace WebSocketSharp.Server
#region Property
public bool IsBinded { get; private set; }
public bool IsBound { get; private set; }
#endregion
@@ -50,7 +50,7 @@ namespace WebSocketSharp.Server
public WebSocketService()
{
IsBinded = false;
IsBound = false;
}
#endregion
@@ -64,10 +64,13 @@ namespace WebSocketSharp.Server
_server.AddService(this);
};
// _socket.OnClose += (sender, e) =>
// {
// _server.RemoveService(this);
// };
_socket.OnClose += (sender, e) =>
{
if (_server.State == WsServerState.START)
{
_server.RemoveService(this);
}
};
}
#endregion
@@ -105,7 +108,7 @@ namespace WebSocketSharp.Server
_socket.OnError += onError;
_socket.OnClose += onClose;
IsBinded = true;
IsBound = true;
}
public void BPing()
@@ -115,52 +118,52 @@ namespace WebSocketSharp.Server
public void BPing(string data)
{
if (IsBinded) _server.Ping(data);
if (IsBound) _server.Ping(data);
}
public void Close()
{
if (IsBinded) _socket.Close();
if (IsBound) _socket.Close();
}
public void Close(CloseStatusCode code, string reason)
{
if (IsBinded) _socket.Close(code, reason);
if (IsBound) _socket.Close(code, reason);
}
public void Open()
{
if (IsBinded) _socket.Connect();
if (IsBound) _socket.Connect();
}
public void Ping()
{
if (IsBinded) _socket.Ping();
if (IsBound) _socket.Ping();
}
public void Ping(string data)
{
if (IsBinded) _socket.Ping(data);
if (IsBound) _socket.Ping(data);
}
public void Publish(byte[] data)
{
if (IsBinded) _server.Publish(data);
if (IsBound) _server.Publish(data);
}
public void Publish(string data)
{
if (IsBinded) _server.Publish(data);
if (IsBound) _server.Publish(data);
}
public void Send(byte[] data)
{
if (IsBinded) _socket.Send(data);
if (IsBound) _socket.Send(data);
}
public void Send(string data)
{
if (IsBinded) _socket.Send(data);
if (IsBound) _socket.Send(data);
}
#endregion

View File

@@ -0,0 +1,40 @@
#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,12 +74,24 @@ namespace WebSocketSharp.Stream
public int Read(byte[] buffer, int offset, int size)
{
return _innerStream.Read(buffer, offset, size);
lock (_forRead)
{
var readLen = _innerStream.Read(buffer, offset, size);
if (readLen < size)
{
var msg = String.Format("Data can not be read from {0}.", typeof(TStream).Name);
throw new IOException(msg);
}
return readLen;
}
}
public int ReadByte()
{
return _innerStream.ReadByte();
lock (_forRead)
{
return _innerStream.ReadByte();
}
}
public WsFrame ReadFrame()
@@ -92,12 +104,18 @@ namespace WebSocketSharp.Stream
public void Write(byte[] buffer, int offset, int count)
{
_innerStream.Write(buffer, offset, count);
lock (_forWrite)
{
_innerStream.Write(buffer, offset, count);
}
}
public void WriteByte(byte value)
{
_innerStream.WriteByte(value);
lock (_forWrite)
{
_innerStream.WriteByte(value);
}
}
public void WriteFrame(WsFrame frame)

View File

@@ -77,6 +77,7 @@
<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.