@@ -9,8 +9,8 @@ use aes::Aes128;
9
9
use anyhow:: { anyhow, Result } ;
10
10
use bytes:: { Buf , BufMut , BytesMut } ;
11
11
use cfb8:: Encryptor ;
12
- use std:: { net :: SocketAddr , sync:: Arc , time:: Duration } ;
13
- use tokio:: net:: TcpStream ;
12
+ use std:: { sync:: Arc , time:: Duration } ;
13
+ use tokio:: { io , net:: TcpStream } ;
14
14
use tracing:: { debug, error, info} ;
15
15
use uuid:: Uuid ;
16
16
@@ -31,11 +31,10 @@ pub struct Session {
31
31
pub secret : Option < Vec < u8 > > , // Shared secret,
32
32
pub verify_token : [ u8 ; 4 ] ,
33
33
pub cipher : Option < Encryptor < Aes128 > > ,
34
- pub addr : SocketAddr ,
35
34
}
36
35
37
36
impl Session {
38
- pub async fn new ( addr : SocketAddr ) -> Self {
37
+ pub async fn new ( ) -> Self {
39
38
let config = get_config ( ) . await ;
40
39
41
40
Self {
@@ -47,7 +46,6 @@ impl Session {
47
46
secret : None ,
48
47
verify_token : generate_verify_token ( ) ,
49
48
cipher : None ,
50
- addr,
51
49
}
52
50
}
53
51
}
@@ -63,30 +61,15 @@ pub struct MinecraftServer {
63
61
impl MinecraftServer {
64
62
pub async fn new ( stream : TcpStream , keys : Arc < rsa:: RsaPrivateKey > ) -> Result < Self > {
65
63
Ok ( Self {
66
- session : Session :: new ( stream . peer_addr ( ) ? ) . await ,
64
+ session : Session :: new ( ) . await ,
67
65
buffer : BytesMut :: new ( ) ,
68
66
config : get_config ( ) . await ,
69
67
stream,
70
68
keys,
71
69
} )
72
70
}
73
71
74
- pub async fn run ( & mut self ) {
75
- match self . _run ( ) . await {
76
- Ok ( _) => info ! (
77
- "Connection from {:?} closed successfully" ,
78
- self . session. addr
79
- ) ,
80
- Err ( e) => {
81
- let _ = self
82
- . send_disconnect ( self . config . messages . internal_error . clone ( ) )
83
- . await ;
84
- error ! ( "Internal error occurred: {}" , e)
85
- }
86
- }
87
- }
88
-
89
- async fn _run ( & mut self ) -> Result < ( ) > {
72
+ pub async fn run ( & mut self ) -> Result < ( ) > {
90
73
loop {
91
74
self . stream . readable ( ) . await ?;
92
75
let mut temp_buf = vec ! [ 0 ; 1024 ] ;
@@ -100,7 +83,12 @@ impl MinecraftServer {
100
83
self . buffer . put_slice ( & temp_buf[ ..n] ) ;
101
84
self . handle_packet ( ) . await ?;
102
85
}
103
- Err ( _) => { }
86
+ Err ( ref e) if e. kind ( ) == io:: ErrorKind :: WouldBlock => {
87
+ continue ;
88
+ }
89
+ Err ( e) => {
90
+ error ! ( "Read error: {}" , e)
91
+ }
104
92
}
105
93
self . buffer . clear ( ) ;
106
94
}
0 commit comments