Skip to content
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
6 changes: 3 additions & 3 deletions src/Fleck.Tests/SocketWrapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void Setup()
[Test]
public void ShouldCompleteAcceptTaskOnDispose()
{
Task task = _wrapper.Accept(socket => { }, exception => { });
Task task = _wrapper.AcceptAsync(socket => { }, exception => { });
_wrapper.Dispose();

Assert.DoesNotThrow(task.Wait);
Expand Down Expand Up @@ -79,7 +79,7 @@ public void ShouldNotWriteToClosedSocketIfCancelled()
Exception ex = null;
_wrapper.Dispose();

var task = _wrapper.Send(new byte[1], () => {}, e => {ex = e;});
var task = _wrapper.SendAsync(new byte[1], () => {}, e => {ex = e;});

Assert.IsNull(task);
Assert.IsNull(ex);
Expand All @@ -89,7 +89,7 @@ public void ShouldHandleObjectDisposedOnReceive()
{
Exception ex = null;
_wrapper.Dispose();
_wrapper.Receive(new byte[1], i => {}, e => {ex = e;}, 0);
_wrapper.ReceiveAsync(new byte[1], i => {}, e => {ex = e;}, 0);
Assert.IsInstanceOf<ObjectDisposedException>(ex);
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/Fleck.Tests/WebSocketConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,25 @@ public void ShouldNotSendOnClosed()
_connection.Handler = _handlerMock.Object;
SetupReadLengths(0);
_connection.StartReceiving();
_connection.Send("Zing");
_socketMock.Verify(x => x.Send(It.IsAny<byte[]>(), It.IsAny<Action>(), It.IsAny<Action<Exception>>()), Times.Never());
_connection.SendAsync("Zing");
_socketMock.Verify(x => x.SendAsync(It.IsAny<byte[]>(), It.IsAny<Action>(), It.IsAny<Action<Exception>>()), Times.Never());
}

[Test]
public void ShouldNotSendWhenSocketDisconnected()
{
_connection.Handler = _handlerMock.Object;
_socketMock.SetupGet(x => x.Connected).Returns(false);
_connection.Send("Zing");
_socketMock.Verify(x => x.Send(It.IsAny<byte[]>(), It.IsAny<Action>(), It.IsAny<Action<Exception>>()), Times.Never());
_connection.SendAsync("Zing");
_socketMock.Verify(x => x.SendAsync(It.IsAny<byte[]>(), It.IsAny<Action>(), It.IsAny<Action<Exception>>()), Times.Never());
}

[Test]
public void ShouldNotReadWhenSocketClosed()
{
_socketMock.SetupGet(x => x.Connected).Returns(false);
_connection.StartReceiving();
_socketMock.Verify(x => x.Receive(It.IsAny<byte[]>(), It.IsAny<Action<int>>(), It.IsAny<Action<Exception>>(), 0), Times.Never());
_socketMock.Verify(x => x.ReceiveAsync(It.IsAny<byte[]>(), It.IsAny<Action<int>>(), It.IsAny<Action<Exception>>(), 0), Times.Never());
}

[Test]
Expand Down Expand Up @@ -117,7 +117,7 @@ public void ShouldCallOnErrorWhenError()
{
_socketMock.Setup(
x =>
x.Receive(It.IsAny<byte[]>(), It.IsAny<Action<int>>(), It.IsAny<Action<Exception>>(), It.IsAny<int>()))
x.ReceiveAsync(It.IsAny<byte[]>(), It.IsAny<Action<int>>(), It.IsAny<Action<Exception>>(), It.IsAny<int>()))
.Callback<byte[], Action<int>, Action<Exception>, int>((buffer, success, error, offset) =>
{
error(new Exception());
Expand All @@ -137,7 +137,7 @@ public void ShouldSwallowObjectDisposedExceptionOnRead()
{
_socketMock.Setup(
x =>
x.Receive(It.IsAny<byte[]>(), It.IsAny<Action<int>>(), It.IsAny<Action<Exception>>(), It.IsAny<int>()))
x.ReceiveAsync(It.IsAny<byte[]>(), It.IsAny<Action<int>>(), It.IsAny<Action<Exception>>(), It.IsAny<int>()))
.Callback<byte[], Action<int>, Action<Exception>, int>((buffer, success, error, offset) =>
{
error(new ObjectDisposedException("socket"));
Expand All @@ -157,7 +157,7 @@ private void SetupReadLengths(params int[] args)
var index = 0;
_socketMock.Setup(
x =>
x.Receive(It.IsAny<byte[]>(), It.IsAny<Action<int>>(), It.IsAny<Action<Exception>>(), It.IsAny<int>()))
x.ReceiveAsync(It.IsAny<byte[]>(), It.IsAny<Action<int>>(), It.IsAny<Action<Exception>>(), It.IsAny<int>()))
.Callback<byte[], Action<int>, Action<Exception>, int>((buffer, success, error, offset) =>
{
if (args.Length > index)
Expand Down
2 changes: 1 addition & 1 deletion src/Fleck.Tests/WebSocketServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void ShouldStart()
_server.Start(connection => { });

socketMock.Verify(s => s.Bind(It.Is<IPEndPoint>(i => i.Port == 8000)));
socketMock.Verify(s => s.Accept(It.IsAny<Action<ISocket>>(), It.IsAny<Action<Exception>>()));
socketMock.Verify(s => s.AcceptAsync(It.IsAny<Action<ISocket>>(), It.IsAny<Action<Exception>>()));
}

[Test]
Expand Down
17 changes: 13 additions & 4 deletions src/Fleck/Interfaces/ISocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,24 @@ public interface ISocket
Stream Stream { get; }
bool NoDelay { get; set; }

Task<ISocket> Accept(Action<ISocket> callback, Action<Exception> error);
Task Send(byte[] buffer, Action callback, Action<Exception> error);
Task<int> Receive(byte[] buffer, Action<int> callback, Action<Exception> error, int offset = 0);
Task Authenticate(X509Certificate2 certificate, SslProtocols enabledSslProtocols, Action callback, Action<Exception> error);
Task<ISocket> AcceptAsync(Action<ISocket> callback, Action<Exception> error);
Task SendAsync(byte[] buffer, Action callback, Action<Exception> error);
Task<int> ReceiveAsync(byte[] buffer, Action<int> callback, Action<Exception> error, int offset = 0);
Task AuthenticateAsync(X509Certificate2 certificate, SslProtocols enabledSslProtocols, Action callback, Action<Exception> error);

void Dispose();
void Close();

void Bind(EndPoint ipLocal);
void Listen(int backlog);

[Obsolete]
Task<ISocket> Accept(Action<ISocket> callback, Action<Exception> error);
[Obsolete]
Task Send(byte[] buffer, Action callback, Action<Exception> error);
[Obsolete]
Task<int> Receive(byte[] buffer, Action<int> callback, Action<Exception> error, int offset = 0);
[Obsolete]
Task Authenticate(X509Certificate2 certificate, SslProtocols enabledSslProtocols, Action callback, Action<Exception> error);
}
}
15 changes: 12 additions & 3 deletions src/Fleck/Interfaces/IWebSocketConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ public interface IWebSocketConnection
Action<byte[]> OnPing { get; set; }
Action<byte[]> OnPong { get; set; }
Action<Exception> OnError { get; set; }
Task SendAsync(string message);
Task SendAsync(byte[] message);
Task SendPingAsync(byte[] message);
Task SendPongAsync(byte[] message);
void Close();
IWebSocketConnectionInfo ConnectionInfo { get; }
bool IsAvailable { get; }

[Obsolete]
Task Send(string message);
[Obsolete]
Task Send(byte[] message);
[Obsolete]
Task SendPing(byte[] message);
[Obsolete]
Task SendPong(byte[] message);
void Close();
IWebSocketConnectionInfo ConnectionInfo { get; }
bool IsAvailable { get; }
}
}
32 changes: 26 additions & 6 deletions src/Fleck/SocketWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace Fleck
public class SocketWrapper : ISocket
{
private readonly Socket _socket;
private readonly CancellationTokenSource _tokenSource;
private readonly TaskFactory _taskFactory;
private Stream _stream;
private CancellationTokenSource _tokenSource;
private TaskFactory _taskFactory;

public string RemoteIpAddress
{
Expand Down Expand Up @@ -45,7 +45,7 @@ public SocketWrapper(Socket socket)
_stream = new NetworkStream(_socket);
}

public Task Authenticate(X509Certificate2 certificate, SslProtocols enabledSslProtocols, Action callback, Action<Exception> error)
public Task AuthenticateAsync(X509Certificate2 certificate, SslProtocols enabledSslProtocols, Action callback, Action<Exception> error)
{
var ssl = new SslStream(_stream, false);
_stream = new QueuedStream(ssl);
Expand Down Expand Up @@ -86,7 +86,7 @@ public bool NoDelay
set { _socket.NoDelay = value; }
}

public Task<int> Receive(byte[] buffer, Action<int> callback, Action<Exception> error, int offset)
public Task<int> ReceiveAsync(byte[] buffer, Action<int> callback, Action<Exception> error, int offset)
{
try
{
Expand All @@ -106,7 +106,7 @@ public Task<int> Receive(byte[] buffer, Action<int> callback, Action<Exception>
}
}

public Task<ISocket> Accept(Action<ISocket> callback, Action<Exception> error)
public Task<ISocket> AcceptAsync(Action<ISocket> callback, Action<Exception> error)
{
Func<IAsyncResult, ISocket> end = r => _tokenSource.Token.IsCancellationRequested ? null : new SocketWrapper(_socket.EndAccept(r));
var task = _taskFactory.FromAsync(_socket.BeginAccept, end, null);
Expand Down Expand Up @@ -136,7 +136,7 @@ public int EndSend(IAsyncResult asyncResult)
return 0;
}

public Task Send(byte[] buffer, Action callback, Action<Exception> error)
public Task SendAsync(byte[] buffer, Action callback, Action<Exception> error)
{
if (_tokenSource.IsCancellationRequested)
return null;
Expand All @@ -159,5 +159,25 @@ public Task Send(byte[] buffer, Action callback, Action<Exception> error)
return null;
}
}

Task<ISocket> ISocket.Accept(Action<ISocket> callback, Action<Exception> error)
{
return AcceptAsync(callback, error);
}

Task ISocket.Send(byte[] buffer, Action callback, Action<Exception> error)
{
return SendAsync(buffer, callback, error);
}

Task<int> ISocket.Receive(byte[] buffer, Action<int> callback, Action<Exception> error, int offset)
{
return ReceiveAsync(buffer, callback, error, offset);
}

Task ISocket.Authenticate(X509Certificate2 certificate, SslProtocols enabledSslProtocols, Action callback, Action<Exception> error)
{
return AuthenticateAsync(certificate, enabledSslProtocols, callback, error);
}
}
}
34 changes: 27 additions & 7 deletions src/Fleck/WebSocketConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public WebSocketConnection(ISocket socket, Action<IWebSocketConnection> initiali
OnClose = () => { };
OnMessage = x => { };
OnBinary = x => { };
OnPing = x => SendPong(x);
OnPing = x => SendPongAsync(x);
OnPong = x => { };
OnError = x => { };
_initialize = initialize;
Expand Down Expand Up @@ -57,22 +57,42 @@ public bool IsAvailable {
get { return !_closing && !_closed && Socket.Connected; }
}

public Task Send(string message)
public Task SendAsync(string message)
{
return Send(message, Handler.FrameText);
}

public Task Send(byte[] message)
public Task SendAsync(byte[] message)
{
return Send(message, Handler.FrameBinary);
}

public Task SendPing(byte[] message)
public Task SendPingAsync(byte[] message)
{
return Send(message, Handler.FramePing);
}

public Task SendPong(byte[] message)
public Task SendPongAsync(byte[] message)
{
return Send(message, Handler.FramePong);
}

Task IWebSocketConnection.Send(string message)
{
return Send(message, Handler.FrameText);
}

Task IWebSocketConnection.Send(byte[] message)
{
return Send(message, Handler.FrameBinary);
}

Task IWebSocketConnection.SendPing(byte[] message)
{
return Send(message, Handler.FramePing);
}

Task IWebSocketConnection.SendPong(byte[] message)
{
return Send(message, Handler.FramePong);
}
Expand Down Expand Up @@ -149,7 +169,7 @@ private void Read(List<byte> data, byte[] buffer)
if (!IsAvailable)
return;

Socket.Receive(buffer, r =>
Socket.ReceiveAsync(buffer, r =>
{
if (r <= 0) {
FleckLog.Debug("0 bytes read. Closing.");
Expand Down Expand Up @@ -204,7 +224,7 @@ private void HandleReadError(Exception e)

private Task SendBytes(byte[] bytes, Action callback = null)
{
return Socket.Send(bytes, () =>
return Socket.SendAsync(bytes, () =>
{
FleckLog.Debug("Sent " + bytes.Length + " bytes");
if (callback != null)
Expand Down
4 changes: 2 additions & 2 deletions src/Fleck/WebSocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void Start(Action<IWebSocketConnection> config)

private void ListenForClients()
{
ListenerSocket.Accept(OnClientConnect, e => FleckLog.Error("Listener socket is closed", e));
ListenerSocket.AcceptAsync(OnClientConnect, e => FleckLog.Error("Listener socket is closed", e));
}

private void OnClientConnect(ISocket clientSocket)
Expand Down Expand Up @@ -124,7 +124,7 @@ private void OnClientConnect(ISocket clientSocket)
{
FleckLog.Debug("Authenticating Secure Connection");
clientSocket
.Authenticate(Certificate,
.AuthenticateAsync(Certificate,
EnabledSslProtocols,
connection.StartReceiving,
e => FleckLog.Warn("Failed to Authenticate", e));
Expand Down
4 changes: 2 additions & 2 deletions src/Samples/ConsoleApp/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static void Main()
socket.OnMessage = message =>
{
Console.WriteLine(message);
allSockets.ToList().ForEach(s => s.Send("Echo: " + message));
allSockets.ToList().ForEach(s => s.SendAsync("Echo: " + message));
};
});

Expand All @@ -37,7 +37,7 @@ static void Main()
{
foreach (var socket in allSockets.ToList())
{
socket.Send(input);
socket.SendAsync(input);
}
input = Console.ReadLine();
}
Expand Down