Fix for issue #4

This commit is contained in:
sta 2012-08-19 16:41:59 +09:00
parent 0f0a05feb2
commit 0bea2dd623
48 changed files with 37 additions and 29 deletions

Binary file not shown.

View File

@ -84,7 +84,10 @@ namespace Example
ws.OnMessage += (sender, e) => ws.OnMessage += (sender, e) =>
{ {
enNfMessage("[WebSocket] Message", e.Data, "notification-message-im"); if (!String.IsNullOrEmpty(e.Data))
{
enNfMessage("[WebSocket] Message", e.Data, "notification-message-im");
}
}; };
ws.OnError += (sender, e) => ws.OnError += (sender, e) =>
@ -112,7 +115,8 @@ namespace Example
Console.Write("> "); Console.Write("> ");
data = Console.ReadLine(); data = Console.ReadLine();
if (data == "exit" || !ws.IsConnected) if (data == "exit")
//if (data == "exit" || !ws.IsConnected)
{ {
break; break;
} }

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,5 +1,5 @@
<Properties> <Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release_Ubuntu" /> <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench /> <MonoDevelop.Ide.Workbench />
<MonoDevelop.Ide.DebuggingService.Breakpoints> <MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore /> <BreakpointStore />

View File

@ -111,9 +111,18 @@ namespace WebSocketSharp.Frame
{ {
Fin = fin; Fin = fin;
Opcode = opcode; Opcode = opcode;
Masked = mask;
ulong dataLength = payloadData.Length; ulong dataLength = payloadData.Length;
if (dataLength == 0)
{
Masked = Mask.UNMASK;
}
else
{
Masked = mask;
}
if (dataLength < 126) if (dataLength < 126)
{ {
PayloadLen = (byte)dataLength; PayloadLen = (byte)dataLength;
@ -130,6 +139,7 @@ namespace WebSocketSharp.Frame
} }
PayloadData = payloadData; PayloadData = payloadData;
if (Masked == Mask.MASK) if (Masked == Mask.MASK)
{ {
MaskingKey = new byte[4]; MaskingKey = new byte[4];
@ -264,7 +274,11 @@ namespace WebSocketSharp.Frame
} }
} }
// Payload Data // Payload Data
if (buffer3Len <= (ulong)_readBufferLen) if (buffer3Len == 0)
{
buffer3 = new byte[]{};
}
else if (buffer3Len <= (ulong)_readBufferLen)
{ {
buffer3 = new byte[buffer3Len]; buffer3 = new byte[buffer3Len];
readLen = stream.Read(buffer3, 0, (int)buffer3Len); readLen = stream.Read(buffer3, 0, (int)buffer3Len);

View File

@ -74,6 +74,7 @@ namespace WebSocketSharp
private string _protocol; private string _protocol;
private string _protocols; private string _protocols;
private volatile WsState _readyState; private volatile WsState _readyState;
private AutoResetEvent _receivedPong;
private SslStream _sslStream; private SslStream _sslStream;
private TcpClient _tcpClient; private TcpClient _tcpClient;
private Uri _uri; private Uri _uri;
@ -116,25 +117,8 @@ namespace WebSocketSharp
{ {
get get
{ {
if (_tcpClient == null) return false; if (_readyState != WsState.OPEN) return false;
return Ping();
var socket = _tcpClient.Client;
if (!socket.Connected) return false;
if (socket.Poll(0, SelectMode.SelectWrite) &&
!socket.Poll(0, SelectMode.SelectError))
{
// var buffer = new byte[1];
//
// if (socket.Receive(buffer, SocketFlags.Peek) != 0)
// {
// return true;
// }
return true;
}
return false;
} }
} }
@ -198,6 +182,7 @@ namespace WebSocketSharp
_fragmentLen = 1024; // Max value is int.MaxValue - 14. _fragmentLen = 1024; // Max value is int.MaxValue - 14.
_protocol = String.Empty; _protocol = String.Empty;
_readyState = WsState.CONNECTING; _readyState = WsState.CONNECTING;
_receivedPong = new AutoResetEvent(false);
_unTransmittedBuffer = new SynchronizedCollection<WsFrame>(); _unTransmittedBuffer = new SynchronizedCollection<WsFrame>();
} }
@ -894,6 +879,7 @@ namespace WebSocketSharp
} }
else if (frame.Opcode == Opcode.PONG) else if (frame.Opcode == Opcode.PONG)
{// FINAL & PONG {// FINAL & PONG
_receivedPong.Set();
OnMessage.Emit(this, new MessageEventArgs(frame.Opcode, frame.PayloadData)); OnMessage.Emit(this, new MessageEventArgs(frame.Opcode, frame.PayloadData));
} }
else else
@ -913,7 +899,9 @@ namespace WebSocketSharp
{ {
case Opcode.TEXT: case Opcode.TEXT:
case Opcode.BINARY: case Opcode.BINARY:
goto default;
case Opcode.PONG: case Opcode.PONG:
_receivedPong.Set();
goto default; goto default;
case Opcode.CLOSE: case Opcode.CLOSE:
#if DEBUG #if DEBUG
@ -1194,12 +1182,12 @@ namespace WebSocketSharp
Close(CloseStatusCode.AWAY); Close(CloseStatusCode.AWAY);
} }
public void Ping() public bool Ping()
{ {
Ping(String.Empty); return Ping(String.Empty);
} }
public void Ping(string data) public bool Ping(string data)
{ {
var payloadData = new PayloadData(data); var payloadData = new PayloadData(data);
@ -1207,11 +1195,13 @@ namespace WebSocketSharp
{ {
var msg = "Ping frame must have a payload length of 125 bytes or less."; var msg = "Ping frame must have a payload length of 125 bytes or less.";
error(msg); error(msg);
return; return false;
} }
var frame = createFrame(Fin.FINAL, Opcode.PING, payloadData); var frame = createFrame(Fin.FINAL, Opcode.PING, payloadData);
send(frame); if (!send(frame)) return false;
return _receivedPong.WaitOne(5 * 1000);
} }
public void Send(string data) public void Send(string data)

Binary file not shown.