Skip to content

Commit

Permalink
修改缓冲区大小
Browse files Browse the repository at this point in the history
  • Loading branch information
tkzcfc committed Jul 25, 2024
1 parent 8d4f784 commit 7f41229
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 11 deletions.
8 changes: 1 addition & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion np_base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ base64 = "0.22"
brotli = "6.0"
crypto_secretbox = "0.1.1"
hex-literal = "0.4.1"
xxtea = "0.2.0"
socket2 = "0.5"

2 changes: 1 addition & 1 deletion np_base/src/net/tcp_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async fn poll_read<S>(
where
S: AsyncRead + AsyncWrite + Send + 'static,
{
let mut buffer = BytesMut::with_capacity(1024);
let mut buffer = BytesMut::with_capacity(4096);

loop {
if reader.read_buf(&mut buffer).await? == 0 {
Expand Down
6 changes: 5 additions & 1 deletion np_base/src/proxy/outlet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use log::{debug, error, info, trace};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use socket2::{SockRef, TcpKeepalive};
use tokio::io::{AsyncReadExt, AsyncWriteExt, WriteHalf};
use tokio::net::{TcpStream, UdpSocket};
use tokio::select;
Expand Down Expand Up @@ -215,6 +216,9 @@ async fn tcp_connect(
client_map: Arc<Mutex<HashMap<u32, ClientType>>>,
) -> anyhow::Result<TcpWriter> {
let stream = TcpStream::connect(&addr).await?;
let ka = TcpKeepalive::new().with_time(Duration::from_secs(30));
let sf = SockRef::from(&stream);
sf.set_tcp_keepalive(&ka)?;
let (mut reader, writer) = tokio::io::split(stream);

tokio::spawn(async move {
Expand Down Expand Up @@ -275,7 +279,7 @@ async fn udp_connect(
let last_active_time_1 = last_active_time.clone();
let recv_task = async {
let write_timeout = Duration::from_secs(1);
let mut buffer = vec![0u8; 65536];
let mut buffer = vec![0u8; 65535];
loop {
match socket.recv_buf(&mut buffer).await {
Ok(size) => {
Expand Down
2 changes: 1 addition & 1 deletion np_client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct Client {
pub async fn run(opts: &Opts) -> anyhow::Result<()> {
info!("Start connecting to server {}", opts.server);
let stream = TcpStream::connect(&opts.server).await?;
let ka = TcpKeepalive::new().with_time(std::time::Duration::from_secs(30));
let ka = TcpKeepalive::new().with_time(Duration::from_secs(30));
let sf = SockRef::from(&stream);
sf.set_tcp_keepalive(&ka)?;
info!("Successful connection with server {}", opts.server);
Expand Down

0 comments on commit 7f41229

Please sign in to comment.