/
conn.go
181 lines (162 loc) · 4.29 KB
/
conn.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) 2014, Markover Inc.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
// Source code and contact info at http://github.com/poptip/ftc
package ftc
import (
"encoding/base64"
"errors"
"io"
"sync"
"time"
"code.google.com/p/go.net/websocket"
"github.com/dustin/randbo"
"github.com/golang/glog"
)
const defaultTimeout = 30 * time.Second
// newID returns a pseudo-random, URL-encoded, base64
// string used for connection identifiers.
func newID() string {
buf := make([]byte, 15)
n, err := randbo.New().Read(buf)
if err != nil {
glog.Fatal(err)
}
if n != len(buf) {
glog.Fatal("short read")
}
return base64.URLEncoding.EncodeToString(buf)
}
// Conn represents an FTC connection.
type Conn struct {
c *conn
msgs chan []byte
}
func newPubConn(c *conn) *Conn {
return &Conn{c: c, msgs: make(chan []byte, 10)}
}
func (c *Conn) onMessage(msg []byte) {
select {
case c.msgs <- msg:
glog.Infoln("sent message to msgs chan:", string(msg))
return
case <-time.After(defaultTimeout):
glog.Warningln("onMessage timed out")
}
}
func (c *Conn) Read(p []byte) (int, error) {
// TODO(andybons): if the connection is closed, return io.EOF.
return copy(p, <-c.msgs), nil
}
func (c *Conn) Write(p []byte) (int, error) {
pkt := packet{typ: packetTypeMessage, data: p}
c.c.mu.RLock()
defer c.c.mu.RUnlock()
if c.c.ws != nil {
return len(p), newPacketEncoder(c.c).encode(pkt)
}
return len(p), newPayloadEncoder(c.c).encode([]packet{pkt})
}
// Close closes the connection.
func (c *Conn) Close() error {
var err error
if c.c != nil {
err = c.c.Close()
c.c = nil
}
return err
}
// conn represents an internal FTC connection.
// The publicly available Conn abstracts away the
// underlying protocol by only sending message data
// through Read/Write. If a conn is using a WebSocket
// connection as its underlying transport mechanism,
// then reads and writes go directly to that connection.
// Otherwise (XHR long polling) they are pushed onto
// a buffered channel by a POST to be read later by
// a subsequent GET.
type conn struct {
id string // A unique ID assigned to the conn.
buf chan []byte // Storage buffer for messages.
pubConn *Conn // Public connection that only reads and writes message data.
mu sync.RWMutex // Protects the items below.
ws *websocket.Conn // If upgraded, used to send and receive messages.
closed bool // Whether the connection is closed.
}
// newConn allocates and returns a new FTC connection.
func newConn() *conn {
c := &conn{
id: newID(),
buf: make(chan []byte, 10),
}
c.pubConn = newPubConn(c)
return c
}
// Read copies the next available message to the given
// byte slice. If no message is available, it will block.
func (c *conn) Read(p []byte) (int, error) {
c.mu.RLock()
defer c.mu.RUnlock()
if c.closed {
return 0, errors.New("cannot read on closed connection")
}
if c.ws != nil {
return c.ws.Read(p)
}
select {
case b := <-c.buf:
return copy(p, b), io.EOF
case <-time.After(defaultTimeout):
return 0, errors.New("timeout")
}
}
// Write writes the contents of p as a single message to
// the connection. This call may block if the number of
// outstanding writes exceeds the size of buf.
func (c *conn) Write(p []byte) (int, error) {
glog.Infof("writing %q (upgraded: %t)", p, c.upgraded())
c.mu.RLock()
defer c.mu.RUnlock()
if c.closed {
return 0, errors.New("cannot write on closed connection")
}
if c.ws != nil {
return c.ws.Write(p)
}
select {
case c.buf <- p:
return len(p), nil
case <-time.After(defaultTimeout):
return 0, errors.New("timeout")
}
}
// Close closes the connection.
func (c *conn) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return errors.New("connection is already closed")
}
close(c.buf)
close(c.pubConn.msgs)
if c.ws != nil {
c.ws.Close()
}
c.closed = true
return nil
}
// upgrade assigns the given WebSocket connection to
// the connection.
// TODO(andybons): Flush any messages waiting in buf and close it.
func (c *conn) upgrade(ws *websocket.Conn) {
glog.Infoln("upgrading connection...")
c.mu.Lock()
c.ws = ws
c.mu.Unlock()
}
// upgraded returns true if the connection has been upgraded.
func (c *conn) upgraded() bool {
c.mu.RLock()
defer c.mu.RUnlock()
return c.ws != nil
}