Skip to content

Commit 5eaaa2e

Browse files
committed
Substantial improvements to memory use and performance; server can talk back to client; simplified code
1 parent ba31ce5 commit 5eaaa2e

File tree

7 files changed

+318
-362
lines changed

7 files changed

+318
-362
lines changed

SimplSockets/BlockingQueue.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace SimplSockets
66
{
77
/// <summary>
88
/// A queue that wraps a regular generic queue but when empty will block Dequeue threads until an item is available.
9+
/// This class is thread safe.
910
/// </summary>
1011
/// <typeparam name="T">The type of the object contained in the queue.</typeparam>
1112
internal sealed class BlockingQueue<T>

SimplSockets/ISimplSocket.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Net;
3+
4+
namespace SimplSockets
5+
{
6+
/// <summary>
7+
/// Represents a socket.
8+
/// </summary>
9+
public interface ISimplSocket : IDisposable
10+
{
11+
/// <summary>
12+
/// Connects to an endpoint. Once this is called, you must call Close before calling Connect or Listen again. This method will not raise an error.
13+
/// </summary>
14+
/// <param name="endPoint">The endpoint.</param>
15+
/// <returns>true if connection is successful, false otherwise.</returns>
16+
bool Connect(EndPoint endPoint);
17+
18+
/// <summary>
19+
/// Begin listening for incoming connections. Once this is called, you must call Close before calling Connect or Listen again.
20+
/// </summary>
21+
/// <param name="localEndpoint">The local endpoint to listen on.</param>
22+
void Listen(EndPoint localEndpoint);
23+
24+
/// <summary>
25+
/// Sends a message to the server without waiting for a response (one-way communication).
26+
/// </summary>
27+
/// <param name="message">The message to send.</param>
28+
void Send(byte[] message);
29+
30+
/// <summary>
31+
/// Sends a message to the server and receives the response to that message.
32+
/// </summary>
33+
/// <param name="message">The message to send.</param>
34+
/// <returns>The response message.</returns>
35+
byte[] SendReceive(byte[] message);
36+
37+
/// <summary>
38+
/// Sends a message back to the client.
39+
/// </summary>
40+
/// <param name="message">The reply message to send.</param>
41+
/// <param name="receivedMessage">The received message which is being replied to.</param>
42+
void Reply(byte[] message, SimplSocket.ReceivedMessage receivedMessage);
43+
44+
/// <summary>
45+
/// Closes the connection. Once this is called, you can call Connect or Listen again to start a new connection.
46+
/// </summary>
47+
void Close();
48+
49+
/// <summary>
50+
/// Gets the currently connected client count (when acting as a client, this will return 0 or 1).
51+
/// </summary>
52+
int CurrentlyConnectedClientCount { get; }
53+
54+
/// <summary>
55+
/// An event that is fired whenever a message is received. Hook into this to process messages and potentially call Reply to send a response.
56+
/// </summary>
57+
event EventHandler<MessageReceivedArgs> MessageReceived;
58+
59+
/// <summary>
60+
/// An event that is fired whenever a socket communication error occurs. Hook into this to do something when communication errors happen.
61+
/// </summary>
62+
event EventHandler<SocketErrorArgs> Error;
63+
}
64+
}

SimplSockets/ISimplSocketClient.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

SimplSockets/ISimplSocketServer.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

SimplSockets/Pool.cs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Pool(int poolCount, Func<T> newItemMethod, Action<T> resetItemMethod)
3333
}
3434

3535
/// <summary>
36-
/// Pushes an item into the pool for later re-use.
36+
/// Pushes an item into the pool for later re-use. The item will have its state reset.
3737
/// </summary>
3838
/// <param name="item">The item.</param>
3939
public void Push(T item)
@@ -44,6 +44,11 @@ public void Push(T item)
4444
return;
4545
}
4646

47+
if (_resetItemMethod != null)
48+
{
49+
_resetItemMethod(item);
50+
}
51+
4752
lock (_queue)
4853
{
4954
_queue.Enqueue(item);
@@ -53,42 +58,26 @@ public void Push(T item)
5358
/// <summary>
5459
/// Pops an item out of the pool for use. The item will have its state reset.
5560
/// </summary>
56-
/// <returns></returns>
61+
/// <returns>An item.</returns>
5762
public T Pop()
5863
{
5964
T result = null;
6065

61-
// Cheap check
62-
if (_queue.Count == 0)
63-
{
64-
result = _newItemMethod();
65-
if (_resetItemMethod != null)
66-
{
67-
_resetItemMethod(result);
68-
}
69-
return result;
70-
}
71-
7266
lock (_queue)
7367
{
7468
// Double lock check
75-
if (_queue.Count == 0)
69+
if (_queue.Count > 0)
7670
{
77-
result = _newItemMethod();
78-
if (_resetItemMethod != null)
79-
{
80-
_resetItemMethod(result);
81-
}
82-
return result;
71+
return _queue.Dequeue();
8372
}
73+
}
8474

85-
result = _queue.Dequeue();
86-
if (_resetItemMethod != null)
87-
{
88-
_resetItemMethod(result);
89-
}
90-
return result;
75+
result = _newItemMethod();
76+
if (_resetItemMethod != null)
77+
{
78+
_resetItemMethod(result);
9179
}
80+
return result;
9281
}
9382
}
9483
}

0 commit comments

Comments
 (0)