当前位置: 首页>>代码示例>>Golang>>正文


Golang Socket.SendBytes方法代码示例

本文整理汇总了Golang中github.com/pebbe/zmq4.Socket.SendBytes方法的典型用法代码示例。如果您正苦于以下问题:Golang Socket.SendBytes方法的具体用法?Golang Socket.SendBytes怎么用?Golang Socket.SendBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/pebbe/zmq4.Socket的用法示例。


在下文中一共展示了Socket.SendBytes方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Send

// Sends marshaled data through 0mq socket.
func (g *Goodbye) Send(socket *zmq.Socket) (err error) {
	frame, err := g.Marshal()
	if err != nil {
		return err
	}

	socType, err := socket.GetType()
	if err != nil {
		return err
	}

	// If we're sending to a ROUTER, we send the routingId first
	if socType == zmq.ROUTER {
		_, err = socket.SendBytes(g.routingId, zmq.SNDMORE)
		if err != nil {
			return err
		}
	}

	// Now send the data frame
	_, err = socket.SendBytes(frame, 0)
	if err != nil {
		return err
	}

	return err
}
开发者ID:hintjens,项目名称:zccp,代码行数:28,代码来源:goodbye.go

示例2: receieve

func receieve(socketrep *zmq.Socket, s Server) {
	var d database
	d.Map = make(map[int]*mydetails)
	go d.statemachinecommit(s)
	for {
		fmt.Println("tcp://127.0.0.1:7001")
		msg, err := socketrep.RecvBytes(0)

		fmt.Println("recived")
		if err != nil {
			fmt.Println(err)
		}
		var req request
		json.Unmarshal(msg, &req)
		fmt.Println("parsing----------------------------------------------------------------------------------------")
		fmt.Println(s.leaderid)
		fmt.Println("parsing")
		fmt.Println(req)
		resp := d.parse(req, s)
		fmt.Println(resp)
		message, _ := json.Marshal(resp)
		socketrep.SendBytes(message, 0)

	}

}
开发者ID:vamsikrishnacs,项目名称:raft-final,代码行数:26,代码来源:kvstore.go

示例3: StartZeromqProtoRepServer

func StartZeromqProtoRepServer(port string) {
	var socket *zmq.Socket
	var err error

	if socket, err = zmq.NewSocket(zmq.REP); err != nil {
		log.Fatal(err)
	}

	if err = socket.Bind("tcp://0.0.0.0:" + port); err != nil {
		log.Fatal(err)
	}

	for {
		request, _ := socket.RecvBytes(0)

		var body pb.Request

		proto.Unmarshal(request, &body)

		response, _ := proto.Marshal(&pb.Response{
			Method:        body.Method,
			PayloadLength: int64(len(body.Payload)),
		})

		socket.SendBytes(response, 0)
	}
}
开发者ID:sguzwf,项目名称:internal-api-benchmark,代码行数:27,代码来源:zeromq-proto-rep.go

示例4: StartZeromqProtobufTest

func StartZeromqProtobufTest(address string, clients int, requestsPerClient int, messageSize int, timer metrics.Timer, requestSize *int) func(wg *sync.WaitGroup) {
	return func(wg *sync.WaitGroup) {
		var socket *zmq.Socket
		var err error
		var request []byte

		if socket, err = zmq.NewSocket(zmq.REQ); err != nil {
			log.Fatal(err)
		}

		defer socket.Close()
		defer wg.Done()

		if err = socket.Connect("tcp://" + address); err != nil {
			log.Fatal(err)
		}

		if request, err = proto.Marshal(&pb.Request{
			Method:  "TEST",
			Payload: strings.Repeat("a", messageSize),
		}); err != nil {
			log.Fatal(err)
		}

		*requestSize = len(request)

		for i := 0; i < requestsPerClient; i++ {
			timer.Time(func() {
				socket.SendBytes(request, 0)

				socket.Recv(0)
			})
		}
	}
}
开发者ID:sguzwf,项目名称:internal-api-benchmark,代码行数:35,代码来源:zeromq-protobuf.go

示例5: sendError

func sendError(socket *zmq.Socket, req *Request, err error) {
	// Response envelope
	resp := &Response{
		Error: &Response_Error{},
	}

	if req != nil {
		resp.UUID = req.UUID
	}

	// If error is a zrpc error
	if zrpcErr, ok := err.(zrpcError); ok {
		resp.StatusCode = proto.Uint32(uint32(zrpcErr.GetStatusCode()))
		resp.Error.Message = proto.String(zrpcErr.GetMessage())
	} else {
		// Default to internal error
		resp.StatusCode = proto.Uint32(uint32(http.StatusInternalServerError))
		resp.Error.Message = proto.String(err.Error())
	}

	// Encode the response
	buf, protoErr := proto.Marshal(resp)
	if protoErr != nil {
		glog.Error(protoErr)
		return
	}

	// Send the response
	if _, err := socket.SendBytes(buf, 0); err != nil {
		glog.Error(err)
	}
}
开发者ID:gust1n,项目名称:zrpc,代码行数:32,代码来源:server.go

示例6: send

func send(socket *zmq.Socket, update keyUpdate) error {
	var err error

	if _, err = socket.Send(update.key, zmq.SNDMORE); err != nil {
		return err
	}
	if _, err = socket.SendBytes(update.payload, 0); err != nil {
		return err
	}
	return nil
}
开发者ID:a-palchikov,项目名称:kron,代码行数:11,代码来源:main.go

示例7: send

func send(requester *zmq.Socket, req *proto.Request) {
	data, err := protobuf.Marshal(req)
	panicOnErr(err)
	requester.SendBytes(data, 0)

	reply, err := requester.RecvBytes(0)
	panicOnErr(err)
	response := new(proto.Response)
	err = protobuf.Unmarshal(reply, response)
	panicOnErr(err)
	fmt.Println("Received ", response.String())
}
开发者ID:emembrives,项目名称:remote,代码行数:12,代码来源:client.go

示例8: Send

// Send sends marshaled data through 0mq socket.
func (b *Binary) Send(socket *zmq.Socket) (err error) {
	frame, err := b.Marshal()
	if err != nil {
		return err
	}

	socType, err := socket.GetType()
	if err != nil {
		return err
	}

	// If we're sending to a ROUTER, we send the routingID first
	if socType == zmq.ROUTER {
		_, err = socket.SendBytes(b.routingID, zmq.SNDMORE)
		if err != nil {
			return err
		}
	}

	// Now send the data frame
	_, err = socket.SendBytes(frame, zmq.SNDMORE)
	if err != nil {
		return err
	}
	// Now send any frame fields, in order
	_, err = socket.SendBytes(b.Address, zmq.SNDMORE)
	_, err = socket.SendBytes(b.Content, 0)

	return err
}
开发者ID:asokoloski,项目名称:zproto,代码行数:31,代码来源:binary.go

示例9: AddToQueue

func AddToQueue(sender *zmq.Socket, data interface{}) {

	s, err := json.Marshal(data)
	if err != nil {
		glog.Error("failed to serialize  object when adding to  queue ", err, "     data = ", data)
		return
	}
	// uncomment this line to see the whole json serialization of data - for debugging purpose
	glog.V(1).Info(string(s))

	_, err = sender.SendBytes(s, zmq.DONTWAIT)

	if err != nil {
		glog.Error("Error sending to ZMQ :", err)
	}
	glog.V(1).Info("Add to queue done")
}
开发者ID:alext234,项目名称:code-grep,代码行数:17,代码来源:mq-backend.go

示例10: Send

// Sends marshaled data through 0mq socket.
func (d *Deliver) Send(socket *zmq.Socket) (err error) {
	frame, err := d.Marshal()
	if err != nil {
		return err
	}

	socType, err := socket.GetType()
	if err != nil {
		return err
	}

	// If we're sending to a ROUTER, we send the routingId first
	if socType == zmq.ROUTER {
		_, err = socket.SendBytes(d.routingId, zmq.SNDMORE)
		if err != nil {
			return err
		}
	}

	// Now send the data frame
	_, err = socket.SendBytes(frame, zmq.SNDMORE)
	if err != nil {
		return err
	}
	// Now send any frame fields, in order
	_, err = socket.SendBytes(d.Content, 0)

	return err
}
开发者ID:hintjens,项目名称:zccp,代码行数:30,代码来源:deliver.go

示例11: socketLoop

func (w *worker) socketLoop(s *zmq.Socket, send <-chan Data, recv chan<- Data, state zmq.State) {
	if recv != nil {
		defer close(recv)
	}

	var (
		sendBuf     Data
		sendPending bool
		recvBuf     Data
		recvPending bool
	)

	for {
		var (
			err error

			sendActive <-chan Data
			recvActive chan<- Data
		)

		if !sendPending {
			sendActive = send
		}

		if recvPending {
			recvActive = recv
		}

		select {
		case <-w.notifier:
			const fullState = zmq.POLLIN | zmq.POLLOUT

			if state&fullState != fullState {
				if state, err = s.GetEvents(); err != nil {
					handleGeneralError(err)
					return
				}
			}

		case <-w.closer:
			return

		case sendBuf, sendPending = <-sendActive:
			if !sendPending {
				send = nil
			}

		case recvActive <- recvBuf:
			recvPending = false
			recvBuf.Bytes = nil
		}

		for {
			loop := false

			if sendPending && state&zmq.POLLOUT != 0 {
				flags := zmq.DONTWAIT

				if sendBuf.More {
					flags |= zmq.SNDMORE
				}

				if _, err = s.SendBytes(sendBuf.Bytes, flags); err == nil {
					sendPending = false
					sendBuf.Bytes = nil
					loop = true
				} else if !handleIOError(err) {
					return
				}

				if state, err = s.GetEvents(); err != nil {
					handleGeneralError(err)
					return
				}
			}

			if !recvPending && state&zmq.POLLIN != 0 {
				if data, err := s.RecvBytes(zmq.DONTWAIT); err == nil {
					if more, err := s.GetRcvmore(); err == nil {
						recvBuf.Bytes = data
						recvBuf.More = more
						recvPending = true
						loop = true
					} else {
						handleGeneralError(err)
						return
					}
				} else if !handleIOError(err) {
					return
				}

				if state, err = s.GetEvents(); err != nil {
					handleGeneralError(err)
					return
				}
			}

			if !loop {
				break
			}
//.........这里部分代码省略.........
开发者ID:ninchat,项目名称:zmq4chan,代码行数:101,代码来源:io.go

示例12: handleOutPort

// handleOutPort handles messages sent from this server
func (s *serverImpl) handleOutPort() {
	//	fmt.Println("handleOutPort: Waiting for message on outbox")
	// initial cache refresh
	s.setAddressOf(make(map[int]string))
	s.refreshPeerCache(BROADCAST)
	peerSocketCache := make(map[int]*zmq.Socket)

	for {
		msg := <-s.outbox

		receivers := list.New()
		// BROADCAST packet or packet for new server
		if _, ok := s.address(msg.Pid); !ok {
			s.refreshPeerCache(msg.Pid)
			// packet is dropped silently
			if _, ok := s.address(msg.Pid); !ok && msg.Pid != BROADCAST {
				fmt.Println("Address of ", msg.Pid, " could not be found")
				continue
			}
		}

		if msg.Pid != BROADCAST {
			receivers.PushBack(msg.Pid)
		} else {
			s.RLock()
			for key, _ := range s.addressOf {
				if key != s.pid {
					receivers.PushBack(key)
				}
			}
			s.RUnlock()
		}
		// change msg.Pid to match this server
		// receiving server will then find correct Pid
		msg.Pid = s.Pid()
		// send message to receivers
		gobbedMessage := EnvelopeToBytes(msg)
		for pid := receivers.Front(); pid != nil; pid = pid.Next() {
			// cache connections to peers
			var requester *zmq.Socket
			var ok bool
			var err error
			pidInt := 0
			if pidInt, ok = pid.Value.(int); ok {
			}

			if requester, ok = peerSocketCache[pidInt]; !ok {
				requester, err = zmq.NewSocket(zmq.PUSH)
				if err != nil {
					fmt.Println("Error in creating request socket. ", err.Error())
					return
				}
				peerSocketCache[pidInt] = requester
				socketStr, _ := s.address(pidInt)
				requester.Connect("tcp://" + socketStr)
				defer requester.Close()
			}

			requester.SendBytes(gobbedMessage, 0)
		}

	}
}
开发者ID:pkhadilkar,项目名称:cluster,代码行数:64,代码来源:comm.go

示例13: pointToPointListener

func pointToPointListener(socket *zmq.Socket, getResponse func(hash []byte) []byte) {
	for {
		req, _ := socket.RecvBytes(0)
		socket.SendBytes(getResponse(req), 0)
	}
}
开发者ID:slunk,项目名称:Distributed-Storage-Systems,代码行数:6,代码来源:communication.go

示例14: main

func main() {
	flag.Parse(nil)

	address_list := flag.Args
	if len(address_list) == 0 {
		fmt.Println("No Addresses submitted")
		fmt.Println(flag.Help())
		return
	}

	var send, recv bool
	skip := false

	var socket *zmq.Socket
	switch *socket_type {
	case "PUSH":
		socket, _ = zmq.NewSocket(zmq.PUSH)
		send = true
	case "PULL":
		socket, _ = zmq.NewSocket(zmq.PULL)
		recv = true
	case "PUB":
		socket, _ = zmq.NewSocket(zmq.PUB)
		send = true
	case "SUB":
		socket, _ = zmq.NewSocket(zmq.SUB)
		recv = true
		if len(*subscriptions) == 0 {
			socket.SetSubscribe("")
		}
		for _, subscription := range *subscriptions {
			socket.SetSubscribe(subscription)
		}
	case "REQ":
		socket, _ = zmq.NewSocket(zmq.REQ)
		send = true
		recv = true
	case "REP":
		socket, _ = zmq.NewSocket(zmq.REP)
		send = true
		recv = true
		skip = true
	}
	defer socket.Close()

	// connect or bind
	if *mode {
		for _, address := range address_list {
			socket.Connect(address)
		}
	} else {
		for _, address := range address_list {
			socket.Bind(address)
		}
	}

	delim := byte('\n')
	if *null {
		fmt.Println("Setting delim to null")
		delim = byte(0x00)
	}

	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)

	for i := 0; i < *number || *number == -1; i++ {
		if send && !skip {
			line, _ := reader.ReadBytes(delim)
			socket.SendBytes([]byte(line), 0)
		}
		if recv {
			data, _ := socket.RecvBytes(0)
			writer.Write(data)
			writer.Flush()
		}
		if skip {
			skip = false
		}
	}

	fmt.Println("finished", *number)
}
开发者ID:jhawk28,项目名称:zmqc,代码行数:82,代码来源:zmqc.go


注:本文中的github.com/pebbe/zmq4.Socket.SendBytes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。