public bool Send(byte command, byte[] text) { try { byte[] sendb = text; byte[] lens = ConvertToByteList(sendb.Length); byte[] b = new byte[2 + lens.Length + sendb.Length]; b[0] = command; b[1] = (byte)lens.Length; lens.CopyTo(b, 2); sendb.CopyTo(b, 2 + lens.Length); int count = (b.Length <= 520 ? b.Length / 520 : (b.Length / 520) + 1); if (count == 0) { IPEndPoint server = new IPEndPoint(IPAddress.Parse(IP), PORT); EndPoint ep = server; tcpc.Client.SendTo(b, ep); } else { throw new Exception("发送数据不得大于520byte"); } } catch (Exception ee) { Isline = false; Stop(); Timeoutevent?.Invoke(); Timeoutobjevent?.Invoke(this); Send(command, text); ErrorMge(9, "send:" + ee.Message); return(false); } return(true); }
public bool Send(byte command, string text) { try { byte[] sendb = System.Text.Encoding.UTF8.GetBytes(text); byte[] lens = System.Text.Encoding.UTF8.GetBytes(sendb.Length.ToString()); byte[] b = new byte[2 + lens.Length + sendb.Length]; b[0] = command; b[1] = (byte)lens.Length; lens.CopyTo(b, 2); sendb.CopyTo(b, 2 + lens.Length); int count = (b.Length <= 520 ? b.Length / 520 : (b.Length / 520) + 1); if (count == 0) { IPEndPoint server = new IPEndPoint(IPAddress.Parse(IP), PORT); EndPoint ep = server; tcpc.Client.SendTo(b, ep); } else { for (int i = 0; i < count; i++) { int zz = b.Length - (i * 520) > 520 ? 520 : b.Length - (i * 520); byte[] temp = new byte[zz]; Array.Copy(b, i * 520, temp, 0, zz); IPEndPoint server = new IPEndPoint(IPAddress.Parse(IP), PORT); EndPoint ep = server; tcpc.Client.SendTo(temp, ep); Thread.Sleep(1); } } } catch (Exception ee) { Isline = false; Stop(); Timeoutevent?.Invoke(); Timeoutobjevent?.Invoke(this); Send(command, text); ErrorMge(9, "send:" + ee.Message); return(false); } return(true); }
public bool Send(byte command, string text) { try { if (DT == DataType.json) { byte[] sendb = System.Text.Encoding.UTF8.GetBytes(text); byte[] lens = System.Text.Encoding.UTF8.GetBytes(sendb.Length.ToString()); byte[] b = new byte[2 + lens.Length + sendb.Length]; b[0] = command; b[1] = (byte)lens.Length; lens.CopyTo(b, 2); sendb.CopyTo(b, 2 + lens.Length); int count = (b.Length <= 40960 ? b.Length / 40960 : (b.Length / 40960) + 1); Send(b); } else if (DT == DataType.bytes) { return(Send(command, System.Text.Encoding.UTF8.GetBytes(text))); } else { return(Send(System.Text.Encoding.UTF8.GetBytes(text))); } } catch (Exception ee) { if (Isline) { Timeoutevent?.Invoke(); Timeoutobjevent?.Invoke(this); Send(command, text); } Isline = false; Stop(); ErrorMge(9, "send:" + ee.Message); return(false); } return(true); }
public bool Send(byte command, byte[] text) { bool bb = false; try { if (DT == DataType.json) { return(Send(command, System.Text.Encoding.UTF8.GetString(text))); } else if (DT == DataType.bytes) { byte[] sendb = text; byte[] lens = ConvertToByteList(sendb.Length); byte[] b = new byte[2 + 2 + lens.Length + sendb.Length]; b[0] = command; b[1] = (byte)lens.Length; lens.CopyTo(b, 2); CRC.ConCRC(ref b, 2 + lens.Length); sendb.CopyTo(b, 2 + 2 + lens.Length); bb = Send(b); } else { return(Send(text)); } } catch (Exception ee) { Isline = false; Stop(); Timeoutevent?.Invoke(); Timeoutobjevent?.Invoke(this); Send(command, text); ErrorMge(9, "send:" + ee.Message); return(false); } return(bb); }
void Receives(object obj) { while (isok) { Thread.Sleep(50); try { int bytesRead = tcpc.Available; if (bytesRead > 0) { byte[] tempbtye = new byte[bytesRead]; try { IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint Remote = sender; timeout = DateTime.Now; tcpc.Client.ReceiveFrom(tempbtye, ref Remote); _0x99: if (tempbtye[0] == 0x99) { timeout = DateTime.Now; if (tempbtye.Length > 1) { byte[] b = new byte[bytesRead - 1]; try { Array.Copy(tempbtye, 1, b, 0, b.Length); } catch { } tempbtye = b; goto _0x99; } else { continue; } } } catch (Exception ee) { ErrorMge(22, ee.Message); } ListData.Add(tempbtye); } else { try { TimeSpan ts = DateTime.Now - timeout; if (ts.TotalSeconds > mytimeout) { Isline = false; Stop(); Timeoutevent?.Invoke(); Timeoutobjevent?.Invoke(this); ErrorMge?.Invoke(2, "连接超时,未收到服务器指令"); continue; } } catch (Exception ee) { ErrorMge(21, ee.Message); } } } catch (Exception e) { ErrorMge?.Invoke(2, e.Message); } } }
void Receives(object obj) { // var w = new SpinWait(); while (isok) { if (ReceivesSpeedMode != WeaveReceivesSpeedMode.high) { //sleep(10) System.Threading.Thread.Sleep((int)ReceivesSpeedMode); } try { if (tcpc.Client == null) { continue; } int bytesRead = tcpc.Client.Available; if (bytesRead > 0) { byte[] tempbtye = new byte[bytesRead]; try { timeout = DateTime.Now; tcpc.Client.Receive(tempbtye); tempp = new byte[alldata.Length]; alldata.CopyTo(tempp, 0); int lle = alldata.Length; bytesRead = tempbtye.Length; byte[] temp = new byte[lle + bytesRead]; Array.Copy(alldata, 0, temp, 0, lle); Array.Copy(tempbtye, 0, temp, lle, bytesRead); alldata = temp; } catch (Exception ee) { ErrorMge(22, ee.Message); } } if (alldata.Length > 3) { Unup(); } else { if (tcpc.Client.Available == 0) { if (resttime > 0) { System.Threading.Thread.Sleep(1); } else { System.Threading.Thread.Yield(); } } } try { TimeSpan ts = DateTime.Now - timeout; if (ts.TotalSeconds > mytimeout) { Isline = false; Stop(); Timeoutevent?.Invoke(); Timeoutobjevent?.Invoke(this); ErrorMge?.Invoke(2, "连接超时,未收到服务器指令"); continue; } } catch (Exception ee) { ErrorMge(21, ee.Message); } } catch (Exception e) { ErrorMge?.Invoke(2, e.Message); } } }