public ServerDuplexSessionChannel(MessageEncoderFactory messageEncoderFactory, BufferManager bufferManager, int maxBufferSize, IAsyncDvcChannel socket, EndpointAddress localAddress, ChannelManagerBase channelManager) : base(messageEncoderFactory, bufferManager, maxBufferSize, AnonymousAddress, localAddress, AnonymousAddress.Uri, channelManager) { base.InitializeSocket(socket); }
private static async void ReceiveAsync(IAsyncDvcChannel obj) { while (true) { Console.WriteLine($"{obj.GetHashCode():x8}\t > " + Encoding.UTF8.GetString(await obj.ReadMessageAsync())); } }
public ConnectionProxy(string serviceName, IAsyncDvcChannel connection) { this.ServiceName = serviceName; this.Connection = connection; this.CallbackProxy = new ClientCallbackProxy(this); this.ReadThread = RunReadThread(); }
private static async Task ReadAsync(IAsyncDvcChannel dvc) { while (true) { var packet = await dvc.ReadMessageAsync(); Console.WriteLine("> " + Encoding.UTF8.GetString(packet)); } }
protected void InitializeSocket(IAsyncDvcChannel socket) { if (this.channel != null) { throw new InvalidOperationException("Socket is already set"); } this.channel = socket; }
protected override void OnAbort() { try { channel?.Dispose(); } catch (ObjectDisposedException) { /* no-op */ } catch (OperationCanceledException) { /* no-op */ } catch (DvcChannelDisconnectedException) { /* no-op */ } channel = null; }
private static async void HandleTest1(IAsyncDvcChannel obj) { await obj.SendMessageAsync(Encoding.UTF8.GetBytes("Connected")); while (true) { var text = Encoding.UTF8.GetString(await obj.ReadMessageAsync()); Console.WriteLine($"{obj.GetHashCode():x8}\tReceived {text}"); await obj.SendMessageAsync(Encoding.UTF8.GetBytes(text)); } }
// Called from DVC API public async void AcceptConnection(IAsyncDvcChannel channel) { ServiceRegistration existingRegistration; try { var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); // Read service name var serviceNameUtf8 = await channel.ReadMessageAsync(cts.Token).ConfigureAwait(false); var serviceName = Encoding.UTF8.GetString(serviceNameUtf8); // Lookup service lock (syncRegisteredServices) { if (!RegisteredServices.TryGetValue(serviceName, out existingRegistration)) { throw new ChannelNotAvailableException($"Service '{serviceName}' not registered"); } } // Accept await channel.SendMessageAsync(Encoding.UTF8.GetBytes("ACCEPTED")).ConfigureAwait(false); } catch (Exception ex) { Logger.Error(ex, "Exception trying to accept a new brokered connection"); try { await channel.SendMessageAsync(Encoding.UTF8.GetBytes("ERROR Service not registered")).ConfigureAwait(false); channel.Dispose(); } catch (OperationCanceledException) { /* no-op */ } catch (ObjectDisposedException) { /* no-op */ } catch (DvcChannelDisconnectedException) { /* no-op */ } catch (Exception ex2) { Logger.Error(ex2, "Could not notify RDS endpoint of failed conncetion attempt"); } return; } if (!existingRegistration.TryAcceptConnection(channel)) { KillRegistration(existingRegistration); } }
private void AcceptConnection(IAsyncDvcChannel obj) { this.target = obj; Dispatcher.Invoke(() => { ReadAsync(obj); }); obj.Disconnected += (_1, e) => { Dispatcher.Invoke(() => { listBox.Items.Add("Disconnected"); }); }; }
internal bool TryAcceptConnection(IAsyncDvcChannel connection) { AssertAlive(); var connectionProxy = new ConnectionProxy(ServiceName, connection); try { connectionProxy.SetHandler(Target.AcceptConnection(connectionProxy.CallbackProxy)); return(true); } catch (COMException ex) when(ex.HResult == unchecked ((int)0x800706ba) /* RPC_S_SERVER_UNAVAILABLE */) { // caller needs to ditch this instance try { connection.SendMessage(Encoding.UTF8.GetBytes("ERROR Service is no longer available")); } catch (OperationCanceledException) { /* no-op */ } catch (ObjectDisposedException) { /* no-op */ } catch (DvcChannelDisconnectedException) { /* no-op */ } catch (Exception ex2) { Logger.Error(ex2, "Could not notify RDS endpoint of failed conncetion attempt"); } // this will tear down connection, too. connectionProxy.Dispose(); return(false); } catch (Exception ex) { try { connectionProxy.Dispose(); } catch (Exception ex2) { Logger.Error(ex2, $"Could not tear down new connection for {ServiceName}"); } Logger.Error(ex, $"Could not handle new connection for {ServiceName}"); return(true); } }
private static async void HandleTest2(IAsyncDvcChannel obj) { Console.WriteLine($"{obj.GetHashCode():x8}\tConnected"); obj.Disconnected += (_2, e) => Console.WriteLine($"{obj.GetHashCode():x8}\tDisconnected"); ReceiveAsync(obj); try { while (true) { await Task.Delay(5000); obj.SendMessage(Encoding.UTF8.GetBytes($"{obj.GetHashCode():x8}\t HB {DateTime.Now}")); } } catch (Exception ex) { Console.WriteLine($"{obj.GetHashCode():x8}\tCannot reply {ex}"); } }
private async void ReadAsync(IAsyncDvcChannel obj) { try { while (true) { listBox.Items.Add(Encoding.UTF8.GetString(await obj.ReadMessageAsync())); } } catch (TaskCanceledException) { listBox.Items.Add("Ending read due to cancelled"); } catch (ObjectDisposedException) { listBox.Items.Add("Ending read due to ODE"); } catch (DvcChannelDisconnectedException) { listBox.Items.Add("Ending read due to disconnected"); } }
public static Task SendMessageAsync(this IAsyncDvcChannel @this, byte[] data) => @this.SendMessageAsync(data, 0, data.Length);
public static void SendMessage(this IAsyncDvcChannel @this, byte[] data) => @this.SendMessage(data, 0, data.Length);