Crate ethcore_network [] [src]

Network and general IO module.

Example usage for craeting a network service and adding an IO handler:

extern crate ethcore_network as net;
use net::*;
use std::sync::Arc;

struct MyHandler;

impl NetworkProtocolHandler for MyHandler {
    fn initialize(&self, io: &NetworkContext) {
        io.register_timer(0, 1000);
    }

    fn read(&self, io: &NetworkContext, peer: &PeerId, packet_id: u8, data: &[u8]) {
        println!("Received {} ({} bytes) from {}", packet_id, data.len(), peer);
    }

    fn connected(&self, io: &NetworkContext, peer: &PeerId) {
        println!("Connected {}", peer);
    }

    fn disconnected(&self, io: &NetworkContext, peer: &PeerId) {
        println!("Disconnected {}", peer);
    }
}

fn main () {
    let mut service = NetworkService::new(NetworkConfiguration::new_local()).expect("Error creating network service");
    service.start().expect("Error starting service");
    service.register_protocol(Arc::new(MyHandler), *b"myp", 1, &[1u8]);

    // Wait for quit condition
    // ...
    // Drop the service
}

Structs

NetworkConfiguration

Network service configuration

NetworkContext

IO access point. This is passed to all IO handlers and provides an interface to the IO subsystem.

NetworkService

IO Service with networking Message defines a notification data type.

NetworkStats

Network statistics structure

SessionInfo

Shared session information

Enums

AllowIP

IP fiter

NetworkError

Network error.

NetworkIoMessage

Messages used to communitate with the event loop from other threads.

NonReservedPeerMode

Non-reserved peer modes.

Traits

NetworkProtocolHandler

Network IO protocol handler. This needs to be implemented for each new subprotocol. All the handler function are called from within IO event loop. Message is the type for message data.

Functions

is_valid_node_url

Check if node url is valid

Type Definitions

NodeId

Node public key

PacketId

Protocol handler level packet id

PeerId

Local (temporary) peer session ID.

ProtocolId

Protocol / handler id