goadb/wire/sender.go
2015-04-28 14:25:36 -05:00

47 lines
838 B
Go

package wire
import (
"fmt"
"io"
)
// Sender sends messages to the server.
type Sender interface {
SendMessage(msg []byte) error
NewSyncSender() SyncSender
Close() error
}
type realSender struct {
writer io.WriteCloser
}
func NewSender(w io.WriteCloser) Sender {
return &realSender{w}
}
func SendMessageString(s Sender, msg string) error {
return s.SendMessage([]byte(msg))
}
func (s *realSender) SendMessage(msg []byte) error {
if len(msg) > MaxMessageLength {
return fmt.Errorf("message length exceeds maximum: %d", len(msg))
}
lengthAndMsg := fmt.Sprintf("%04x%s", len(msg), msg)
return writeFully(s.writer, []byte(lengthAndMsg))
}
func (s *realSender) NewSyncSender() SyncSender {
return NewSyncSender(s.writer)
}
func (s *realSender) Close() error {
return s.writer.Close()
}
var _ Sender = &realSender{}