|
|
@@ -0,0 +1,88 @@ |
|
|
|
public interface INetMqAccessor : IDisposable |
|
|
|
{ |
|
|
|
void StartServer(string endpoint); |
|
|
|
void SendMessage(string message); |
|
|
|
event Action<string> OnMessageReceived; |
|
|
|
} |
|
|
|
public class NetMqAccessor : INetMqAccessor |
|
|
|
{ |
|
|
|
private RouterSocket server; |
|
|
|
private NetMQPoller poller; |
|
|
|
|
|
|
|
public event Action<string> OnMessageReceived; |
|
|
|
|
|
|
|
public void StartServer(string endpoint) |
|
|
|
{ |
|
|
|
server = new RouterSocket(); |
|
|
|
server.Bind(endpoint); |
|
|
|
|
|
|
|
server.ReceiveReady += (s, e) => |
|
|
|
{ |
|
|
|
var msg = e.Socket.ReceiveMultipartMessage(); |
|
|
|
var clientAddress = msg[0]; |
|
|
|
var content = msg[2].ConvertToString(); |
|
|
|
|
|
|
|
OnMessageReceived?.Invoke(content); |
|
|
|
|
|
|
|
var response = new NetMQMessage(); |
|
|
|
response.Append(clientAddress); |
|
|
|
response.AppendEmptyFrame(); |
|
|
|
response.Append($"Echo: {content}"); |
|
|
|
server.SendMultipartMessage(response); |
|
|
|
}; |
|
|
|
|
|
|
|
poller = new NetMQPoller { server }; |
|
|
|
poller.RunAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
public void SendMessage(string message) |
|
|
|
{ |
|
|
|
// Optionnel si tu veux aussi gérer un client ici |
|
|
|
} |
|
|
|
|
|
|
|
public void Dispose() |
|
|
|
{ |
|
|
|
poller?.Stop(); |
|
|
|
server?.Dispose(); |
|
|
|
} |
|
|
|
} |
|
|
|
//////// |
|
|
|
public interface INetMqClientAccessor : IDisposable |
|
|
|
{ |
|
|
|
void Connect(string endpoint); |
|
|
|
void SendMessage<T>(T message); |
|
|
|
Task<TResponse> SendAndReceive<TRequest, TResponse>(TRequest message); |
|
|
|
} |
|
|
|
|
|
|
|
public class NetMqClientAccessor : INetMqClientAccessor |
|
|
|
{ |
|
|
|
private RequestSocket client; |
|
|
|
|
|
|
|
public void Connect(string endpoint) |
|
|
|
{ |
|
|
|
client = new RequestSocket(); |
|
|
|
client.Connect(endpoint); |
|
|
|
} |
|
|
|
|
|
|
|
public void SendMessage<T>(T message) |
|
|
|
{ |
|
|
|
byte[] packed = MessagePackSerializer.Serialize(message); |
|
|
|
client.SendFrame(packed); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<TResponse> SendAndReceive<TRequest, TResponse>(TRequest message) |
|
|
|
{ |
|
|
|
byte[] packed = MessagePackSerializer.Serialize(message); |
|
|
|
client.SendFrame(packed); |
|
|
|
|
|
|
|
// Attente de la réponse |
|
|
|
byte[] responseBytes = client.ReceiveFrameBytes(); |
|
|
|
TResponse response = MessagePackSerializer.Deserialize<TResponse>(responseBytes); |
|
|
|
return response; |
|
|
|
} |
|
|
|
|
|
|
|
public void Dispose() |
|
|
|
{ |
|
|
|
client?.Dispose(); |
|
|
|
} |
|
|
|
} |