private void Run_waitClient(object o) { while (true) { IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, PORTNUM); byte[] recvBuf = mServer.Receive(ref remoteEP); if (recvBuf == null || recvBuf.Length == 0) { break; } string ipAddr = remoteEP.ToString(); if (!mClients.ContainsKey(ipAddr)) { mClients[ipAddr] = new FifoBuffer(MAX_FIFO_BUF); } FifoBuffer fifoBuf = mClients[ipAddr]; fifoBuf.Push(recvBuf, recvBuf.Length); while (true) { int len = fifoBuf.GetSize(); byte[] buf = fifoBuf.readSize(len); ICD.HEADER msg = ICD.HEADER.ConvertBytesToICDMessage(buf); if (msg == null) { break; } fifoBuf.Pop(msg.msgSize); OnProcRead?.Invoke(ipAddr, msg); } } }
private void Run_waitClient(object o) { TcpClient client = (TcpClient)o; string ipAddr = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString(); NetworkStream stream = client.GetStream(); FifoBuffer fifoBuf = new FifoBuffer(MAX_FIFO_BUF); byte[] buff = new byte[MAX_SOCKET_BUF]; while (true) { int nBytes = 0; try { nBytes = stream.Read(buff, 0, buff.Length); if (nBytes == 0) { break; } } catch (Exception e) { LOG.echo(e.ToString()); break; } fifoBuf.Push(buff, nBytes); while (true) { int len = fifoBuf.GetSize(); byte[] buf = fifoBuf.readSize(len); ICD.HEADER msg = ICD.HEADER.ConvertBytesToICDMessage(buf); if (msg == null) { break; } fifoBuf.Pop(msg.msgSize); OnProcRead?.Invoke(ipAddr, msg); } } fifoBuf.Clear(); stream.Close(); CloseClient(ipAddr); }