diff --git a/Example/Example.pidb b/Example/Example.pidb index a19f520f..3077a371 100644 Binary files a/Example/Example.pidb and b/Example/Example.pidb differ diff --git a/Example/bin/Debug/example.exe b/Example/bin/Debug/example.exe index 460ba3a6..b45026f6 100755 Binary files a/Example/bin/Debug/example.exe and b/Example/bin/Debug/example.exe differ diff --git a/Example/bin/Debug/example.exe.mdb b/Example/bin/Debug/example.exe.mdb index 7ebcc73d..de59009b 100644 Binary files a/Example/bin/Debug/example.exe.mdb and b/Example/bin/Debug/example.exe.mdb differ diff --git a/Example/bin/Debug/websocket-sharp.dll b/Example/bin/Debug/websocket-sharp.dll index 4b6e1a4a..ab0165ce 100755 Binary files a/Example/bin/Debug/websocket-sharp.dll and b/Example/bin/Debug/websocket-sharp.dll differ diff --git a/Example/bin/Debug/websocket-sharp.dll.mdb b/Example/bin/Debug/websocket-sharp.dll.mdb index d1c92fae..b1200b4e 100644 Binary files a/Example/bin/Debug/websocket-sharp.dll.mdb and b/Example/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example/bin/Debug_Ubuntu/example.exe b/Example/bin/Debug_Ubuntu/example.exe index 9b057aae..3ecd8285 100755 Binary files a/Example/bin/Debug_Ubuntu/example.exe and b/Example/bin/Debug_Ubuntu/example.exe differ diff --git a/Example/bin/Debug_Ubuntu/example.exe.mdb b/Example/bin/Debug_Ubuntu/example.exe.mdb index 6a670133..b3debf12 100644 Binary files a/Example/bin/Debug_Ubuntu/example.exe.mdb and b/Example/bin/Debug_Ubuntu/example.exe.mdb differ diff --git a/Example/bin/Debug_Ubuntu/websocket-sharp.dll b/Example/bin/Debug_Ubuntu/websocket-sharp.dll index 03bffdf9..99281697 100755 Binary files a/Example/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index 413f6352..9788d09c 100644 Binary files a/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example/bin/Release/example.exe b/Example/bin/Release/example.exe index 78d906e7..2a19e18d 100755 Binary files a/Example/bin/Release/example.exe and b/Example/bin/Release/example.exe differ diff --git a/Example/bin/Release/websocket-sharp.dll b/Example/bin/Release/websocket-sharp.dll index 09cef2ee..1bc42dcb 100755 Binary files a/Example/bin/Release/websocket-sharp.dll and b/Example/bin/Release/websocket-sharp.dll differ diff --git a/Example/bin/Release_Ubuntu/example.exe b/Example/bin/Release_Ubuntu/example.exe index dee7da90..a640a243 100755 Binary files a/Example/bin/Release_Ubuntu/example.exe and b/Example/bin/Release_Ubuntu/example.exe differ diff --git a/Example/bin/Release_Ubuntu/websocket-sharp.dll b/Example/bin/Release_Ubuntu/websocket-sharp.dll index 0e7c945d..cd94097c 100755 Binary files a/Example/bin/Release_Ubuntu/websocket-sharp.dll and b/Example/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example1/bin/Debug/example1.exe b/Example1/bin/Debug/example1.exe index 37fc0ae0..a9a89d8e 100755 Binary files a/Example1/bin/Debug/example1.exe and b/Example1/bin/Debug/example1.exe differ diff --git a/Example1/bin/Debug/example1.exe.mdb b/Example1/bin/Debug/example1.exe.mdb index 9e28ca8b..a2f284af 100644 Binary files a/Example1/bin/Debug/example1.exe.mdb and b/Example1/bin/Debug/example1.exe.mdb differ diff --git a/Example1/bin/Debug/websocket-sharp.dll b/Example1/bin/Debug/websocket-sharp.dll index 4b6e1a4a..ab0165ce 100755 Binary files a/Example1/bin/Debug/websocket-sharp.dll and b/Example1/bin/Debug/websocket-sharp.dll differ diff --git a/Example1/bin/Debug/websocket-sharp.dll.mdb b/Example1/bin/Debug/websocket-sharp.dll.mdb index d1c92fae..b1200b4e 100644 Binary files a/Example1/bin/Debug/websocket-sharp.dll.mdb and b/Example1/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example1/bin/Debug_Ubuntu/example1.exe b/Example1/bin/Debug_Ubuntu/example1.exe index 6a24073f..ba8a0250 100755 Binary files a/Example1/bin/Debug_Ubuntu/example1.exe and b/Example1/bin/Debug_Ubuntu/example1.exe differ diff --git a/Example1/bin/Debug_Ubuntu/example1.exe.mdb b/Example1/bin/Debug_Ubuntu/example1.exe.mdb index a60ddc54..be7f7672 100644 Binary files a/Example1/bin/Debug_Ubuntu/example1.exe.mdb and b/Example1/bin/Debug_Ubuntu/example1.exe.mdb differ diff --git a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll index 03bffdf9..99281697 100755 Binary files a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index 413f6352..9788d09c 100644 Binary files a/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example1/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example1/bin/Release/example1.exe b/Example1/bin/Release/example1.exe index 633c9e08..dd225143 100755 Binary files a/Example1/bin/Release/example1.exe and b/Example1/bin/Release/example1.exe differ diff --git a/Example1/bin/Release/websocket-sharp.dll b/Example1/bin/Release/websocket-sharp.dll index 09cef2ee..1bc42dcb 100755 Binary files a/Example1/bin/Release/websocket-sharp.dll and b/Example1/bin/Release/websocket-sharp.dll differ diff --git a/Example1/bin/Release_Ubuntu/example1.exe b/Example1/bin/Release_Ubuntu/example1.exe index 578b8092..f226f691 100755 Binary files a/Example1/bin/Release_Ubuntu/example1.exe and b/Example1/bin/Release_Ubuntu/example1.exe differ diff --git a/Example1/bin/Release_Ubuntu/websocket-sharp.dll b/Example1/bin/Release_Ubuntu/websocket-sharp.dll index 0e7c945d..cd94097c 100755 Binary files a/Example1/bin/Release_Ubuntu/websocket-sharp.dll and b/Example1/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example2/bin/Debug/example2.exe b/Example2/bin/Debug/example2.exe index c8bf7f10..825fbc3b 100755 Binary files a/Example2/bin/Debug/example2.exe and b/Example2/bin/Debug/example2.exe differ diff --git a/Example2/bin/Debug/example2.exe.mdb b/Example2/bin/Debug/example2.exe.mdb index f27ea0ec..dfa654e2 100644 Binary files a/Example2/bin/Debug/example2.exe.mdb and b/Example2/bin/Debug/example2.exe.mdb differ diff --git a/Example2/bin/Debug/websocket-sharp.dll b/Example2/bin/Debug/websocket-sharp.dll index 4b6e1a4a..ab0165ce 100755 Binary files a/Example2/bin/Debug/websocket-sharp.dll and b/Example2/bin/Debug/websocket-sharp.dll differ diff --git a/Example2/bin/Debug/websocket-sharp.dll.mdb b/Example2/bin/Debug/websocket-sharp.dll.mdb index d1c92fae..b1200b4e 100644 Binary files a/Example2/bin/Debug/websocket-sharp.dll.mdb and b/Example2/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example2/bin/Debug_Ubuntu/example2.exe b/Example2/bin/Debug_Ubuntu/example2.exe index ea846c4b..08833647 100755 Binary files a/Example2/bin/Debug_Ubuntu/example2.exe and b/Example2/bin/Debug_Ubuntu/example2.exe differ diff --git a/Example2/bin/Debug_Ubuntu/example2.exe.mdb b/Example2/bin/Debug_Ubuntu/example2.exe.mdb index 0183c0f9..279b7260 100644 Binary files a/Example2/bin/Debug_Ubuntu/example2.exe.mdb and b/Example2/bin/Debug_Ubuntu/example2.exe.mdb differ diff --git a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll index 03bffdf9..99281697 100755 Binary files a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index 413f6352..9788d09c 100644 Binary files a/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example2/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example2/bin/Release/example2.exe b/Example2/bin/Release/example2.exe index 3a3de3a0..bd5f6ed6 100755 Binary files a/Example2/bin/Release/example2.exe and b/Example2/bin/Release/example2.exe differ diff --git a/Example2/bin/Release/websocket-sharp.dll b/Example2/bin/Release/websocket-sharp.dll index 09cef2ee..1bc42dcb 100755 Binary files a/Example2/bin/Release/websocket-sharp.dll and b/Example2/bin/Release/websocket-sharp.dll differ diff --git a/Example2/bin/Release_Ubuntu/example2.exe b/Example2/bin/Release_Ubuntu/example2.exe index c52d6272..7e73f85a 100755 Binary files a/Example2/bin/Release_Ubuntu/example2.exe and b/Example2/bin/Release_Ubuntu/example2.exe differ diff --git a/Example2/bin/Release_Ubuntu/websocket-sharp.dll b/Example2/bin/Release_Ubuntu/websocket-sharp.dll index 0e7c945d..cd94097c 100755 Binary files a/Example2/bin/Release_Ubuntu/websocket-sharp.dll and b/Example2/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/Example3/bin/Debug/Example3.exe b/Example3/bin/Debug/Example3.exe index f2c476bc..6dd355a0 100755 Binary files a/Example3/bin/Debug/Example3.exe and b/Example3/bin/Debug/Example3.exe differ diff --git a/Example3/bin/Debug/Example3.exe.mdb b/Example3/bin/Debug/Example3.exe.mdb index a3bf5d64..686d663d 100644 Binary files a/Example3/bin/Debug/Example3.exe.mdb and b/Example3/bin/Debug/Example3.exe.mdb differ diff --git a/Example3/bin/Debug/websocket-sharp.dll b/Example3/bin/Debug/websocket-sharp.dll index 4b6e1a4a..ab0165ce 100755 Binary files a/Example3/bin/Debug/websocket-sharp.dll and b/Example3/bin/Debug/websocket-sharp.dll differ diff --git a/Example3/bin/Debug/websocket-sharp.dll.mdb b/Example3/bin/Debug/websocket-sharp.dll.mdb index d1c92fae..b1200b4e 100644 Binary files a/Example3/bin/Debug/websocket-sharp.dll.mdb and b/Example3/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/Example3/bin/Debug_Ubuntu/Example3.exe b/Example3/bin/Debug_Ubuntu/Example3.exe index a0639f02..05f6f7a6 100755 Binary files a/Example3/bin/Debug_Ubuntu/Example3.exe and b/Example3/bin/Debug_Ubuntu/Example3.exe differ diff --git a/Example3/bin/Debug_Ubuntu/Example3.exe.mdb b/Example3/bin/Debug_Ubuntu/Example3.exe.mdb index 39599b76..86e73bb3 100644 Binary files a/Example3/bin/Debug_Ubuntu/Example3.exe.mdb and b/Example3/bin/Debug_Ubuntu/Example3.exe.mdb differ diff --git a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll index 03bffdf9..99281697 100755 Binary files a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll and b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index 413f6352..9788d09c 100644 Binary files a/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/Example3/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/Example3/bin/Release/Example3.exe b/Example3/bin/Release/Example3.exe index 9a3c4472..f5100eff 100755 Binary files a/Example3/bin/Release/Example3.exe and b/Example3/bin/Release/Example3.exe differ diff --git a/Example3/bin/Release/websocket-sharp.dll b/Example3/bin/Release/websocket-sharp.dll index 09cef2ee..1bc42dcb 100755 Binary files a/Example3/bin/Release/websocket-sharp.dll and b/Example3/bin/Release/websocket-sharp.dll differ diff --git a/Example3/bin/Release_Ubuntu/Example3.exe b/Example3/bin/Release_Ubuntu/Example3.exe index b786037d..6aaf59c8 100755 Binary files a/Example3/bin/Release_Ubuntu/Example3.exe and b/Example3/bin/Release_Ubuntu/Example3.exe differ diff --git a/Example3/bin/Release_Ubuntu/websocket-sharp.dll b/Example3/bin/Release_Ubuntu/websocket-sharp.dll index 0e7c945d..cd94097c 100755 Binary files a/Example3/bin/Release_Ubuntu/websocket-sharp.dll and b/Example3/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp/IWsStream.cs b/websocket-sharp/IWsStream.cs deleted file mode 100644 index a4dc3ee8..00000000 --- a/websocket-sharp/IWsStream.cs +++ /dev/null @@ -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); - } -} diff --git a/websocket-sharp/Net/HttpListenerWebSocketContext.cs b/websocket-sharp/Net/HttpListenerWebSocketContext.cs index 2de6a5b6..dafd9b7a 100644 --- a/websocket-sharp/Net/HttpListenerWebSocketContext.cs +++ b/websocket-sharp/Net/HttpListenerWebSocketContext.cs @@ -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; } } diff --git a/websocket-sharp/Net/Sockets/TcpListenerWebSocketContext.cs b/websocket-sharp/Net/Sockets/TcpListenerWebSocketContext.cs index 0d88ab58..8f60fb38 100644 --- a/websocket-sharp/Net/Sockets/TcpListenerWebSocketContext.cs +++ b/websocket-sharp/Net/Sockets/TcpListenerWebSocketContext.cs @@ -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); } } } diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 27f3827d..bc2920a3 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -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 _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); - - return; - } - - _wsStream = new WsStream(_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); - } - - return new WsStream(netStream); - } - - internal static IWsStream 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 - #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); } diff --git a/websocket-sharp/WsStream.cs b/websocket-sharp/WsStream.cs index 026da8fa..edba2e14 100644 --- a/websocket-sharp/WsStream.cs +++ b/websocket-sharp/WsStream.cs @@ -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 : 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; diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll b/websocket-sharp/bin/Debug/websocket-sharp.dll index 4b6e1a4a..ab0165ce 100755 Binary files a/websocket-sharp/bin/Debug/websocket-sharp.dll and b/websocket-sharp/bin/Debug/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb b/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb index d1c92fae..b1200b4e 100644 Binary files a/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb and b/websocket-sharp/bin/Debug/websocket-sharp.dll.mdb differ diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll index 03bffdf9..99281697 100755 Binary files a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll and b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb index 413f6352..9788d09c 100644 Binary files a/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb and b/websocket-sharp/bin/Debug_Ubuntu/websocket-sharp.dll.mdb differ diff --git a/websocket-sharp/bin/Release/websocket-sharp.dll b/websocket-sharp/bin/Release/websocket-sharp.dll index 09cef2ee..1bc42dcb 100755 Binary files a/websocket-sharp/bin/Release/websocket-sharp.dll and b/websocket-sharp/bin/Release/websocket-sharp.dll differ diff --git a/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll index 0e7c945d..cd94097c 100755 Binary files a/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll and b/websocket-sharp/bin/Release_Ubuntu/websocket-sharp.dll differ diff --git a/websocket-sharp/websocket-sharp.csproj b/websocket-sharp/websocket-sharp.csproj index a1e0d30c..24f92169 100644 --- a/websocket-sharp/websocket-sharp.csproj +++ b/websocket-sharp/websocket-sharp.csproj @@ -74,7 +74,6 @@ - diff --git a/websocket-sharp/websocket-sharp.pidb b/websocket-sharp/websocket-sharp.pidb index 242450b5..9e9313ae 100644 Binary files a/websocket-sharp/websocket-sharp.pidb and b/websocket-sharp/websocket-sharp.pidb differ