package hopp import "io" import "net" // import "time" const defaultSizeLimit int64 = 1024 * 1024 // 1 megabyte // Conn is a HOPP connection. type Conn interface { // Close closes the connection. Any blocked operations on the connection // or its transactions will be unblocked and return errors. Close() error // See documentation for [net.Conn] LocalAddr() net.Addr RemoteAddr() net.Addr // OpenTrans opens a transaction. The other party will not know about // this until the first message is sent. OpenTrans() (Trans, error) // AcceptTrans accepts a transaction from the other party. This must // be called in a loop to avoid the connection locking up. AcceptTrans() (Trans, error) // SetSizeLimit sets a limit (in bytes) for how large messages can be. // By default, this limit is 1 megabyte. SetSizeLimit(limit int64) } // Trans is a HOPP transaction. Methods of this interface are not safe for // concurrent use with the exception of the Close and ID methods. The // recommended use case is one goroutine per transaction. type Trans interface { // Close closes the transaction. Any blocked operations will be // unblocked and return errors. This method is safe for concurrent use. Close() error // ID returns the transaction ID. This must not change, and it must be // unique within the connection. This method is safe for concurrent use. ID() int64 // TODO: add methods for setting send and receive deadlines // Send sends a message. This method is not safe for concurrent use. Send(method uint16, data []byte) error // SendWriter sends data written to an [io.Writer]. The writer must be // closed after use. Closing the writer flushes any data that hasn't // been written yet. Any writer previously opened through this function // will be discarded. This method is not safe for concurrent use, and // neither is its result. SendWriter(method uint16) (io.WriteCloser, error) // Receive receives a message. This method is not safe for concurrent // use. Receive() (method uint16, data []byte, err error) // ReceiveReader receives a message as an [io.Reader]. Any reader // previously opened through this function will be discarded. This // method is not safe for concurrent use, and neither is its result. ReceiveReader() (method uint16, data io.Reader, err error) }