Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
R
railgun-routing-client
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Railgun
railgun-routing-client
Commits
35667ab5
Commit
35667ab5
authored
Jan 06, 2026
by
nanamicat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
smooth
parent
5f37e261
Pipeline
#42432
passed with stages
in 56 seconds
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
34 deletions
+30
-34
src/main.rs
src/main.rs
+3
-6
src/protocol.rs
src/protocol.rs
+0
-1
src/router.rs
src/router.rs
+27
-26
src/settings.rs
src/settings.rs
+0
-1
No files found.
src/main.rs
View file @
35667ab5
...
@@ -47,8 +47,6 @@ async fn main() -> anyhow::Result<()> {
...
@@ -47,8 +47,6 @@ async fn main() -> anyhow::Result<()> {
let
resolver
=
Resolver
::
builder_tokio
()
?
.build
();
let
resolver
=
Resolver
::
builder_tokio
()
?
.build
();
let
mut
hello
=
Hello
{
seq
:
rand
::
random
(),
time
:
0
};
loop
{
loop
{
let
server_addr
=
config
.server
.to_socket_addrs
(
&
resolver
)
.await
?
;
let
server_addr
=
config
.server
.to_socket_addrs
(
&
resolver
)
.await
?
;
...
@@ -74,7 +72,8 @@ async fn main() -> anyhow::Result<()> {
...
@@ -74,7 +72,8 @@ async fn main() -> anyhow::Result<()> {
now
=
timer
.tick
()
=>
{
now
=
timer
.tick
()
=>
{
// to clients
// to clients
hello
.time
=
SystemTime
::
now
()
.duration_since
(
SystemTime
::
UNIX_EPOCH
)
?
.as_millis
()
as
u32
;
let
hello
=
Hello
{
time
:
SystemTime
::
now
()
.duration_since
(
SystemTime
::
UNIX_EPOCH
)
?
.as_millis
()
as
u32
};
let
len
=
bincode
::
encode_into_slice
(
&
hello
,
&
mut
buf
,
bincode
::
config
::
standard
())
?
;
let
len
=
bincode
::
encode_into_slice
(
&
hello
,
&
mut
buf
,
bincode
::
config
::
standard
())
?
;
for
id
in
connections
[
&
config
.id
]
.keys
()
{
for
id
in
connections
[
&
config
.id
]
.keys
()
{
let
router
=
&
routers
[
id
];
let
router
=
&
routers
[
id
];
...
@@ -89,15 +88,13 @@ async fn main() -> anyhow::Result<()> {
...
@@ -89,15 +88,13 @@ async fn main() -> anyhow::Result<()> {
peers
:
if
now
.duration_since
(
start
)
<
TIMEOUT
{
Default
::
default
()
}
else
{
connections
peers
:
if
now
.duration_since
(
start
)
<
TIMEOUT
{
Default
::
default
()
}
else
{
connections
.iter
()
.iter
()
.filter
(|(
_
,
to
)|
to
.contains_key
(
&
config
.id
))
.filter
(|(
_
,
to
)|
to
.contains_key
(
&
config
.id
))
.map
(|(
from
,
_
)|
routers
.get_mut
(
from
)
.unwrap
()
.update
(
now
))
.map
(|(
from
,
_
)|
routers
.get_mut
(
from
)
.unwrap
()
.update
(
now
,
start
))
.collect
()},
.collect
()},
via
:
Default
::
default
(),
via
:
Default
::
default
(),
plan
:
Default
::
default
()
plan
:
Default
::
default
()
};
};
let
len
=
bincode
::
encode_into_slice
(
&
uplink
,
&
mut
buf
,
bincode
::
config
::
standard
())
?
;
let
len
=
bincode
::
encode_into_slice
(
&
uplink
,
&
mut
buf
,
bincode
::
config
::
standard
())
?
;
let
_
=
socket
.send_to
(
&
buf
[
..
len
],
server_addr
)
.await
;
let
_
=
socket
.send_to
(
&
buf
[
..
len
],
server_addr
)
.await
;
hello
.seq
+=
1
;
}
}
}
}
}
}
...
...
src/protocol.rs
View file @
35667ab5
...
@@ -4,7 +4,6 @@ use std::collections::BTreeMap;
...
@@ -4,7 +4,6 @@ use std::collections::BTreeMap;
#[derive(Encode,
Decode)]
#[derive(Encode,
Decode)]
pub
struct
Hello
{
pub
struct
Hello
{
pub
seq
:
u32
,
pub
time
:
u32
,
pub
time
:
u32
,
}
}
...
...
src/router.rs
View file @
35667ab5
use
crate
::
data
::
Router
as
RouterData
;
use
crate
::
data
::
Router
as
RouterData
;
use
crate
::
protocol
::{
Hello
,
PeerQuality
};
use
crate
::
protocol
::{
Hello
,
PeerQuality
};
use
crate
::
settings
::{
INTERVAL
,
Settings
,
TIMEOUT
};
use
crate
::
settings
::{
Settings
,
INTERVAL
,
TIMEOUT
};
use
saturating_cast
::
SaturatingCast
;
use
saturating_cast
::
SaturatingCast
;
use
std
::
collections
::
BTreeMap
;
use
std
::
collections
::
BTreeMap
;
use
std
::
net
::{
IpAddr
,
Ipv4Addr
,
SocketAddr
};
use
std
::
net
::{
IpAddr
,
Ipv4Addr
,
SocketAddr
};
...
@@ -9,12 +9,12 @@ use tokio::time::Instant;
...
@@ -9,12 +9,12 @@ use tokio::time::Instant;
pub
struct
Router
{
pub
struct
Router
{
pub
link_address
:
SocketAddr
,
pub
link_address
:
SocketAddr
,
seq
:
u32
,
delay
:
i32
,
// 平滑后的单向延迟 (包含时钟偏差)
delay
:
i32
,
// 平滑后的单向延迟 (包含时钟偏差)
prev_delay
:
i32
,
// 上一次的传输耗时 (local - remote)
prev_delay
:
i32
,
// 上一次的传输耗时 (local - remote)
jitter
:
i32
,
jitter
:
i32
,
receive
:
u64
,
receive
:
u64
,
time
:
Instant
,
remote_time
:
u32
,
local_time
:
Instant
,
}
}
impl
Router
{
impl
Router
{
...
@@ -33,48 +33,49 @@ impl Router {
...
@@ -33,48 +33,49 @@ impl Router {
pub
fn
new
(
data
:
&
RouterData
,
config
:
&
Settings
)
->
Router
{
pub
fn
new
(
data
:
&
RouterData
,
config
:
&
Settings
)
->
Router
{
Router
{
Router
{
link_address
:
SocketAddr
::
new
(
IpAddr
::
V4
(
Router
::
link_address
(
config
.id
,
data
.id
)),
config
.bind
.port
()),
link_address
:
SocketAddr
::
new
(
IpAddr
::
V4
(
Router
::
link_address
(
config
.id
,
data
.id
)),
config
.bind
.port
()),
seq
:
rand
::
random
(),
remote_time
:
rand
::
random
(),
receive
:
0
,
receive
:
0
,
jitter
:
0
,
jitter
:
0
,
prev_delay
:
0
,
prev_delay
:
0
,
delay
:
0
,
delay
:
0
,
time
:
Instant
::
now
(),
local_
time
:
Instant
::
now
(),
}
}
}
}
pub
fn
on_message
(
&
mut
self
,
data
:
&
Hello
)
{
pub
fn
on_message
(
&
mut
self
,
data
:
&
Hello
)
{
let
delay
=
(
SystemTime
::
now
()
.duration_since
(
SystemTime
::
UNIX_EPOCH
)
.unwrap
()
.as_millis
()
as
u32
)
.wrapping_sub
(
data
.time
)
as
i32
;
let
delta
=
(
data
.time
.wrapping_sub
(
self
.remote_time
)
as
i32
as
f32
/
INTERVAL
.as_millis
()
as
f32
)
.round
()
as
i32
;
self
.delay
+=
(
delay
-
self
.delay
)
/
4
;
let
d
=
(
delay
-
self
.prev_delay
)
.abs
();
self
.jitter
+=
(
d
-
self
.jitter
)
/
4
;
self
.prev_delay
=
delay
;
let
delta
=
data
.seq
.wrapping_sub
(
self
.seq
)
as
i32
;
match
delta
{
match
delta
{
..=-
64
|
64
..
=>
{
-
63
..=-
1
=>
{
self
.receive
=
1
;
self
.receive
|
=
1
<<
(
-
delta
);
self
.seq
=
data
.seq
;
self
.prev_delay
=
delay
;
self
.delay
=
delay
;
}
}
0
=>
return
,
1
..=
63
=>
{
1
..=
63
=>
{
self
.receive
=
(
self
.receive
<<
delta
)
|
1
;
self
.receive
=
(
self
.receive
<<
delta
)
|
1
;
self
.
seq
=
data
.seq
;
self
.
remote_time
=
data
.time
;
self
.time
=
Instant
::
now
();
self
.
local_
time
=
Instant
::
now
();
}
}
-
63
..=-
1
=>
{
_
=>
{
self
.receive
|
=
1
<<
(
-
delta
);
self
.remote_time
=
data
.time
;
return
;
}
}
0
=>
{}
}
}
let
delay
=
(
SystemTime
::
now
()
.duration_since
(
SystemTime
::
UNIX_EPOCH
)
.unwrap
()
.as_millis
()
as
u32
)
.wrapping_sub
(
data
.time
)
as
i32
;
self
.delay
=
delay
;
//+= (delay - self.delay) / 4;
let
d
=
(
delay
-
self
.prev_delay
)
.abs
();
self
.jitter
+=
(
d
-
self
.jitter
)
/
4
;
self
.prev_delay
=
delay
;
}
}
pub
(
crate
)
fn
update
(
&
mut
self
,
now
:
Instant
)
->
PeerQuality
{
pub
(
crate
)
fn
update
(
&
mut
self
,
now
:
Instant
,
start
:
Instant
)
->
PeerQuality
{
let
reliability
=
self
.receive
.count_ones
()
as
u8
;
let
reliability
=
self
.receive
.count_ones
()
as
u8
;
if
reliability
>
0
{
if
reliability
>
0
{
let
duration
=
now
.duration_since
(
self
.time
);
let
max
=
now
.duration_since
(
start
)
.div_duration_f32
(
INTERVAL
)
as
u32
;
let
reliability
=
if
max
<
64
{
(
reliability
as
u32
*
64
/
max
)
as
u8
}
else
{
reliability
};
let
duration
=
now
.duration_since
(
self
.local_time
);
if
duration
>
TIMEOUT
{
if
duration
>
TIMEOUT
{
self
.receive
=
0
;
self
.receive
=
0
;
Default
::
default
()
Default
::
default
()
...
...
src/settings.rs
View file @
35667ab5
...
@@ -59,5 +59,4 @@ impl Endpoint {
...
@@ -59,5 +59,4 @@ impl Endpoint {
pub
const
INTERVAL
:
Duration
=
Duration
::
from_secs
(
1
);
pub
const
INTERVAL
:
Duration
=
Duration
::
from_secs
(
1
);
pub
const
TIMEOUT
:
Duration
=
Duration
::
from_secs
(
60
);
pub
const
TIMEOUT
:
Duration
=
Duration
::
from_secs
(
60
);
pub
const
HISTORY
:
u8
=
100
;
pub
const
ROUTE_PROTOCOL
:
RouteProtocol
=
RouteProtocol
::
Other
(
252
);
pub
const
ROUTE_PROTOCOL
:
RouteProtocol
=
RouteProtocol
::
Other
(
252
);
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment