forked from gocircuit/circuit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
58 lines (49 loc) · 1.03 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
// Copyright 2013 The Go Circuit Project
// Use of this source code is governed by the license for
// The Go Circuit Project, found in the LICENSE file.
//
// Authors:
// 2013 Petar Maymounkov <p@gocircuit.org>
package codec
import (
"io"
"net"
)
type Conn struct {
enc Encoder
dec Decoder
carrier CarrierConn
}
func NewConn(carrier CarrierConn, codec Codec) *Conn {
if carrier == nil {
panic("nil carrier")
}
return &Conn{
enc: codec.NewEncoder(),
dec: codec.NewDecoder(),
carrier: carrier,
}
}
func (c *Conn) String() string {
return c.RemoteAddr().String()
}
func (c *Conn) RemoteAddr() net.Addr {
return c.carrier.RemoteAddr()
}
func (c *Conn) Write(v interface{}) (err error) {
chunk, err := c.enc.Encode(v)
if err != nil {
return err
}
return c.carrier.Write(chunk)
}
func (c *Conn) Read(v interface{}) (err error) {
chunk, err := c.carrier.Read()
if err != nil && err != io.EOF {
return err
}
return c.dec.Decode(chunk, v)
}
func (c *Conn) Close() (err error) {
return c.carrier.Close()
}