Hello.
I created a Rust project with this command.
cargo new everlong
and I created src/lib.rs
to write all mod
in the file.
These are my codes of main.rs
and lib.rs
.
lib.rs
mod connection;
mod server;
pub use connection::{Connection, ConnectionError, ReadResult};
pub use server::{Server, ServerResult};
main.rs
extern crate bytes;
extern crate slab;
extern crate rml_rtmp;
use std::collections::{HashSet};
use std::net::{TcpListener, TcpStream};
use std::sync::mpsc::{channel, Receiver, TryRecvError};
use std::thread;
use slab::Slab;
fn main() {
let address = "0.0.0.0:1935";
let listener = TcpListener::bind(&address).unwrap();
let (stream_sender, stream_receiver) = channel();
thread::spawn(|| {handle_connections(stream_receiver)});
println!("Listening for connections on {}", address);
for stream in listener.incoming() {
println!("New connection!");
match stream_sender.send(stream.unwrap()) {
Ok(_) => (),
Err(error) => panic!("Error sending stream to connection handler: {:?}", error),
}
}
}
fn handle_connections(connection_receiver: Receiver<TcpStream>) {
let mut connections = Slab::new();
let mut connection_ids = HashSet::new();
let mut server = Server::new();
loop {
match connection_receiver.try_recv() {
Err(TryRecvError::Disconnected) => panic!("Connection receiver closed"),
Err(TryRecvError::Empty) => (),
Ok(stream) => {
let connection = Connection::new(stream);
let id = connections.insert(connection);
let connection = connections.get_mut(id).unwrap();
connection.connection_id = Some(id);
connection_ids.insert(id);
println!("Connection {} started", id);
}
}
let mut ids_to_clear = Vec::new();
let mut packets_to_write = Vec::new();
for connection_id in &connection_ids {
let connection = connections.get_mut(*connection_id).unwrap();
match connection.read() {
Err(ConnectionError::SocketClosed) => {
println!("Socket closed for id {}", connection_id);
ids_to_clear.push(*connection_id);
},
Err(error) => {
println!("I/O error while reading connection {}: {:?}", connection_id, error);
ids_to_clear.push(*connection_id);
},
Ok(result) => {
match result {
ReadResult::NoBytesReceived => (),
ReadResult::HandshakingInProgress => (),
ReadResult::BytesReceived {buffer, byte_count} => {
let mut server_results = match server.bytes_received(*connection_id, &buffer[..byte_count]) {
Ok(results) => results,
Err(error) => {
println!("Input caused the following server error: {}", error);
ids_to_clear.push(*connection_id);
continue;
},
};
for result in server_results.drain(..) {
match result {
ServerResult::OutboundPacket {target_connection_id, packet} => {
packets_to_write.push((target_connection_id, packet));
},
ServerResult::DisconnectConnection {connection_id: id_to_close} => {
ids_to_clear.push(id_to_close);
}
}
}
}
}
}
}
}
for (connection_id, packet) in packets_to_write.drain(..) {
let connection = connections.get_mut(connection_id).unwrap();
connection.write(packet.bytes);
}
for closed_id in ids_to_clear {
println!("Connection {} closed", closed_id);
connection_ids.remove(&closed_id);
connections.remove(closed_id);
server.notify_connection_closed(closed_id);
}
}
}
But, I cannot compile the files.
Compiling everlong v0.1.0 (/Users/kunosouichirou/Documents/GitHub/Everlong/everlong)
error[E0433]: failed to resolve: use of undeclared type or module `Server`
--> src/main.rs:31:22
|
31 | let mut server = Server::new();
| ^^^^^^ use of undeclared type or module `Server`
error[E0433]: failed to resolve: use of undeclared type or module `Connection`
--> src/main.rs:38:34
|
38 | let connection = Connection::new(stream);
| ^^^^^^^^^^ use of undeclared type or module `Connection`
error[E0433]: failed to resolve: use of undeclared type or module `ConnectionError`
--> src/main.rs:53:21
|
53 | Err(ConnectionError::SocketClosed) => {
| ^^^^^^^^^^^^^^^ use of undeclared type or module `ConnectionError`
error[E0433]: failed to resolve: use of undeclared type or module `ReadResult`
--> src/main.rs:65:25
|
65 | ReadResult::NoBytesReceived => (),
| ^^^^^^^^^^ use of undeclared type or module `ReadResult`
error[E0433]: failed to resolve: use of undeclared type or module `ReadResult`
--> src/main.rs:66:25
|
66 | ReadResult::HandshakingInProgress => (),
| ^^^^^^^^^^ use of undeclared type or module `ReadResult`
error[E0433]: failed to resolve: use of undeclared type or module `ReadResult`
--> src/main.rs:67:25
|
67 | ReadResult::BytesReceived {buffer, byte_count} => {
| ^^^^^^^^^^ use of undeclared type or module `ReadResult`
error[E0433]: failed to resolve: use of undeclared type or module `ServerResult`
--> src/main.rs:79:37
|
79 | ... ServerResult::OutboundPacket {target_connection_id, packet} => {
| ^^^^^^^^^^^^ use of undeclared type or module `ServerResult`
error[E0433]: failed to resolve: use of undeclared type or module `ServerResult`
--> src/main.rs:83:37
|
83 | ... ServerResult::DisconnectConnection {connection_id: id_to_close} => {
| ^^^^^^^^^^^^ use of undeclared type or module `ServerResult`
error[E0609]: no field `connection_id` on type `&mut _`
--> src/main.rs:41:28
|
41 | connection.connection_id = Some(id);
| ^^^^^^^^^^^^^
error: aborting due to 9 previous errors
Some errors have detailed explanations: E0433, E0609.
For more information about an error, try `rustc --explain E0433`.
error: could not compile `everlong`.
To learn more, run the command again with --verbose.
I think my main.rs
does not refer lib.rs
, but I don't know why it does not work.
Help me!