nanomsg.org/go/mangos/v2@v2.0.9-0.20200203084354-8a092611e461/transport/connipc_posix.go (about)

     1  // +build !windows
     2  
     3  // Copyright 2019 The Mangos Authors
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use file except in compliance with the License.
     7  // You may obtain a copy of the license at
     8  //
     9  //    http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package transport
    18  
    19  import (
    20  	"encoding/binary"
    21  	"io"
    22  	"net"
    23  
    24  	"nanomsg.org/go/mangos/v2"
    25  )
    26  
    27  // NewConnPipeIPC allocates a new Pipe using the IPC exchange protocol.
    28  func NewConnPipeIPC(c net.Conn, proto ProtocolInfo) ConnPipe {
    29  	p := &connipc{
    30  		conn: conn{
    31  			c:       c,
    32  			proto:   proto,
    33  			options: make(map[string]interface{}),
    34  			maxrx:   0,
    35  		},
    36  	}
    37  	p.options[mangos.OptionMaxRecvSize] = 0
    38  	p.options[mangos.OptionLocalAddr] = c.LocalAddr()
    39  	p.options[mangos.OptionRemoteAddr] = c.RemoteAddr()
    40  	return p
    41  }
    42  
    43  func (p *connipc) Send(msg *Message) error {
    44  
    45  	var buff = net.Buffers{}
    46  
    47  	// Serialize the length header
    48  	l := uint64(len(msg.Header) + len(msg.Body))
    49  	lbyte := make([]byte, 9)
    50  	lbyte[0] = 1
    51  	binary.BigEndian.PutUint64(lbyte[1:], l)
    52  
    53  	// Attach the length header along with the actual header and body
    54  	buff = append(buff, lbyte, msg.Header, msg.Body)
    55  
    56  	if _, err := buff.WriteTo(p.c); err != nil {
    57  		return err
    58  	}
    59  	msg.Free()
    60  	return nil
    61  }
    62  
    63  func (p *connipc) Recv() (*Message, error) {
    64  
    65  	var sz int64
    66  	var err error
    67  	var msg *Message
    68  	var one [1]byte
    69  
    70  	if _, err = p.c.Read(one[:]); err != nil {
    71  		return nil, err
    72  	}
    73  	if err = binary.Read(p.c, binary.BigEndian, &sz); err != nil {
    74  		return nil, err
    75  	}
    76  
    77  	// Limit messages to the maximum receive value, if not
    78  	// unlimited.  This avoids a potential denaial of service.
    79  	if sz < 0 || (p.maxrx > 0 && sz > int64(p.maxrx)) {
    80  		return nil, mangos.ErrTooLong
    81  	}
    82  	msg = mangos.NewMessage(int(sz))
    83  	msg.Body = msg.Body[0:sz]
    84  	if _, err = io.ReadFull(p.c, msg.Body); err != nil {
    85  		msg.Free()
    86  		return nil, err
    87  	}
    88  	return msg, nil
    89  }