forked from fiorix/go-smpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
70 lines (58 loc) · 1.3 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
// Copyright 2015 go-smpp authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package smpptest
import (
"bufio"
"bytes"
"io"
"net"
"github.com/Zensey/go-smpp/smpp/pdu"
)
// Conn implements a server side connection.
type Conn interface {
// Write serializes the given PDU and writes to the connection.
Write(p pdu.Body) error
// Close terminates the connection.
Close() error
// RemoteAddr returns the peer address.
RemoteAddr() net.Addr
}
// conn provides the basics of an SMPP connection.
type conn struct {
rwc net.Conn
r *bufio.Reader
w *bufio.Writer
}
func newConn(c net.Conn) *conn {
return &conn{
rwc: c,
r: bufio.NewReader(c),
w: bufio.NewWriter(c),
}
}
// RemoteAddr implements the Conn interface.
func (c *conn) RemoteAddr() net.Addr {
return c.rwc.RemoteAddr()
}
// Read reads PDU off the wire.
func (c *conn) Read() (pdu.Body, error) {
return pdu.Decode(c.r)
}
// Write implements the Conn interface.
func (c *conn) Write(p pdu.Body) error {
var b bytes.Buffer
err := p.SerializeTo(&b)
if err != nil {
return err
}
_, err = io.Copy(c.w, &b)
if err != nil {
return err
}
return c.w.Flush()
}
// Close implements the Conn interface.
func (c *conn) Close() error {
return c.rwc.Close()
}