Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different Send and Receive Types Info Request #32

Open
NickSt opened this issue Oct 10, 2023 · 1 comment
Open

Different Send and Receive Types Info Request #32

NickSt opened this issue Oct 10, 2023 · 1 comment

Comments

@NickSt
Copy link

NickSt commented Oct 10, 2023

I was wondering if there is a configuration I'm missing somewhere. Most examples seem to show using the same generic type for both sending and receiving. Typically though you rarely would receive an object of the same type as the one you are sending. Is there a way to configure H.Pipes to use different types. My current workaround is just including a string in the message that I then deserialize later, but this takes a way a lot of the value that this library adds as its not super hard just to do that to the standard pipe stream without adding another dependency to a project.

@michalpaukert
Copy link

michalpaukert commented Nov 29, 2023

I am using it like this
`

   public async Task<T> SendIpcMessage<T>(PipeMessage message, CancellationToken cancellation = default) where T : PipeMessage
   {
       if (!_client.IsConnected)
       {
           await _client.ConnectAsync(cancellation).ConfigureAwait(false);
       }

       var messageResult = null as T;
       void ClientOnMessageReceived(object? sender, ConnectionMessageEventArgs<PipeMessage?> e)
       {
           if (e.Message is T result)
           {
               messageResult = result;
           }
       }

       try
       {
           _client.MessageReceived += ClientOnMessageReceived;

           _logger.LogDebug("Sending message {type} to pipe {name}", message.GetType(), _client.PipeName);
           await _client.WriteAsync(message, cancellation);

           while (!cancellation.IsCancellationRequested)
           {
               if (messageResult is { } outMessage)
               {
                   return outMessage;
               }
           }

           throw new TimeoutException();
       }
       catch (Exception e)
       {
           _logger.LogError(e, "Failed to send IPC message {type} to pipe {pipeName}", message.GetType(), _client.PipeName);
           throw;
       }
       finally
       {
           _client.MessageReceived -= ClientOnMessageReceived;
       }
   }

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants