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


Golang Socket.GetEvents方法代码示例

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


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

示例1: Add

// Add registers a socket for sending and/or receiving.  The caller can't
// access the socket directly after this.  The send channel (if any) should be
// closed by the caller.
func (io *IO) Add(s *zmq.Socket, send <-chan Data, recv chan<- Data) (err error) {
	fd, err := s.GetFd()
	if err != nil {
		return
	}

	w := newWorker()

	io.lock.Lock()
	io.workers[int32(fd)] = w
	io.lock.Unlock()

	defer func() {
		if err != nil {
			io.lock.Lock()
			delete(io.workers, int32(fd))
			io.lock.Unlock()
		}
	}()

	e := &syscall.EpollEvent{
		Events: syscall.EPOLLIN | syscall.EPOLLET&0xffffffff,
		Fd:     int32(fd),
	}

	if err = syscall.EpollCtl(io.epollFd, syscall.EPOLL_CTL_ADD, fd, e); err != nil {
		return
	}

	state, err := s.GetEvents()
	if err != nil {
		syscall.EpollCtl(io.epollFd, syscall.EPOLL_CTL_DEL, fd, nil)
		return
	}

	go func() {
		defer s.Close()
		defer syscall.EpollCtl(io.epollFd, syscall.EPOLL_CTL_DEL, fd, nil)

		w.socketLoop(s, send, recv, state)
	}()

	return
}
开发者ID:ninchat,项目名称:zmq4chan,代码行数:47,代码来源:io.go

示例2: 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


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