예제 #1
0
// func NewZmqChanSocket(zmqContext *zmq.Context, zSock *zmq.Socket, txbuf int, rxbuf in) (*ZmqChanSocket, error)
//
// Produce a new ZmqChanSocket. Pass a contact and a zmq.Socket, plus the buffering parameters for the channels.
//
// If this call succeeds (err == nil), then a ZmqChanSocket is returned, and control of your zmq.Socket is passed
// irrevocably to this routine. You should forget you ever had the socket. Do not attempt to use it in any way,
// as its manipulation is now the responsibility of goroutines launched by this routine. Closing the ZmqChanSocket
// will also close your zmq.Socket.
//
// If this routine errors, it is the caller's responsibility to close the zmq.Socket
//
func NewZmqChanSocket(zSock *zmq.Socket, txbuf int, rxbuf int) (ChanSocket, error) {
	s := &ZmqChanSocket{
		zSock: zSock,
	}

	zmqContext, err := zSock.Context()
	if err != nil {
		return nil, err
	}

	if s.zControlIn, s.zControlOut, err = newPair(zmqContext); err != nil {
		return nil, err
	}
	if s.zTxIn, s.zTxOut, err = newPair(zmqContext); err != nil {
		s.zControlIn.Close()
		s.zControlOut.Close()
		return nil, err
	}

	// as we should never read or send to these sockets unless they are ready
	// we set the timeout to 0 so a write or read in any other circumstance
	// returns a immediate error
	s.zSock.SetRcvtimeo(0)
	s.zSock.SetSndtimeo(0)
	s.zTxIn.SetRcvtimeo(0)
	s.zTxIn.SetSndtimeo(0)
	s.zTxOut.SetRcvtimeo(0)
	s.zTxOut.SetSndtimeo(0)
	s.zControlIn.SetRcvtimeo(0)
	s.zControlIn.SetSndtimeo(0)
	s.zControlOut.SetRcvtimeo(0)
	s.zControlOut.SetSndtimeo(0)

	s.initImpl(txbuf, rxbuf)

	barrier()
	s.wg.Add(2)
	go s.runSockets()
	go s.runChannels()
	return s, nil
}