private void WaitforData() { byte[] data = new byte[512]; running = true; while (running) { try // when the connection is forced close --> then here comes an exception (the connection. receive still wait for the data even though the client is not connected) { int byteRead = connection.Receive(data); // save received data in the buffer string msg = System.Text.Encoding.UTF8.GetString(data, 0, byteRead); if (msg.StartsWith("@quit")) { Close(); } else { int split = msg.IndexOf(":"); Username = msg.Substring(0, split); string msgText = msg.Substring(split + 1).Trim(); ReceivedMsg?.Invoke(this, msgText); } } catch (Exception) { } } }
static public isaCommand OnMsgReceived(isaCommand cmd) { isaCommand retcmd = new Command { command = cmd.command }; //this routes back out to the client callback if (ReceivedMsg != null) { retcmd = (isaCommand)ReceivedMsg?.Invoke(cmd); } return(retcmd); }
void ReceiveAction(IAsyncResult ar) { int size = 0; try { size = server.EndReceive(ar); } catch { if (Disconnected != null) { Disconnected.Invoke(server); } server.Close(); return; } if (size == 0) { if (Disconnected != null) { Disconnected.Invoke(server); } server.Close(); } else { if (ReceivedMsg != null) { ReceivedMsg.Invoke(Server, buffer, size); } try { server.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveAction), null); } catch { if (Disconnected != null) { Disconnected.Invoke(server); } } } }
/// <summary> /// 接受操作 /// </summary> /// <param name="ar">Sokcet连接异步结果</param> private void ReceiveAction() { try { while (true) { byte[] vs = new byte[8]; server.Receive(vs); BinaryReader br = new BinaryReader(new MemoryStream(vs)); int head = br.ReadInt32(); int len = br.ReadInt32(); br.Dispose(); if (head == 0x544F4332) { byte[] vs1 = new byte[0]; Array.Resize(ref vs1, len); server.Receive(vs1); try //控制异常范围 { ReceivedMsg.Invoke(this, server, vs1, len); } catch { } } else { throw new Exception(); //抛出异常实现什么都不做 } } } catch { Disconnected.Invoke(this, server); server.Dispose(); } }
public void WaitToRecieveData() { byte[] data = new byte[512]; running = true; while (running) { try { int byteRead = connection.Receive(data); string msg = System.Text.Encoding.UTF8.GetString(data, 0, byteRead); if (msg.StartsWith("@quit")) { Disconnect(); } else { ReceivedMsg?.Invoke(this, msg); } } catch (Exception) { } } }
private void OnSessionReceived(HcToolInfo info, Command cmd, int addr, int[] values) { ReceivedMsg?.Invoke(info, cmd, addr, values); }