2015-05-21 00:57:19 +03:00
|
|
|
package transport
|
|
|
|
|
2016-01-04 00:25:03 +03:00
|
|
|
import (
|
|
|
|
"time"
|
2016-01-06 19:25:12 +03:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2016-01-04 00:25:03 +03:00
|
|
|
)
|
|
|
|
|
2015-05-21 00:57:19 +03:00
|
|
|
type Message struct {
|
|
|
|
Header map[string]string
|
|
|
|
Body []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type Socket interface {
|
2015-05-21 23:08:19 +03:00
|
|
|
Recv(*Message) error
|
|
|
|
Send(*Message) error
|
|
|
|
Close() error
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Client interface {
|
2015-06-01 20:55:27 +03:00
|
|
|
Recv(*Message) error
|
|
|
|
Send(*Message) error
|
2015-05-21 00:57:19 +03:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2015-05-21 23:08:19 +03:00
|
|
|
type Listener interface {
|
2015-05-21 00:57:19 +03:00
|
|
|
Addr() string
|
|
|
|
Close() error
|
2015-05-21 23:08:19 +03:00
|
|
|
Accept(func(Socket)) error
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Transport interface {
|
2015-06-01 20:55:27 +03:00
|
|
|
Dial(addr string, opts ...DialOption) (Client, error)
|
2015-05-21 23:08:19 +03:00
|
|
|
Listen(addr string) (Listener, error)
|
2015-12-20 00:56:14 +03:00
|
|
|
String() string
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-12-31 21:14:40 +03:00
|
|
|
type Options struct {
|
2016-01-06 19:25:12 +03:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2015-12-31 21:14:40 +03:00
|
|
|
}
|
2015-05-23 22:04:16 +03:00
|
|
|
|
2015-12-31 21:11:46 +03:00
|
|
|
type DialOptions struct {
|
2016-01-04 00:25:03 +03:00
|
|
|
Stream bool
|
|
|
|
Timeout time.Duration
|
2015-12-31 21:14:40 +03:00
|
|
|
|
2016-01-06 19:25:12 +03:00
|
|
|
// Other options for implementations of the interface
|
|
|
|
// can be stored in a context
|
|
|
|
Context context.Context
|
2015-06-01 20:55:27 +03:00
|
|
|
}
|
|
|
|
|
2015-12-31 21:11:46 +03:00
|
|
|
type Option func(*Options)
|
2015-05-23 22:04:16 +03:00
|
|
|
|
2015-12-31 21:11:46 +03:00
|
|
|
type DialOption func(*DialOptions)
|
2015-06-01 20:55:27 +03:00
|
|
|
|
2015-05-21 00:57:19 +03:00
|
|
|
var (
|
2015-05-23 22:04:16 +03:00
|
|
|
DefaultTransport Transport = newHttpTransport([]string{})
|
2016-01-04 00:25:03 +03:00
|
|
|
|
|
|
|
DefaultDialTimeout = time.Second * 5
|
2015-05-21 00:57:19 +03:00
|
|
|
)
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
func WithStream() DialOption {
|
2015-12-31 21:11:46 +03:00
|
|
|
return func(o *DialOptions) {
|
2015-12-31 21:14:40 +03:00
|
|
|
o.Stream = true
|
2015-06-01 20:55:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 00:25:03 +03:00
|
|
|
func WithTimeout(d time.Duration) DialOption {
|
|
|
|
return func(o *DialOptions) {
|
|
|
|
o.Timeout = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:04:16 +03:00
|
|
|
func NewTransport(addrs []string, opt ...Option) Transport {
|
|
|
|
return newHttpTransport(addrs, opt...)
|
|
|
|
}
|
|
|
|
|
2015-06-01 20:55:27 +03:00
|
|
|
func Dial(addr string, opts ...DialOption) (Client, error) {
|
|
|
|
return DefaultTransport.Dial(addr, opts...)
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
|
|
|
|
2015-05-21 23:08:19 +03:00
|
|
|
func Listen(addr string) (Listener, error) {
|
|
|
|
return DefaultTransport.Listen(addr)
|
2015-05-21 00:57:19 +03:00
|
|
|
}
|
2015-12-20 00:56:14 +03:00
|
|
|
|
|
|
|
func String() string {
|
|
|
|
return DefaultTransport.String()
|
|
|
|
}
|