Browse Source

ajout help

master
Christophe Brachet 3 weeks ago
parent
commit
54f1a4787b
1 changed files with 88 additions and 0 deletions
  1. +88
    -0
      help.txt

+ 88
- 0
help.txt View File

@@ -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();
}
}

Loading…
Cancel
Save