Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.net core support #299

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 69 additions & 38 deletions websocket-sharp/Ext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static class Ext

private static byte[] compress (this byte[] data)
{
if (data.LongLength == 0)
if (data.Length == 0)
//return new byte[] { 0x00, 0x00, 0x00, 0xff, 0xff };
return data;

Expand All @@ -93,7 +93,7 @@ private static MemoryStream compress (this Stream stream)
stream.Position = 0;
using (var ds = new DeflateStream (output, CompressionMode.Compress, true)) {
stream.CopyTo (ds, 1024);
ds.Close (); // BFINAL set to 1.
ds.Dispose (); // BFINAL set to 1.
output.Write (_last, 0, 1);
output.Position = 0;

Expand All @@ -104,14 +104,14 @@ private static MemoryStream compress (this Stream stream)
private static byte[] compressToArray (this Stream stream)
{
using (var output = stream.compress ()) {
output.Close ();
output.Dispose ();
return output.ToArray ();
}
}

private static byte[] decompress (this byte[] data)
{
if (data.LongLength == 0)
if (data.Length == 0)
return data;

using (var input = new MemoryStream (data))
Expand All @@ -136,7 +136,7 @@ private static MemoryStream decompress (this Stream stream)
private static byte[] decompressToArray (this Stream stream)
{
using (var output = stream.decompress ()) {
output.Close ();
output.Dispose ();
return output.ToArray ();
}
}
Expand Down Expand Up @@ -230,7 +230,7 @@ internal static bool CheckWaitTime (this TimeSpan time, out string message)
internal static void Close (this HttpListenerResponse response, HttpStatusCode code)
{
response.StatusCode = (int) code;
response.OutputStream.Close ();
response.OutputStream.Dispose ();
}

internal static void CloseWithAuthChallenge (
Expand Down Expand Up @@ -290,7 +290,7 @@ internal static bool ContainsTwice (this string[] values)
return contains (0);
}

internal static T[] Copy<T> (this T[] source, long length)
internal static T[] Copy<T> (this T[] source, int length)
{
var dest = new T[length];
Array.Copy (source, 0, dest, 0, length);
Expand Down Expand Up @@ -318,7 +318,7 @@ internal static void CopyToAsync (
AsyncCallback callback = null;
callback = ar => {
try {
var nread = source.EndRead (ar);
var nread = TaskToApm.End<int> (ar);
if (nread <= 0) {
if (completed != null)
completed ();
Expand Down Expand Up @@ -644,11 +644,31 @@ internal static byte[] ReadBytes (this Stream stream, long length, int bufferLen
catch {
}

dest.Close ();
dest.Dispose ();
return dest.ToArray ();
}
}

internal static int EndWrite(this Stream stream, IAsyncResult ar)
{
return TaskToApm.End<int>(ar);
}

internal static int EndRead(this Stream stream, IAsyncResult ar)
{
return TaskToApm.End<int>(ar);
}

internal static IAsyncResult BeginWrite(this Stream stream, byte[] buffer, int offset, int length, AsyncCallback callback, object state)
{
return TaskToApm.Begin(stream.WriteAsync(buffer, offset, length), callback, state);
}

internal static IAsyncResult BeginRead(this Stream stream, byte[] buffer, int offset, int length, AsyncCallback callback, object state)
{
return TaskToApm.Begin(stream.ReadAsync(buffer, offset, length), callback, state);
}

internal static void ReadBytesAsync (
this Stream stream, int length, Action<byte[]> completed, Action<Exception> error
)
Expand Down Expand Up @@ -735,7 +755,7 @@ Action<Exception> error

if (nread == 0 || nread == len) {
if (completed != null) {
dest.Close ();
dest.Dispose ();
completed (dest.ToArray ());
}

Expand Down Expand Up @@ -836,7 +856,7 @@ internal static byte[] ToByteArray (this Stream stream)
using (var output = new MemoryStream ()) {
stream.Position = 0;
stream.CopyTo (output, 1024);
output.Close ();
output.Dispose ();

return output.ToArray ();
}
Expand Down Expand Up @@ -871,7 +891,7 @@ internal static System.Net.IPAddress ToIPAddress (this string hostnameOrAddress)
return addr;

try {
return System.Net.Dns.GetHostAddresses (hostnameOrAddress)[0];
return System.Net.Dns.GetHostAddressesAsync (hostnameOrAddress).Result[0];
}
catch {
return null;
Expand Down Expand Up @@ -1344,7 +1364,7 @@ public static bool IsLocal (this System.Net.IPAddress address)
}

var host = System.Net.Dns.GetHostName ();
var addrs = System.Net.Dns.GetHostAddresses (host);
var addrs = System.Net.Dns.GetHostAddressesAsync (host).Result;
foreach (var addr in addrs) {
if (address.Equals (addr))
return true;
Expand Down Expand Up @@ -1433,13 +1453,13 @@ public static bool IsPredefinedScheme (this string value)
public static bool IsUpgradeTo (this HttpListenerRequest request, string protocol)
{
if (request == null)
throw new ArgumentNullException ("request");
throw new ArgumentNullException (nameof(request));

if (protocol == null)
throw new ArgumentNullException ("protocol");
throw new ArgumentNullException (nameof(protocol));

if (protocol.Length == 0)
throw new ArgumentException ("An empty string.", "protocol");
throw new ArgumentException ("An empty string.", nameof(protocol));

return request.Headers.Contains ("Upgrade", protocol) &&
request.Headers.Contains ("Connection", "Upgrade");
Expand Down Expand Up @@ -1490,24 +1510,35 @@ public static bool MaybeUri (this string value)
/// <typeparam name="T">
/// The type of elements in <paramref name="array"/>.
/// </typeparam>
public static T[] SubArray<T> (this T[] array, int startIndex, int length)
{
int len;
if (array == null || (len = array.Length) == 0)
return new T[0];
// public static T[] SubArray<T> (this T[] array, int startIndex, int length)
// {
// int len;
// if (array == null || (len = array.Length) == 0)
// return new T[0];

if (startIndex < 0 || length <= 0 || startIndex + length > len)
return new T[0];
// if (startIndex < 0 || length <= 0 || startIndex + length > len)
// return new T[0];

if (startIndex == 0 && length == len)
return array;
// if (startIndex == 0 && length == len)
// return array;

var subArray = new T[length];
Array.Copy (array, startIndex, subArray, 0, length);
// var subArray = new T[length];
// Array.Copy (array, startIndex, subArray, 0, length);

return subArray;
// return subArray;
// }

public static byte[] GetBuffer(this MemoryStream stream)
{
ArraySegment<byte> buffer;
if(stream.TryGetBuffer(out buffer))
{
return buffer.Array;
}
throw new Exception("Could not get buffer");
}


/// <summary>
/// Retrieves a sub-array from the specified <paramref name="array"/>. A sub-array starts at
/// the specified element position in <paramref name="array"/>.
Expand All @@ -1529,10 +1560,10 @@ public static T[] SubArray<T> (this T[] array, int startIndex, int length)
/// <typeparam name="T">
/// The type of elements in <paramref name="array"/>.
/// </typeparam>
public static T[] SubArray<T> (this T[] array, long startIndex, long length)
public static T[] SubArray<T> (this T[] array, int startIndex, int length)
{
long len;
if (array == null || (len = array.LongLength) == 0)
int len;
if (array == null || (len = array.Length) == 0)
return new T[0];

if (startIndex < 0 || length <= 0 || startIndex + length > len)
Expand Down Expand Up @@ -1706,7 +1737,7 @@ public static T To<T> (this byte[] source, ByteOrder sourceOrder)
where T : struct
{
if (source == null)
throw new ArgumentNullException ("source");
throw new ArgumentNullException (nameof(source));

if (source.Length == 0)
return default (T);
Expand Down Expand Up @@ -1805,7 +1836,7 @@ public static byte[] ToByteArray<T> (this T value, ByteOrder order)
public static byte[] ToHostOrder (this byte[] source, ByteOrder sourceOrder)
{
if (source == null)
throw new ArgumentNullException ("source");
throw new ArgumentNullException (nameof(source));

return source.Length > 1 && !sourceOrder.IsHostOrder () ? source.Reverse () : source;
}
Expand Down Expand Up @@ -1834,7 +1865,7 @@ public static byte[] ToHostOrder (this byte[] source, ByteOrder sourceOrder)
public static string ToString<T> (this T[] array, string separator)
{
if (array == null)
throw new ArgumentNullException ("array");
throw new ArgumentNullException (nameof(array));

var len = array.Length;
if (len == 0)
Expand Down Expand Up @@ -1924,12 +1955,12 @@ public static string UrlEncode (this string value)
public static void WriteContent (this HttpListenerResponse response, byte[] content)
{
if (response == null)
throw new ArgumentNullException ("response");
throw new ArgumentNullException (nameof(response));

if (content == null)
throw new ArgumentNullException ("content");
throw new ArgumentNullException (nameof(content));

var len = content.LongLength;
var len = content.Length;
if (len == 0) {
response.Close ();
return;
Expand All @@ -1942,7 +1973,7 @@ public static void WriteContent (this HttpListenerResponse response, byte[] cont
else
output.WriteBytes (content, 1024);

output.Close ();
output.Dispose();
}

#endregion
Expand Down
Loading