go.dedis.ch/onet/v4@v4.0.0-pre1/network/network.go (about) 1 package network 2 3 // Conn represents any communication between two hosts. 4 type Conn interface { 5 // Send a message through the connection. 6 // obj should be a POINTER to the actual struct to send, or an interface. 7 // It should not be a Golang type. 8 // Returns the number of bytes sent and an error if any. 9 Send(Message) (uint64, error) 10 // Receive any message through the connection. It is a blocking call that 11 // returns either when a message arrived or when Close() has been called, or 12 // when a network error occurred. 13 Receive() (*Envelope, error) 14 // Close will close the connection. Implementations must take care that 15 // Close() makes Receive() returns with an error, and any subsequent Send() 16 // will return with an error. Calling Close() on a closed Conn will return 17 // ErrClosed. 18 Close() error 19 20 // Type returns the type of this connection. 21 Type() ConnType 22 // Gives the address of the remote endpoint. 23 Remote() Address 24 // Returns the local address and port. 25 Local() Address 26 // Tx returns how many bytes this connection has written 27 Tx() uint64 28 // Rx returns how many bytes this connection has read 29 Rx() uint64 30 } 31 32 // Listener is responsible for listening for incoming Conns on a particular 33 // address. It can only accept one type of incoming Conn. 34 type Listener interface { 35 // Listen for incoming connections. 36 // Each time there is an incoming Conn, it calls the given 37 // function in a go routine with the incoming Conn as parameter. 38 // The call is blocking. If this listener is already Listening, Listen 39 // should return an error. 40 Listen(func(Conn)) error 41 // Stop the listening. Implementations must take care of making 42 // Stop() a blocking call. Stop() should return when the Listener really 43 // has stopped listening, i.e. the call to Listen has returned. Calling twice 44 // Stop() should return an error ErrClosed on the second call. 45 Stop() error 46 47 // A complete address including the type this listener is listening 48 // to. 49 Address() Address 50 51 // Returns whether this listener is actually listening or not. This 52 // function is mainly useful for tests where we need to make sure the 53 // listening routine is started. 54 Listening() bool 55 } 56 57 // Host listens for a specific type of Conn and can Connect to specific types 58 // of Conn. It is used by the Router so the router can manage connections 59 // while being oblivious to which type of connections it's handling. 60 type Host interface { 61 Listener 62 63 Connect(si *ServerIdentity) (Conn, error) 64 }