2015-04-11 21:45:29 +00:00
|
|
|
package wire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-07-12 06:18:58 +00:00
|
|
|
|
2020-07-13 06:18:10 +00:00
|
|
|
"goadb/internal/errors"
|
2015-04-11 21:45:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Sender sends messages to the server.
|
|
|
|
type Sender interface {
|
|
|
|
SendMessage(msg []byte) error
|
2015-04-24 13:20:47 +00:00
|
|
|
|
2015-04-12 20:34:20 +00:00
|
|
|
NewSyncSender() SyncSender
|
2015-04-24 13:20:47 +00:00
|
|
|
|
|
|
|
Close() error
|
2015-04-11 21:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type realSender struct {
|
2015-04-24 13:20:47 +00:00
|
|
|
writer io.WriteCloser
|
2015-04-11 21:45:29 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 13:20:47 +00:00
|
|
|
func NewSender(w io.WriteCloser) Sender {
|
2015-04-11 21:45:29 +00:00
|
|
|
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 {
|
2016-05-22 17:49:32 +00:00
|
|
|
return errors.AssertionErrorf("message length exceeds maximum: %d", len(msg))
|
2015-04-11 21:45:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lengthAndMsg := fmt.Sprintf("%04x%s", len(msg), msg)
|
|
|
|
return writeFully(s.writer, []byte(lengthAndMsg))
|
|
|
|
}
|
|
|
|
|
2015-04-12 20:34:20 +00:00
|
|
|
func (s *realSender) NewSyncSender() SyncSender {
|
|
|
|
return NewSyncSender(s.writer)
|
2015-04-11 21:45:29 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 13:20:47 +00:00
|
|
|
func (s *realSender) Close() error {
|
2016-05-22 17:49:32 +00:00
|
|
|
return errors.WrapErrorf(s.writer.Close(), errors.NetworkError, "error closing sender")
|
2015-04-24 13:20:47 +00:00
|
|
|
}
|
|
|
|
|
2015-04-11 21:45:29 +00:00
|
|
|
var _ Sender = &realSender{}
|