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

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,12 +1,6 @@
<Properties> <Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug_Ubuntu" /> <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="websocket-sharp/Frame/WsFrame.cs"> <MonoDevelop.Ide.Workbench />
<Files>
<File FileName="websocket-sharp/WebSocket.cs" Line="795" Column="25" />
<File FileName="websocket-sharp/Frame/WsFrame.cs" Line="278" Column="1" />
<File FileName="websocket-sharp/Ext.cs" Line="121" Column="23" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints> <MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore /> <BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints> </MonoDevelop.Ide.DebuggingService.Breakpoints>

View File

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

View File

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

View File

@ -42,7 +42,7 @@ namespace WebSocketSharp.Server
#region Property #region Property
public bool IsBinded { get; private set; } public bool IsBound { get; private set; }
#endregion #endregion
@ -50,7 +50,7 @@ namespace WebSocketSharp.Server
public WebSocketService() public WebSocketService()
{ {
IsBinded = false; IsBound = false;
} }
#endregion #endregion
@ -64,10 +64,13 @@ namespace WebSocketSharp.Server
_server.AddService(this); _server.AddService(this);
}; };
// _socket.OnClose += (sender, e) => _socket.OnClose += (sender, e) =>
// { {
// _server.RemoveService(this); if (_server.State == WsServerState.START)
// }; {
_server.RemoveService(this);
}
};
} }
#endregion #endregion
@ -105,7 +108,7 @@ namespace WebSocketSharp.Server
_socket.OnError += onError; _socket.OnError += onError;
_socket.OnClose += onClose; _socket.OnClose += onClose;
IsBinded = true; IsBound = true;
} }
public void BPing() public void BPing()
@ -115,52 +118,52 @@ namespace WebSocketSharp.Server
public void BPing(string data) public void BPing(string data)
{ {
if (IsBinded) _server.Ping(data); if (IsBound) _server.Ping(data);
} }
public void Close() public void Close()
{ {
if (IsBinded) _socket.Close(); if (IsBound) _socket.Close();
} }
public void Close(CloseStatusCode code, string reason) public void Close(CloseStatusCode code, string reason)
{ {
if (IsBinded) _socket.Close(code, reason); if (IsBound) _socket.Close(code, reason);
} }
public void Open() public void Open()
{ {
if (IsBinded) _socket.Connect(); if (IsBound) _socket.Connect();
} }
public void Ping() public void Ping()
{ {
if (IsBinded) _socket.Ping(); if (IsBound) _socket.Ping();
} }
public void Ping(string data) public void Ping(string data)
{ {
if (IsBinded) _socket.Ping(data); if (IsBound) _socket.Ping(data);
} }
public void Publish(byte[] data) public void Publish(byte[] data)
{ {
if (IsBinded) _server.Publish(data); if (IsBound) _server.Publish(data);
} }
public void Publish(string data) public void Publish(string data)
{ {
if (IsBinded) _server.Publish(data); if (IsBound) _server.Publish(data);
} }
public void Send(byte[] data) public void Send(byte[] data)
{ {
if (IsBinded) _socket.Send(data); if (IsBound) _socket.Send(data);
} }
public void Send(string data) public void Send(string data)
{ {
if (IsBinded) _socket.Send(data); if (IsBound) _socket.Send(data);
} }
#endregion #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) 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() public int ReadByte()
{ {
return _innerStream.ReadByte(); lock (_forRead)
{
return _innerStream.ReadByte();
}
} }
public WsFrame ReadFrame() public WsFrame ReadFrame()
@ -92,12 +104,18 @@ namespace WebSocketSharp.Stream
public void Write(byte[] buffer, int offset, int count) 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) public void WriteByte(byte value)
{ {
_innerStream.WriteByte(value); lock (_forWrite)
{
_innerStream.WriteByte(value);
}
} }
public void WriteFrame(WsFrame frame) public void WriteFrame(WsFrame frame)

View File

@ -77,6 +77,7 @@
<Compile Include="Server\IWebSocketServer.cs" /> <Compile Include="Server\IWebSocketServer.cs" />
<Compile Include="Server\WebSocketServer.cs" /> <Compile Include="Server\WebSocketServer.cs" />
<Compile Include="Server\WebSocketService.cs" /> <Compile Include="Server\WebSocketService.cs" />
<Compile Include="Server\WsServerState.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup> <ItemGroup>

Binary file not shown.