-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
75 lines (68 loc) · 1.44 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package share_clipboard
import (
"bytes"
"fmt"
"github.com/juju/errors"
log "github.com/sirupsen/logrus"
"net"
)
type Server struct {
laddr string
body []byte
}
func NewServer(laddr string) *Server {
return &Server{laddr: laddr}
}
func (s *Server) Serve() error {
lis, err := net.Listen("tcp", s.laddr)
log.Infof("Serve: %s", lis.Addr().String())
if err != nil {
return errors.Trace(err)
}
for {
conn, err := lis.Accept()
if err != nil {
log.Error(errors.Trace(err))
continue
}
log.Debugf("accept: %s -> %s", conn.LocalAddr().String(), conn.RemoteAddr().String())
go func() {
if err := s.handleConn(conn); err != nil {
log.Error(errors.ErrorStack(err))
}
}()
}
}
func (s *Server) handleConn(conn net.Conn) error {
buf := GetBuffer()
defer PutBuffer(buf)
// [ActionType]Body
header := make([]byte, 1)
n, err := conn.Read(header)
if err != nil {
return errors.Trace(err)
}
if n != 1 {
return fmt.Errorf("read type error: %s", string(header))
}
resp := bytes.NewBufferString(StatusOK)
switch action := header[0]; action {
case ActionTypeCopy:
n, err := conn.Read(buf)
if err != nil {
return errors.Trace(err)
}
if n >= defaultBufSize {
return fmt.Errorf("too large body")
}
s.body = buf[:n]
case ActionTypePaste:
resp.Write(s.body)
default:
return fmt.Errorf("error type: %v", action)
}
if _, err := conn.Write(resp.Bytes()); err != nil {
return errors.Trace(err)
}
return nil
}