public void Send(Message message, TimeSpan timeout)
 {
     base.ThrowIfDisposedOrNotOpen();
     lock (writeLock)
     {
         try
         {
             var encodedBytes = EncodeMessage(message);
             channel.SendMessage(encodedBytes.Array, encodedBytes.Offset, encodedBytes.Count);
         }
         catch (SocketException socketException)
         {
             throw ConvertSocketException(socketException, "Receive");
         }
     }
 }
示例#2
0
        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);
            }
        }
示例#3
0
        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}");
            }
        }
 public static void SendMessage(this IAsyncDvcChannel @this, byte[] data)
 => @this.SendMessage(data, 0, data.Length);
        private void _bt_Click(object sender, RoutedEventArgs e)
        {
            var rs = Encoding.UTF8.GetBytes(textBox.Text);

            target?.SendMessage(rs, 0, rs.Length);
        }