Fix due to the modified WsStream.cs

This commit is contained in:
sta
2012-09-24 19:52:31 +09:00
parent b9859e08c0
commit 14f16577be
62 changed files with 106 additions and 150 deletions

View File

@@ -1,46 +0,0 @@
#region MIT License
/**
* IWsStream.cs
*
* The MIT License
*
* Copyright (c) 2010-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 WebSocketSharp.Frame;
namespace WebSocketSharp
{
public interface IWsStream : IDisposable
{
void Close();
int Read(byte[] buffer, int offset, int size);
int ReadByte();
WsFrame ReadFrame();
string[] ReadHandshake();
void Write(byte[] buffer, int offset, int count);
void WriteByte(byte value);
void WriteFrame(WsFrame frame);
void WriteHandshake(Handshake handshake);
}
}

View File

@@ -29,9 +29,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Principal;
namespace WebSocketSharp.Net {
@@ -40,12 +37,12 @@ namespace WebSocketSharp.Net {
{
private HttpListenerContext _context;
private WebSocket _socket;
private IWsStream _stream;
private WsStream _stream;
internal HttpListenerWebSocketContext(string path, HttpListenerContext context)
{
_context = context;
_stream = WebSocket.CreateServerStream(context);
_stream = WsStream.CreateServerStream(context);
_socket = new WebSocket(path.ToUri(), this);
}
@@ -53,7 +50,7 @@ namespace WebSocketSharp.Net {
get { return _context; }
}
internal IWsStream Stream {
internal WsStream Stream {
get { return _stream; }
}

View File

@@ -29,8 +29,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Principal;
@@ -42,7 +40,7 @@ namespace WebSocketSharp.Net.Sockets {
private bool _isSecure;
private RequestHandshake _request;
private WebSocket _socket;
private IWsStream _stream;
private WsStream _stream;
internal TcpListenerWebSocketContext(TcpClient client)
{
@@ -54,7 +52,7 @@ namespace WebSocketSharp.Net.Sockets {
get { return _client; }
}
internal IWsStream Stream {
internal WsStream Stream {
get { return _stream; }
}
@@ -108,13 +106,10 @@ namespace WebSocketSharp.Net.Sockets {
private void init()
{
_stream = WebSocket.CreateServerStream(_client);
_request = RequestHandshake.Parse(_stream.ReadHandshake());
var port = ((IPEndPoint)_client.Client.LocalEndPoint).Port;
_isSecure = port == 443 ? true : false;
_socket = new WebSocket(this);
_stream = WsStream.CreateServerStream(_client);
_isSecure = _stream.IsSecure;
_request = RequestHandshake.Parse(_stream.ReadHandshake());
_socket = new WebSocket(this);
}
}
}

View File

@@ -33,19 +33,15 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Security.Authentication;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using WebSocketSharp.Frame;
using WebSocketSharp.Net;
using WebSocketSharp.Net.Sockets;
@@ -75,16 +71,14 @@ namespace WebSocketSharp
private int _fragmentLen;
private bool _isClient;
private bool _isSecure;
private NetworkStream _netStream;
private string _protocol;
private string _protocols;
private volatile WsState _readyState;
private AutoResetEvent _receivedPong;
private SslStream _sslStream;
private TcpClient _tcpClient;
private Uri _uri;
private SynchronizedCollection<WsFrame> _unTransmittedBuffer;
private IWsStream _wsStream;
private WsStream _wsStream;
#endregion
@@ -355,12 +349,6 @@ namespace WebSocketSharp
_wsStream = null;
}
if (_netStream != null)
{
_netStream.Dispose();
_netStream = null;
}
if (_tcpClient != null)
{
_tcpClient.Close();
@@ -385,25 +373,7 @@ namespace WebSocketSharp
if (port <= 0)
port = IsSecure ? 443 : 80;
_tcpClient = new TcpClient(host, port);
_netStream = _tcpClient.GetStream();
if (IsSecure)
{
RemoteCertificateValidationCallback validation = (sender, certificate, chain, sslPolicyErrors) =>
{
// FIXME: Always returns true
return true;
};
_sslStream = new SslStream(_netStream, false, validation);
_sslStream.AuthenticateAsClient(host);
_wsStream = new WsStream<SslStream>(_sslStream);
return;
}
_wsStream = new WsStream<NetworkStream>(_netStream);
_wsStream = WsStream.CreateClientStream(host, port, out _tcpClient);
}
private string createExpectedKey()
@@ -468,13 +438,13 @@ namespace WebSocketSharp
if (_tcpClient != null)
{
_wsStream = CreateServerStream(_tcpClient);
_wsStream = WsStream.CreateServerStream(_tcpClient);
return;
}
if (_baseContext != null)
{
_wsStream = CreateServerStream(_baseContext);
_wsStream = WsStream.CreateServerStream(_baseContext);
return;
}
}
@@ -1011,39 +981,6 @@ namespace WebSocketSharp
#endregion
#region Internal Static Methods
internal static IWsStream CreateServerStream(TcpClient client)
{
var netStream = client.GetStream();
var port = ((IPEndPoint)client.Client.LocalEndPoint).Port;
if (port == 443)
{
var sslStream = new SslStream(netStream);
var certPath = ConfigurationManager.AppSettings["ServerCertPath"];
sslStream.AuthenticateAsServer(new X509Certificate2(certPath));
return new WsStream<SslStream>(sslStream);
}
return new WsStream<NetworkStream>(netStream);
}
internal static IWsStream CreateServerStream(WebSocketSharp.Net.HttpListenerContext context)
{
var conn = context.Connection;
var stream = conn.Stream;
if (conn.IsSecure)
return new WsStream<SslStream>((SslStream)stream);
return new WsStream<NetworkStream>((NetworkStream)stream);
}
#endregion
#region Public Methods
public void Close()
@@ -1111,7 +1048,8 @@ namespace WebSocketSharp
}
var frame = createFrame(Fin.FINAL, Opcode.PING, payloadData);
if (!send(frame)) return false;
if (!send(frame))
return false;
return _receivedPong.WaitOne(5 * 1000);
}

View File

@@ -28,45 +28,118 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using WebSocketSharp.Frame;
namespace WebSocketSharp
{
public class WsStream<TStream> : IWsStream
where TStream : Stream
public class WsStream : IDisposable
{
#region Fields
private TStream _innerStream;
private Object _forRead;
private Object _forWrite;
private Stream _innerStream;
private Type _innerStreamType;
private bool _isSecure;
private Object _forRead;
private Object _forWrite;
#endregion
#region Constructor
public WsStream(TStream innerStream)
public WsStream(NetworkStream innerStream)
{
Type streamType = typeof(TStream);
if (streamType != typeof(NetworkStream) &&
streamType != typeof(SslStream))
{
throw new NotSupportedException("Not supported Stream type: " + streamType.ToString());
}
init(innerStream);
}
public WsStream(SslStream innerStream)
{
init(innerStream);
}
#endregion
#region Public Property
public bool IsSecure {
get { return _isSecure; }
}
#endregion
#region Private Methods
private void init(Stream innerStream)
{
if (innerStream == null)
{
throw new ArgumentNullException("innerStream");
_innerStream = innerStream;
_innerStreamType = innerStream.GetType();
_isSecure = _innerStreamType == typeof(SslStream) ? true : false;
_forRead = new object();
_forWrite = new object();
}
#endregion
#region Internal Methods
internal static WsStream CreateClientStream(string hostname, int port, out TcpClient client)
{
client = new TcpClient(hostname, port);
var netStream = client.GetStream();
if (port == 443)
{
RemoteCertificateValidationCallback validationCb = (sender, certificate, chain, sslPolicyErrors) =>
{
// FIXME: Always returns true
return true;
};
var sslStream = new SslStream(netStream, false, validationCb);
sslStream.AuthenticateAsClient(hostname);
return new WsStream(sslStream);
}
_innerStream = innerStream;
_forRead = new object();
_forWrite = new object();
return new WsStream(netStream);
}
internal static WsStream CreateServerStream(TcpClient client)
{
var netStream = client.GetStream();
var port = ((IPEndPoint)client.Client.LocalEndPoint).Port;
if (port == 443)
{
var sslStream = new SslStream(netStream);
var certPath = ConfigurationManager.AppSettings["ServerCertPath"];
sslStream.AuthenticateAsServer(new X509Certificate2(certPath));
return new WsStream(sslStream);
}
return new WsStream(netStream);
}
internal static WsStream CreateServerStream(WebSocketSharp.Net.HttpListenerContext context)
{
var conn = context.Connection;
var stream = conn.Stream;
if (conn.IsSecure)
return new WsStream((SslStream)stream);
return new WsStream((NetworkStream)stream);
}
#endregion
@@ -90,7 +163,7 @@ namespace WebSocketSharp
var readLen = _innerStream.Read(buffer, offset, size);
if (readLen < size)
{
var msg = String.Format("Data can not be read from {0}.", typeof(TStream).Name);
var msg = String.Format("Data can not be read from {0}.", _innerStreamType);
throw new IOException(msg);
}
return readLen;

View File

@@ -74,7 +74,6 @@
<Compile Include="WebSocket.cs" />
<Compile Include="Server\WebSocketServer.cs" />
<Compile Include="Server\WebSocketService.cs" />
<Compile Include="IWsStream.cs" />
<Compile Include="WsStream.cs" />
<Compile Include="RequestHandshake.cs" />
<Compile Include="ResponseHandshake.cs" />

Binary file not shown.