///<summary>Called when we have received data from the local client.<br>Incoming data will immediately be forwarded to the remote host.</br></summary> ///<param name="ar">The result of the asynchronous operation.</param> protected void OnClientReceive(IAsyncResult ar) { try { int Ret = ClientSocket.EndReceive(ar); if (Ret <= 0) { Dispose(); return; } byte[] recv = new byte[Ret]; System.Array.Copy(_buffer, 0, recv, 0, recv.Length); //byte[] encrypt = recv; //Logger.ThreadWrite("sending bytes:\t" + recv.Length); byte[] encrypt = _aes.encryptNet(_key, recv); recv = null; //using (var ns = new NetworkStream(DestinationSocket)) //{ // ns.Write(encrypt, 0, encrypt.Length); // ns.Flush(); //} int sentCount = 0; while (sentCount < encrypt.Length) { var toSend = Math.Min(encrypt.Length - sentCount, 1024); sentCount += DestinationSocket.Send(encrypt, sentCount, toSend, SocketFlags.None); } ClientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(this.OnClientReceive), ClientSocket); encrypt = null; } catch { Dispose(); } }
///<summary>Called when we have received data from the local client.<br>Incoming data will immediately be forwarded to the remote host.</br></summary> ///<param name="ar">The result of the asynchronous operation.</param> protected void OnClientReceive(IAsyncResult ar) { try { int Ret = ClientSocket.EndReceive(ar); if (Ret <= 0) { Dispose(); return; } //Logger.WriteSocketToFile(ClientSocket, _buffer, 0, Ret,false); int sent = DestinationSocket.Send(_buffer, 0, Ret, SocketFlags.None); //Logger.WriteSocketToFile(DestinationSocket, _buffer, 0, Ret,true); if (sent > 0) { ClientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(this.OnClientReceive), ClientSocket); return; } } catch { Dispose(); } }
public override void StartForward() { new Task(new Action(() => { const int BUFFER_SIZE_MIN = 1024 * 128; // 缓冲区最小值,128K const int BUFFER_SIZE_MAX = 1024 * 512; // 缓冲区最大值,512K const int BUFFER_SIZE_STEP = 1024 * 128; // 缓冲区自动调整的步长值,128K byte[] buffer = new byte[BUFFER_SIZE_MIN]; byte[] read_bytes = null; byte[] encrypt_bytes = null; try { while (true) { int read_num = -1; try { //Logger.Write(this.GetType().Name + " reading bytes"); read_num = ClientSocket.Receive(buffer, 0, buffer.Length, SocketFlags.None); //Logger.Write(this.GetType().Name + " " + read_num + " bytes read"); } catch (SocketException sex) { if (sex.SocketErrorCode == SocketError.WouldBlock) { System.Threading.Thread.Sleep(100); continue; } else { Dispose(); break; } } catch { Dispose(); break; } Logger.ThreadWrite(this.GetType().Name + " read bytes: " + read_num); if (read_num == 0) { System.Threading.Thread.Sleep(1000); continue; } read_bytes = new byte[read_num]; System.Array.Copy(buffer, 0, read_bytes, 0, read_num); encrypt_bytes = _aes.encryptNet(_key, read_bytes); if (encrypt_bytes == null) { Dispose(); break; // 加密出错,退出 } try { Logger.Write(this.GetType().Name + " sending bytes: " + encrypt_bytes.Length); DestinationSocket.Send(encrypt_bytes, 0, encrypt_bytes.Length, SocketFlags.None); } catch { Dispose(); break; } //outputStream.flush(); if (read_num == buffer.Length && read_num < BUFFER_SIZE_MAX) { // 自动调整缓冲区大小 buffer = new byte[read_num + BUFFER_SIZE_STEP]; // log(this.getName() + " 缓冲区大小自动调整为:" + buffer.length); } else if (read_num < (buffer.Length - BUFFER_SIZE_STEP) && (buffer.Length - BUFFER_SIZE_STEP) >= BUFFER_SIZE_MIN) { buffer = new byte[buffer.Length - BUFFER_SIZE_STEP]; // log(this.getName() + " 缓冲区大小自动调整为:" + +buffer.length); } } } catch (Exception ex) { Dispose(); } finally { buffer = null; read_bytes = null; encrypt_bytes = null; } })).Start(); }
public override void StartForward() { new Task(new Action(() => { byte[] buffer = null; byte[] size_bytes = null; int[] sizes = null; byte[] decrypt_bytes = null; const int BUFFER_SIZE_MAX = 1024 * 768; try { while (true) { buffer = new byte[Encrypt.ENCRYPT_SIZE]; int read_num = -1; try { //var vs = new NetworkStream(ClientSocket); read_num = ClientSocket.Receive(buffer, 0, buffer.Length, SocketFlags.None); //new Queue<byte>().en } catch (SocketException sex) { if (sex.SocketErrorCode == SocketError.WouldBlock) { System.Threading.Thread.Sleep(1000); continue; } else { Dispose(); break; } } catch (Exception ex) { Dispose(); break; } if (read_num != Encrypt.ENCRYPT_SIZE) { Dispose(); break; } size_bytes = _aes.decrypt(_key, buffer); if (size_bytes == null) { Dispose(); break; // 解密出错,退出 } sizes = _aes.getBlockSizes(size_bytes); if (sizes == null || sizes.Length != 2 || sizes[0] > BUFFER_SIZE_MAX) { Dispose(); break; } int size_count = sizes[0] + sizes[1]; buffer = new byte[size_count]; int read_count = 0; while (read_count < size_count) { try { read_num = ClientSocket.Receive(buffer, read_count, size_count - read_count, SocketFlags.None); } catch (SocketException sex) { if (sex.SocketErrorCode == SocketError.WouldBlock) { System.Threading.Thread.Sleep(1000); continue; } else { Dispose(); break; } } catch { Dispose(); break; } if (read_num == 0) { Dispose(); break; } read_count += read_num; } if (read_count != size_count) { Dispose(); break; } if (sizes[1] > 0) { // 如果存在噪音数据 byte[] _buffer = new byte[sizes[0]]; System.Array.Copy(buffer, 0, _buffer, 0, _buffer.Length); decrypt_bytes = _aes.decrypt(_key, _buffer); } else { decrypt_bytes = _aes.decrypt(_key, buffer); } if (decrypt_bytes == null) { Dispose(); break; } try { DestinationSocket.Send(decrypt_bytes, 0, decrypt_bytes.Length, SocketFlags.None); } catch { Dispose(); } } } catch (Exception ex) { Dispose(); } buffer = null; size_bytes = null; sizes = null; decrypt_bytes = null; })).Start(); }
///<summary>Called when we have received data from the local client.<br>Incoming data will immediately be forwarded to the remote host.</br></summary> ///<param name="ar">The result of the asynchronous operation.</param> protected void OnClientReceive(IAsyncResult ar) { try { int Ret = ClientSocket.EndReceive(ar); if (Ret < Encrypt.ENCRYPT_SIZE) { //System.Threading.Thread.Sleep(10); Dispose(); return; } byte[] recv = new byte[Ret]; System.Array.Copy(_buffer, 0, recv, 0, recv.Length); _sendingQueue.AddRange(recv); recv = null; byte[] head = new byte[Encrypt.ENCRYPT_SIZE]; int read = _sendingQueue.PopRange(head, head.Length); byte[] size_bytes = _aes.decrypt(_key, head); if (size_bytes == null) { Dispose(); return; } int[] sizes = _aes.getBlockSizes(size_bytes); if (sizes == null || sizes.Length != 2) { Dispose(); return; } int size_count = sizes[0] + sizes[1]; byte[] buffer = new byte[size_count]; int read_count = _sendingQueue.PopRange(buffer, buffer.Length); int read_num = 0; while (read_count < size_count) { read_num = ClientSocket.Receive(buffer, read_count, size_count - read_count, SocketFlags.None); if (read_num == 0) { break; } read_count += read_num; } if (read_count != size_count) { Dispose(); return; } byte[] decrypt_bytes = null; if (sizes[1] > 0) { // 如果存在噪音数据 byte[] realdataBuffer = new byte[sizes[0]]; System.Array.Copy(buffer, 0, realdataBuffer, 0, realdataBuffer.Length); decrypt_bytes = _aes.decrypt(_key, realdataBuffer); } else { decrypt_bytes = _aes.decrypt(_key, buffer); } //Logger.ThreadWrite("got bytes:\t" + decrypt_bytes.Length); if (decrypt_bytes == null) { Dispose(); return; } //using (var ns = new NetworkStream(DestinationSocket)) //{ // ns.Write(decrypt_bytes, 0, decrypt_bytes.Length); // ns.Flush(); //} int sentCount = 0; while (sentCount < decrypt_bytes.Length) { var toSend = Math.Min(decrypt_bytes.Length - sentCount, 1024); sentCount += DestinationSocket.Send(decrypt_bytes, sentCount, decrypt_bytes.Length - sentCount, SocketFlags.None); } ClientSocket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(this.OnClientReceive), ClientSocket); decrypt_bytes = null; } catch { Dispose(); } }