private void ReceiveThread2(string key) { Socket handler = this.GetHandler(key); List <byte> result_buffer = new List <byte>(); result_buffer.AddToEnd(GetResidue(key)); byte[] buffer = new byte[PacketSizeTCP]; int received = 0; int bytes_received = 0; while (handler.IsAvailableToRead()) { try { //while (Speed > MaxSpeed) Thread.Sleep(1); bytes_received = 0; bytes_received = handler.Receive(buffer, 0, buffer.Length, SocketFlags.None); if (bytes_received > 0) { BandwidthBuffer.Write((int)((double)40 * Math.Ceiling((double)bytes_received / 1500)) + bytes_received); } } catch (Exception ex) { Exceptions.Write(ex); break; } if (bytes_received > 0) { result_buffer.AddRange(buffer.Take(bytes_received)); received += bytes_received; } else { break; } } var packet = result_buffer.GetArray(); if (packet.IsNullOrEmpty()) { this.SetResidue(key, null); return; } int offset; int length; byte compression; if (!TcpAsyncCommon.RecreatePacket(ref packet, out offset, out length, out compression, PacketMode) || (offset + length) > packet.Length) { this.SetResidue(key, packet.Copy()); return; } byte[] result = packet.SubArray(offset, length); //+ 1 represents EndByte this.SetResidue(key, packet.SubArray((offset + length + 1), packet.Length - (offset + length + 1))); if (result.IsNullOrEmpty()) { return; } if (compression == 1) { result = result.UnGZip(); } SetReceiver(key, result); }
private bool SendThread(string key) { if (D3BSend == null || key.IsNullOrEmpty()) { return(false); } BufferedArray <byte[]> DBuffer = D3BSend.Get(key); if (DBuffer == null || DBuffer.IsAllRead) { return(false); } StateObject state = this.GetState(key); Socket handler = this.GetHandler(key); byte[] data; if (!handler.IsConnected() || DBuffer == null || DBuffer.IsAllRead || !DBuffer.Read(out data)) { return(false); } byte[] result_data = TcpAsyncCommon.CreatePacket(data, PacketMode); if (data.IsNullOrEmpty()) { return(false); } int sent = 0; int size = result_data.Length; do { int packet = size - sent; if (packet > PacketSizeTCP) { packet = PacketSizeTCP; } try { while (Speed > MaxSpeed) { Thread.Sleep(1); } int bytes_send = handler.Send(result_data, sent, packet, SocketFlags.None); sent += bytes_send; if (bytes_send > 0) { BandwidthBuffer.Write((int)((double)40 * Math.Ceiling((double)bytes_send / 1500)) + bytes_send); } } catch (Exception ex) { Exceptions.Write(ex); return(false); } }while (sent < size); return(true); }