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

New Connected event in NamedPipeClient and minor changes #9

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

New Connected event in NamedPipeClient and minor changes #9

wants to merge 6 commits into from

Conversation

filippobottega
Copy link

@filippobottega filippobottega commented Oct 13, 2016

To solve issue #7 i have created the method StartSynchronously and the event Connected in NamedPipeClient.
Now it's possible to call PushMessage in Connected event handler without using WaitForConnection.
Use instead a blocking collection in Connected and Error event handlers.
Minor changes: WaitForConnection and WaitForDisconnection methods return WaitOne result.

Sample code:

      Dim responseGate = New Concurrent.BlockingCollection(Of YourMessageClass)
      Dim pushMessageGate = New Concurrent.BlockingCollection(Of Boolean)

      Dim client = New NamedPipeClient(Of YourMessageClass)(pipeName, serverName)

      Dim errorEventHandler = New PipeExceptionEventHandler(
        Sub(exception)
          pushMessageGate.Add(False)
        End Sub)

      Dim connectedEventHandler = New ConnectionEventHandler(Of YourMessageClass, YourMessageClass)(
        Sub(connection)
          connection.PushMessage(messageToPush)
          pushMessageGate.Add(True)
        End Sub)

      Dim serverMessageEventHandler = New ConnectionMessageEventHandler(Of YourMessageClass, YourMessageClass)(
        Sub(connection As NamedPipeConnection(Of YourMessageClass, YourMessageClass), message As YourMessageClass)
          responseGate.Add(message)
        End Sub)

      Dim shootDownClient =
        Sub()
          client.Stop()
          RemoveHandler client.Error, errorEventHandler
          RemoveHandler client.Connected, connectedEventHandler
          RemoveHandler client.ServerMessage, serverMessageEventHandler
        End Sub

      AddHandler client.Error, errorEventHandler
      AddHandler client.Connected, connectedEventHandler
      AddHandler client.ServerMessage, serverMessageEventHandler

      client.StartSynchronously()

      Dim pushMessageResult As Boolean = False
      If Not pushMessageGate.TryTake(pushMessageResult, 10000) Then
        ' Pipeline Timeout
        shootDownClient.Invoke
        Return
      End If

      If pushMessageResult = False Then
        shootDownClient.Invoke
        Return
      End If

      Dim responseMessage As YourMessageClass = Nothing
      If Not responseGate.TryTake(responseMessage, millisecondsTimeout) Then
        ' Client Timeout
        shootDownClient.Invoke
        Return
      End If

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

Successfully merging this pull request may close these issues.

1 participant