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
24 changes: 24 additions & 0 deletions SimplSockets/ClientConnectedArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;

namespace SimplSockets
{
/// <summary>
/// Client connected Args
/// </summary>
public class ClientConnectedArgs : EventArgs
{
/// <summary>
/// Internal Constructor
/// </summary>
/// <param name="guid">Connection's GUID</param>
public ClientConnectedArgs(Guid guid)
{
Guid = guid;
}

/// <summary>
/// The connection's GUID.
/// </summary>
public Guid Guid { get; set; }
}
}
2 changes: 1 addition & 1 deletion SimplSockets/ISimplSocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface ISimplSocketServer : IDisposable
/// <summary>
/// An event that is fired when a client successfully connects to the server. Hook into this to do something when a connection succeeds.
/// </summary>
event EventHandler ClientConnected;
event EventHandler<ClientConnectedArgs> ClientConnected;

/// <summary>
/// An event that is fired whenever a message is received. Hook into this to process messages and potentially call Reply to send a response.
Expand Down
2 changes: 1 addition & 1 deletion SimplSockets/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.4.7.*")]
[assembly: AssemblyVersion("1.5.0.*")]
5 changes: 4 additions & 1 deletion SimplSockets/ReceivedMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public class ReceivedMessage
{
internal Socket Socket;
internal int ThreadId;

/// <summary>
/// The connection's GUID.
/// </summary>
public Guid Guid { get; set; }
/// <summary>
/// The message bytes.
/// </summary>
Expand Down
32 changes: 30 additions & 2 deletions SimplSockets/SimplSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net;
using System.Net.Sockets;
using System.Runtime.Caching;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -160,7 +161,7 @@ public bool Connect(EndPoint endPoint)
_socket.NoDelay = !_useNagleAlgorithm;
// Set the linger state
_socket.LingerState = _lingerOption;

// Post a connect to the socket synchronously
try
{
Expand Down Expand Up @@ -536,7 +537,7 @@ private void CompleteMessage(Socket handler, int threadId, byte[] message)
var receivedMessage = _receivedMessagePool.Pop();
receivedMessage.Socket = handler;
receivedMessage.ThreadId = threadId;
receivedMessage.Message = message;
receivedMessage.Message = message;

// Fire the event if needed
var messageReceived = MessageReceived;
Expand All @@ -555,6 +556,33 @@ private void CompleteMessage(Socket handler, int threadId, byte[] message)
_receivedMessagePool.Push(receivedMessage);
}

/// <summary>
/// Sends a message back to the server.
/// </summary>
/// <param name="message">The reply message to send.</param>
/// <param name="receivedMessage">The received message which is being replied to.</param>
public void Reply(byte[] message, ReceivedMessage receivedMessage)
{
// Sanitize
if (message == null)
{
throw new ArgumentNullException("message");
}
if (receivedMessage.Socket == null)
{
throw new ArgumentException("contains corrupted data", "receivedMessageState");
}

var messageWithControlBytes = ProtocolHelper.AppendControlBytesToMessage(message, receivedMessage.ThreadId);

var socketAsyncEventArgs = _socketAsyncEventArgsSendPool.Pop();
socketAsyncEventArgs.SetBuffer(messageWithControlBytes, 0, messageWithControlBytes.Length);

// Do the send to the appropriate client
TryUnsafeSocketOperation(receivedMessage.Socket, SocketAsyncOperation.Send, socketAsyncEventArgs);
}


/// <summary>
/// Handles an error in socket communication.
/// </summary>
Expand Down
Loading