go.nanomsg.org/mangos/v3@v3.4.3-0.20240217232803-46464076f1f5/protocol/bus/bus.go (about)

     1  // Copyright 2018 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 bus implements the BUS protocol.  In this protocol, participants
    16  // send a message to each of their peers.
    17  package bus
    18  
    19  import (
    20  	"go.nanomsg.org/mangos/v3/protocol"
    21  	"go.nanomsg.org/mangos/v3/protocol/xbus"
    22  )
    23  
    24  type socket struct {
    25  	protocol.Protocol
    26  }
    27  
    28  // Protocol identity information.
    29  const (
    30  	Self     = protocol.ProtoBus
    31  	Peer     = protocol.ProtoBus
    32  	SelfName = "bus"
    33  	PeerName = "bus"
    34  )
    35  
    36  func (s *socket) GetOption(name string) (interface{}, error) {
    37  	switch name {
    38  	case protocol.OptionRaw:
    39  		return false, nil
    40  	}
    41  	return s.Protocol.GetOption(name)
    42  }
    43  
    44  func (s *socket) RecvMsg() (*protocol.Message, error) {
    45  	m, e := s.Protocol.RecvMsg()
    46  	if m != nil {
    47  		// Strip the raw mode header, as we don't use it in cooked mode
    48  		m.Header = m.Header[:0]
    49  	}
    50  	return m, e
    51  }
    52  
    53  func (s *socket) SendMsg(m *protocol.Message) error {
    54  	if len(m.Header) > 0 {
    55  		m.Header = m.Header[:0]
    56  	}
    57  	return s.Protocol.SendMsg(m)
    58  }
    59  
    60  // NewProtocol returns a new protocol implementation.
    61  func NewProtocol() protocol.Protocol {
    62  
    63  	s := &socket{
    64  		Protocol: xbus.NewProtocol(),
    65  	}
    66  	return s
    67  }
    68  
    69  // NewSocket allocates a raw Socket using the BUS protocol.
    70  func NewSocket() (protocol.Socket, error) {
    71  	return protocol.MakeSocket(NewProtocol()), nil
    72  }