go.nanomsg.org/mangos/v3@v3.4.3-0.20240217232803-46464076f1f5/protocol/pair1/pair1.go (about) 1 // Copyright 2022 The Mangos Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use file except in compliance with the License. 5 // You may obtain a copy of the license at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package pair1 implements the PAIRv1 protocol. This protocol is a 1:1 16 // peering protocol. (Only monogamous mode is supported, but we can connect 17 // to poly peers.) 18 package pair1 19 20 import ( 21 "go.nanomsg.org/mangos/v3/protocol" 22 "go.nanomsg.org/mangos/v3/protocol/xpair1" 23 ) 24 25 // Protocol identity information. 26 const ( 27 Self = protocol.ProtoPair1 28 Peer = protocol.ProtoPair1 29 SelfName = "pair1" 30 PeerName = "pair1" 31 ) 32 33 type socket struct { 34 protocol.Protocol 35 } 36 37 func (s *socket) GetOption(name string) (interface{}, error) { 38 switch name { 39 case protocol.OptionRaw: 40 return false, nil 41 } 42 return s.Protocol.GetOption(name) 43 } 44 45 func (s *socket) SendMsg(m *protocol.Message) error { 46 m.Header = make([]byte, 4) 47 err := s.Protocol.SendMsg(m) 48 if err != nil { 49 m.Header = m.Header[:0] 50 } 51 return err 52 } 53 54 func (s *socket) RecvMsg() (*protocol.Message, error) { 55 m, err := s.Protocol.RecvMsg() 56 if err == nil && m != nil { 57 m.Header = m.Header[:0] 58 } 59 return m, err 60 } 61 62 // NewProtocol returns a new protocol implementation. 63 func NewProtocol() protocol.Protocol { 64 s := &socket{ 65 Protocol: xpair1.NewProtocol(), 66 } 67 return s 68 } 69 70 // NewSocket allocates a raw Socket using the PAIR protocol. 71 func NewSocket() (protocol.Socket, error) { 72 return protocol.MakeSocket(NewProtocol()), nil 73 }