This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
connipc_posix.go
88 lines (73 loc) · 2.03 KB
/
connipc_posix.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
// +build !windows
// Copyright 2018 The Mangos Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mangos
import (
"encoding/binary"
"io"
"net"
)
// NewConnPipeIPC allocates a new Pipe using the IPC exchange protocol.
func NewConnPipeIPC(c net.Conn, sock Socket, props ...interface{}) (Pipe, error) {
p := &connipc{conn: conn{c: c, proto: sock.GetProtocol(), sock: sock}}
if err := p.handshake(props); err != nil {
return nil, err
}
return p, nil
}
func (p *connipc) Send(msg *Message) error {
l := uint64(len(msg.Header) + len(msg.Body))
// one := [1]byte{1}
var err error
// send length header
header := make([]byte, 9)
header[0] = 1
binary.BigEndian.PutUint64(header[1:], l)
if _, err = p.c.Write(header[:]); err != nil {
return err
}
if _, err = p.c.Write(msg.Header); err != nil {
return err
}
// hope this works
if _, err = p.c.Write(msg.Body); err != nil {
return err
}
msg.Free()
return nil
}
func (p *connipc) Recv() (*Message, error) {
var sz int64
var err error
var msg *Message
var one [1]byte
if _, err = p.c.Read(one[:]); err != nil {
return nil, err
}
if err = binary.Read(p.c, binary.BigEndian, &sz); err != nil {
return nil, err
}
// Limit messages to the maximum receive value, if not
// unlimited. This avoids a potential denaial of service.
if sz < 0 || (p.maxrx > 0 && sz > p.maxrx) {
return nil, ErrTooLong
}
msg = NewMessage(int(sz))
msg.Body = msg.Body[0:sz]
if _, err = io.ReadFull(p.c, msg.Body); err != nil {
msg.Free()
return nil, err
}
return msg, nil
}