public override void Dispose(string msg) { if (Monitor.TryEnter(this)) { try { if (Remote != null) { Remote = null; IsAvailable = false; ReceiveArgs.Dispose(); SendArgs.Dispose(); Socket.Close(); Socket.Dispose(); Console.WriteLine("Dispose " + msg); } } catch (Exception e) { Console.WriteLine("Dispose " + e.Message); } finally { try { Monitor.Exit(this); } catch (Exception e) { Console.WriteLine("UToken Dispose" + e.Message); } } } }
private async void StartServer() { using (NamedPipeServerStream pipe = new NamedPipeServerStream(id, PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous)) { using (StreamReader reader = new StreamReader(pipe)) { while (IsOpen) { if (!pipe.IsConnected) { await pipe.WaitForConnectionAsync(); } if (!reader.EndOfStream) { string s = await reader.ReadLineAsync(); ReceiveArgs?.Invoke(this, new InstanceArgsEventArgs(s.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries))); } else { pipe.Disconnect(); } } } } }
private void ClientSocket_Event_RecieveMsg(object sender, ReceiveArgs e) { Dispatcher.Invoke(new Action(delegate { string str = ""; if (e.Action == SocketCommand.ActConnected) { str = "连接成功"; } if (e.Action == SocketCommand.ActAbortSocket) { str = "服务器断开连接"; } if (e.Action == SocketCommand.ActMsg) { str = e.Message; } if (ListViwe.Items.Count > 200) { ListViwe.Items.Clear(); } ListViewItem item = new ListViewItem(); item.Content = str; item.Background = Brushes.LawnGreen; ListViwe.Items.Add(item); })); }
void chat_Receive(object sender, ReceiveArgs e) { this.Invoke(new MethodInvoker(() => { DateTime d = DateTime.Now; setMsg(d.ToShortDateString() + ": " + e.Msg); })); }
private void ReceieMsg(object sender, ReceiveArgs e) { Dispatcher.Invoke(new Action(delegate { if (e.Action == SocketCommand.ActConnected) { int count = int.Parse(labUserCount.Content.ToString()); count++; labUserCount.Content = count; } if (e.Action == SocketCommand.ActAbortSocket) { int count = int.Parse(labUserCount.Content.ToString()); count--; labUserCount.Content = count; } if (e.Action == SocketCommand.ActMsg) { if (ListViwe.Items.Count > 200) { ListViwe.Items.Clear(); } ListViewItem item = new ListViewItem(); item.Content = e.Message; item.Background = Brushes.LawnGreen; ListViwe.Items.Add(item); } //如果有其他操作也可以写在这里 })); }
public Task <int> ReceiveAsync(byte[] buffer, int offset, int count) { var tcs = new TaskCompletionSource <int>(); ReceiveArgs.SetBuffer(buffer, offset, count); ReceiveArgs.UserToken = tcs; Socket.ReceiveAsync(ReceiveArgs); return(tcs.Task); }
public Task <int> ReceiveAsync(byte[] buffer, int offset, int count) { var tcs = new TaskCompletionSource <int>(); ReceiveArgs.SetBuffer(buffer, offset, count); ReceiveArgs.UserToken = tcs; if (!Socket.ReceiveAsync(ReceiveArgs)) { return(Task.FromResult(ReceiveArgs.BytesTransferred)); } return(tcs.Task); }
private void ShowMesg(object sender, ReceiveArgs e) { Dispatcher.Invoke(new Action(delegate { if (ListViwe.Items.Count > 200) { ListViwe.Items.Clear(); } ListViewItem item = new ListViewItem(); item.Content = e.Message; item.Background = Brushes.LawnGreen; ListViwe.Items.Add(item); //如果有其他操作也可以写在这里 })); }
public void Start(string[] args) { IsServer = mutex.WaitOne(0); if (IsServer) { IsOpen = true; StartServer(); ReceiveArgs?.Invoke(this, new InstanceArgsEventArgs(args)); } else { sendToServer(args); } }
protected override void Receive() { try { byte[] b = new byte[65535]; ReceiveArgs.SetBuffer(b, 0, b.Length); //直接完成 if (!Socket.ReceiveFromAsync(ReceiveArgs)) { ReceiveCompleted(); } } catch (Exception e) { Console.WriteLine("Receive " + e.Message); } }
/// <summary> /// 发送回执(广播模式下此方法不执行任何操作) /// </summary> public void Ack(ReceiveArgs args) { if (CheckDisposed()) { throw new ObjectDisposedException(nameof(RabbitMQConsumer)); } //广播模式下不需要发送回执 if (IsBroadcast) return; if (args == null) throw new ArgumentNullException(nameof(args)); BasicDeliverEventArgs e = args.OriginArgs as BasicDeliverEventArgs; if (e == null) throw new ArgumentNullException(nameof(args.OriginArgs)); channel.BasicAck(e.DeliveryTag, false); }
private static void Consumer_Received(object sender, ReceiveArgs e) { Console.WriteLine(e.Message.GetString()); }
private void Channel_Received(object sender, ReceiveArgs e) { outputHelper.WriteLine($"【{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}】【{count++}】{e.Message.GetString()}"); Thread.Sleep(100); ((IConsumer)sender).Ack(e); }