I made a server and client API for TCP connection. It is intended to shorten the work you need to do to prepare the server and client.
It's separated into 3 projects - TCPServer
, TCPClient
, Common
.
I've made an extended tcpclient
type IdentifiableTcpClient
, allowing for various modifications, the base additions are a Name and an ID. The class implements IComponentConsumer
which allows for components based system to be created, further extending the flexibility of the client. There is also a DTO type used for serialization to pass through the network IdentifiableTcpClientDTO
.
All components are required to inherit a marker interface IComponent
.
IComponent
public interface IComponent
{
}
IComponentConsumer
public interface IComponentConsumer
{
IComponentConsumer AddComponent<T>(T component) where T : class, IComponent;
T GetComponent<T>() where T : class, IComponent;
}
IdentifiableTcpClient
public class IdentifiableTcpClient : IEquatable<IdentifiableTcpClient>, IComponentConsumer, ICloneable
{
private readonly Dictionary<Type, IComponent> _components = new Dictionary<Type, IComponent>();
private readonly Guid _internalID;
public string Name { get; set; }
public TcpClient TcpClient { get; }
public IdentifiableTcpClient(TcpClient tcpClient, string name)
: this(tcpClient, name, Guid.NewGuid())
{
}
public IdentifiableTcpClient(TcpClient tcpClient)
: this(tcpClient, Environment.UserName)
{
}
//Used for cloning
private IdentifiableTcpClient(TcpClient tcpClient, string name, Guid internalID)
{
_internalID = internalID;
TcpClient = tcpClient;
Name = name;
}
public bool Equals(IdentifiableTcpClient other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return _internalID.Equals(other._internalID);
}
public override bool Equals(object obj)
{
return obj is IdentifiableTcpClient client && Equals(client);
}
public override int GetHashCode()
{
return _internalID.GetHashCode();
}
public object Clone()
{
return new IdentifiableTcpClient(TcpClient, Name, _internalID);
}
public IComponentConsumer AddComponent<T>(T component)
where T : class, IComponent
{
var type = typeof(T);
if (_components.ContainsKey(type))
{
_components[type] = component;
}
else
{
_components.Add(type, component);
}
return this;
}
public T GetComponent<T>()
where T : class, IComponent
{
if (_components.TryGetValue(typeof(T), out var component))
{
return (T)component;
}
return null;
}
}
IdentifiableTcpClientDTO
[Serializable]
public class IdentifiableTcpClientDTO
{
public string Name { get; set; }
public int HashCode { get; set; }
public IdentifiableTcpClientDTO(IdentifiableTcpClient client)
{
Name = client.Name;
HashCode = client.GetHashCode();
}
public IdentifiableTcpClientDTO()
{
}
}
There is also support for messages, commands and direct modifications on the client:
Message
public enum MessageType
{
Message,
Command
}
[Serializable]
public class Message
{
public IdentifiableTcpClientDTO SenderDTO { get; }
public string Data { get; }
public MessageType MessageType { get; }
public IEnumerable<IModification<IdentifiableTcpClient>> Modifications { get; }
public Message(IdentifiableTcpClientDTO senderDTO, string data, MessageType messageType,
IEnumerable<IModification<IdentifiableTcpClient>> modifications)
{
SenderDTO = senderDTO;
Data = data;
MessageType = messageType;
Modifications = modifications;
}
}
Where Modifications
are a couple of types allowing direct modifications to the client's properties/fields, they are not really relevant for this question so I will leave them at that.
Those messages are later serialized/deserialized by MessageSerializer
MessageSerializer
public static class MessageSerializer
{
public static byte[] Serialize(Message message)
{
using (var memoryStream = new MemoryStream())
{
new BinaryFormatter().Serialize(memoryStream, message);
return memoryStream.ToArray();
}
}
public static Message Deserialize(byte[] messageData)
{
using (var memoryStream = new MemoryStream(messageData))
{
return (Message) new BinaryFormatter().Deserialize(memoryStream);
}
}
}
Moving on to the tcp server:
ClientDataEventArgs
public class ClientDataEventArgs
{
public IdentifiableTcpClient Client { get; }
public string Message { get; }
public ClientDataEventArgs(IdentifiableTcpClient client, string message)
{
Client = client;
Message = message;
}
}
TCPServerMain
public class TCPServerMain
{
public event EventHandler<IdentifiableTcpClient> ClientConnected;
public event EventHandler<IdentifiableTcpClient> ClientDisconnected;
public event EventHandler<ClientDataEventArgs> ClientDataReceived;
public TimeSpan DisconnectInterval { get; set; } = TimeSpan.FromMilliseconds(100);
public IdentifiableTcpClientDTO Identity => SharedConfiguration.ServerIdentity;
private static readonly object _padlock = new object();
private readonly Timer _disconnectTimer;
public HashSet<IdentifiableTcpClient> Clients { get; } = new HashSet<IdentifiableTcpClient>();
public TCPServerMain()
{
_disconnectTimer = new Timer(
callback: DisconnectTimerCallback,
state: null,
dueTime: (int) DisconnectInterval.TotalMilliseconds,
period: (int) DisconnectInterval.TotalMilliseconds);
var localAdd = IPAddress.Parse(ConfigurationResources.IPAdress);
var listener = new TcpListener(localAdd, int.Parse(ConfigurationResources.PortNumber));
listener.Start();
listener.BeginAcceptTcpClient(ClientConnectedCallback, listener);
}
private void DisconnectTimerCallback(object state)
{
lock (_padlock)
{
var faultedClients = new HashSet<IdentifiableTcpClient>();
foreach (var tcpClient in Clients)
{
if (!tcpClient.TcpClient.Client.IsConnected())
{
faultedClients.Add(tcpClient);
}
}
foreach (var tcpClient in faultedClients)
{
OnDisconnectedClient(tcpClient);
}
}
}
private void ClientConnectedCallback(IAsyncResult ar)
{
lock (_padlock)
{
var listener = (TcpListener) ar.AsyncState;
try
{
var client = new IdentifiableTcpClient(listener.EndAcceptTcpClient(ar));
OnClientConnected(client);
BeginReadClientDataCallback(client);
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
listener.BeginAcceptTcpClient(ClientConnectedCallback, listener);
}
}
}
private void BeginReadClientDataCallback(IdentifiableTcpClient client)
{
var buffer = new byte[client.TcpClient.ReceiveBufferSize];
try
{
client.TcpClient.GetStream().BeginRead(buffer, 0, client.TcpClient.ReceiveBufferSize,
ar => ClientDataReceivedCallback(buffer, ar), client);
}
catch (InvalidOperationException e)
{
OnDisconnectedClient(client);
Console.WriteLine(e);
}
}
private void ClientDataReceivedCallback(byte[] buffer, IAsyncResult ar)
{
var client = (IdentifiableTcpClient)ar.AsyncState;
try
{
var clientStream = client.TcpClient.GetStream();
var bytesRead = clientStream.EndRead(ar);
if (bytesRead == 0)
{
return;
}
var data = Encoding.GetEncoding("Windows-1251").GetString(buffer, 0, bytesRead);
OnClientDataReceived(client, data);
}
catch (InvalidOperationException)
{
}
catch (IOException e)
{
if (!(e.InnerException is SocketException))
{
throw;
}
}
finally
{
BeginReadClientDataCallback(client);
}
}
private void OnClientConnected(IdentifiableTcpClient client)
{
Clients.Add(client);
ClientConnected?.Invoke(this, client);
}
private void OnClientDataReceived(IdentifiableTcpClient client, string message)
{
ClientDataReceived?.Invoke(this, new ClientDataEventArgs(client, message));
}
private void OnDisconnectedClient(IdentifiableTcpClient client)
{
Clients.Remove(client);
ClientDisconnected?.Invoke(this, client);
}
}
TCPClientMain
public class TCPClientMain
{
public event EventHandler<string> ServerDataReceived;
private readonly IdentifiableTcpClient _currentClient;
public IdentifiableTcpClientDTO Identity => new IdentifiableTcpClientDTO(_currentClient);
public TCPClientMain(string serverIP, int portNumber)
{
_currentClient = new IdentifiableTcpClient(new TcpClient(serverIP, portNumber));
}
public TCPClientMain()
: this(ConfigurationResources.IPAdress, int.Parse(ConfigurationResources.PortNumber))
{
}
public void SendMessageToServer(string message)
{
var bytesToSend = Encoding.GetEncoding("Windows-1251").GetBytes(message);
_currentClient.TcpClient.GetStream().Write(bytesToSend, 0, bytesToSend.Length);
}
public void BeginReadDataFromServer()
{
BeginReadServerDataCallback(_currentClient);
}
private void BeginReadServerDataCallback(IdentifiableTcpClient client)
{
var buffer = new byte[client.TcpClient.ReceiveBufferSize];
client.TcpClient.GetStream().BeginRead(buffer, 0, client.TcpClient.ReceiveBufferSize,
ar => ClientDataReceivedCallback(buffer, ar), client);
}
private void ClientDataReceivedCallback(byte[] buffer, IAsyncResult ar)
{
var client = (IdentifiableTcpClient) ar.AsyncState;
var clientStream = client.TcpClient.GetStream();
var bytesRead = clientStream.EndRead(ar);
if (bytesRead == 0)
{
return; //disconnected
}
var message = MessageSerializer.Deserialize(buffer);
ProcessMessageFromServer(client, message);
BeginReadServerDataCallback(client);
}
private void ProcessMessageFromServer(IdentifiableTcpClient client, Message message)
{
if (message.MessageType == MessageType.Message)
{
OnServerDataReceived(message.SenderDTO, message.Data);
}
else
{
foreach (var modification in message.Modifications)
{
modification.Modify(client);
}
}
}
private void OnServerDataReceived(IdentifiableTcpClientDTO sender, string message)
{
ServerDataReceived?.Invoke(sender, message);
}
}
A couple of helper classes:
SharedConfiguration
public static class SharedConfiguration
{
public static IdentifiableTcpClientDTO ServerIdentity { get; }
static SharedConfiguration()
{
ServerIdentity = new IdentifiableTcpClientDTO { HashCode = -1, Name = "SERVER" };
}
}
SocketExtensions
public static class SocketExtensions
{
public static bool IsConnected(this Socket socket)
{
try
{
return !(socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0);
}
catch (SocketException) { return false; }
}
}
This is my first TCP/IP client/server implementation so it's more than likely I've made some rookie mistakes and that's what I'd like the focus to be on. Code-style and optimizations are of course welcome, especially the latter, since it's pretty important.
SharedConfiguration
class it's inside the Common project, the resources are simply resource files also located in the afore mentionted assembly. If the API is public, those should probably be parameters as well. \$\endgroup\$IModification
andIdentifiableTcpClient
. \$\endgroup\$