2015-05-20 23:57:19 +02:00
|
|
|
package transport
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
Header map[string]string
|
|
|
|
Body []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type Socket interface {
|
2015-05-21 22:08:19 +02:00
|
|
|
Recv(*Message) error
|
|
|
|
Send(*Message) error
|
|
|
|
Close() error
|
2015-05-20 23:57:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Client interface {
|
2015-06-01 19:55:27 +02:00
|
|
|
Recv(*Message) error
|
|
|
|
Send(*Message) error
|
2015-05-20 23:57:19 +02:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2015-05-21 22:08:19 +02:00
|
|
|
type Listener interface {
|
2015-05-20 23:57:19 +02:00
|
|
|
Addr() string
|
|
|
|
Close() error
|
2015-05-21 22:08:19 +02:00
|
|
|
Accept(func(Socket)) error
|
2015-05-20 23:57:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Transport interface {
|
2015-06-01 19:55:27 +02:00
|
|
|
Dial(addr string, opts ...DialOption) (Client, error)
|
2015-05-21 22:08:19 +02:00
|
|
|
Listen(addr string) (Listener, error)
|
2015-05-20 23:57:19 +02:00
|
|
|
}
|
|
|
|
|
2015-05-23 21:04:16 +02:00
|
|
|
type options struct{}
|
|
|
|
|
2015-06-01 19:55:27 +02:00
|
|
|
type dialOptions struct {
|
|
|
|
stream bool
|
|
|
|
}
|
|
|
|
|
2015-05-23 21:04:16 +02:00
|
|
|
type Option func(*options)
|
|
|
|
|
2015-06-01 19:55:27 +02:00
|
|
|
type DialOption func(*dialOptions)
|
|
|
|
|
2015-05-20 23:57:19 +02:00
|
|
|
var (
|
2015-05-23 21:04:16 +02:00
|
|
|
DefaultTransport Transport = newHttpTransport([]string{})
|
2015-05-20 23:57:19 +02:00
|
|
|
)
|
|
|
|
|
2015-06-01 19:55:27 +02:00
|
|
|
func WithStream() DialOption {
|
|
|
|
return func(o *dialOptions) {
|
|
|
|
o.stream = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 21:04:16 +02:00
|
|
|
func NewTransport(addrs []string, opt ...Option) Transport {
|
|
|
|
return newHttpTransport(addrs, opt...)
|
|
|
|
}
|
|
|
|
|
2015-06-01 19:55:27 +02:00
|
|
|
func Dial(addr string, opts ...DialOption) (Client, error) {
|
|
|
|
return DefaultTransport.Dial(addr, opts...)
|
2015-05-20 23:57:19 +02:00
|
|
|
}
|
|
|
|
|
2015-05-21 22:08:19 +02:00
|
|
|
func Listen(addr string) (Listener, error) {
|
|
|
|
return DefaultTransport.Listen(addr)
|
2015-05-20 23:57:19 +02:00
|
|
|
}
|