Skip to content

Commit

Permalink
fix: unreached udp recv
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmlm committed Nov 11, 2020
1 parent 952915e commit 546f4c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/proxy/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
//! Commiunication with client-end.
//!

use crate::{hdr, util, DEFAULT_REQ_ID};
use async_std::net::SocketAddr;
use crate::{hdr, util, DEFAULT_REQ_ID, RECV_TO_SECS};
use async_std::{future, net::SocketAddr};
use myutil::{err::*, *};
use std::time::Duration;
use tide::{Body, Error, Request};

macro_rules! err {
Expand Down Expand Up @@ -44,10 +45,11 @@ macro_rules! gen_hdr {
.map_err(|e| err!(@e))?;

let mut buf = vec![0; 128 * 1024];
mysock
.recv(&mut buf)
future::timeout(Duration::from_secs(RECV_TO_SECS), mysock.recv(&mut buf))
.await
.c(d!())
.map_err(|e| err!(@e))?
.c(d!())
.map(|siz| {
unsafe {
buf.set_len(siz);
Expand Down
14 changes: 13 additions & 1 deletion src/proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod http;
mod util;

use async_std::{
future,
net::{SocketAddr, UdpSocket},
sync::Arc,
task,
Expand All @@ -25,10 +26,14 @@ use std::{
os::unix::io::{IntoRawFd, RawFd},
sync::mpsc::channel,
thread,
time::Duration,
};
use ttserver_def::*;
use ttutils::zlib;

// recv timeout in seconds
const RECV_TO_SECS: u64 = 12;

lazy_static! {
static ref CFG: &'static cfg::Cfg = pnk!(cfg::register_cfg(None));
/// 与客户端交互
Expand Down Expand Up @@ -198,7 +203,14 @@ async fn serv_it_udp(
.or_else(|e| send_err!(@DEFAULT_REQ_ID, e, peeraddr))?;

let mut buf = vec![0; 64 * 1024];
match mysock.recv(&mut buf).await.c(d!()) {
match future::timeout(
Duration::from_secs(RECV_TO_SECS),
mysock.recv(&mut buf),
)
.await
.c(d!())?
.c(d!())
{
Ok(siz) => SOCK
.send_to(&buf[..siz], peeraddr)
.await
Expand Down

0 comments on commit 546f4c5

Please sign in to comment.