本文整理汇总了Golang中syscall.SetNonblock函数的典型用法代码示例。如果您正苦于以下问题:Golang SetNonblock函数的具体用法?Golang SetNonblock怎么用?Golang SetNonblock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SetNonblock函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: newPollServer
func newPollServer() (s *pollServer, err error) {
s = new(pollServer)
if s.pr, s.pw, err = os.Pipe(); err != nil {
return nil, err
}
if err = syscall.SetNonblock(int(s.pr.Fd()), true); err != nil {
goto Errno
}
if err = syscall.SetNonblock(int(s.pw.Fd()), true); err != nil {
goto Errno
}
if s.poll, err = newpollster(); err != nil {
goto Error
}
if _, err = s.poll.AddFD(int(s.pr.Fd()), 'r', true); err != nil {
s.poll.Close()
goto Error
}
s.pending = make(map[int]*pollDesc)
go s.Run()
return s, nil
Errno:
err = &os.PathError{
Op: "setnonblock",
Path: s.pr.Name(),
Err: err,
}
Error:
s.pr.Close()
s.pw.Close()
return nil, err
}
示例2: Init
func (sc *selectCtx) Init() error {
sc.rset.Zero()
sc.wset.Zero()
sc.fdmax = -1
// Create and configure the pipe-ends.
b := make([]int, 2)
err := syscall.Pipe(b)
if err != nil {
return err
}
sc.pfdr, sc.pfdw = b[0], b[1]
syscall.CloseOnExec(sc.pfdr)
syscall.CloseOnExec(sc.pfdw)
err = syscall.SetNonblock(sc.pfdr, true)
if err != nil {
syscall.Close(sc.pfdr)
syscall.Close(sc.pfdw)
return err
}
err = syscall.SetNonblock(sc.pfdw, true)
if err != nil {
syscall.Close(sc.pfdr)
syscall.Close(sc.pfdw)
return err
}
// pfdr (read-end of pipe) is set (and remains set forever) on
// rset.
sc.rset.Set(sc.pfdr)
sc.fdmax = sc.pfdr
// allocate dummy selectCtx.{b,b1} buffers.
sc.b = make([]byte, 1)
sc.b1 = make([]byte, 128)
return nil
}
示例3: open
func (t *pty) open(cols, rows int) error {
var winp = new(C.struct_winsize)
winp.ws_col = C.ushort(cols)
winp.ws_row = C.ushort(rows)
winp.ws_xpixel = 0
winp.ws_ypixel = 0
var master, slave C.int
var name = make([]C.char, 40)
if ret := int(C.GoOpenpty(&master, &slave, &name[0], winp)); ret == -1 {
return errors.New("openpty(3) failed")
}
t.master = int(master)
t.slave = int(slave)
t.pty = C.GoString(&name[0])
if err := syscall.SetNonblock(t.master, true); err != nil {
return err
}
if err := syscall.SetNonblock(t.slave, true); err != nil {
return err
}
return nil
}
示例4: newPollServer
func newPollServer() (s *pollServer, err os.Error) {
s = new(pollServer)
s.cr = make(chan *netFD, 1)
s.cw = make(chan *netFD, 1)
if s.pr, s.pw, err = os.Pipe(); err != nil {
return nil, err
}
var e int
if e = syscall.SetNonblock(s.pr.Fd(), true); e != 0 {
goto Errno
}
if e = syscall.SetNonblock(s.pw.Fd(), true); e != 0 {
goto Errno
}
if s.poll, err = newpollster(); err != nil {
goto Error
}
if _, err = s.poll.AddFD(s.pr.Fd(), 'r', true); err != nil {
s.poll.Close()
goto Error
}
s.pending = make(map[int]*netFD)
go s.Run()
return s, nil
Errno:
err = &os.PathError{"setnonblock", s.pr.Name(), os.Errno(e)}
Error:
s.pr.Close()
s.pw.Close()
return nil, err
}
示例5: ProxyNetAccept
func (io *NetIOManager) ProxyNetAccept(serverinfo syscall.Sockaddr) (sa syscall.Sockaddr, err error) {
var clientfd, serverfd int
// accpet mongodb client connection request
clientfd, clientinfo, err := syscall.Accept(io.proxy_server_fd)
if err != nil {
goto ClientError
}
err = syscall.SetNonblock(clientfd, true)
if err != nil {
goto ClientCleanup
}
err = syscall.EpollCtl(io.epoll_fd, syscall.EPOLL_CTL_ADD, clientfd,
&syscall.EpollEvent{Events: syscall.EPOLLIN | syscall.EPOLLOUT |
syscall.EPOLLRDHUP, Fd: int32(clientfd)})
if err != nil {
goto ClientCleanup
}
// establish connection with mongodb server
serverfd, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM,
syscall.IPPROTO_TCP)
if err != nil {
goto ServerError
}
err = syscall.Connect(serverfd, serverinfo)
if err != nil {
goto ServerCleanup
}
err = syscall.SetNonblock(serverfd, true)
if err != nil {
goto ServerCleanup
}
err = syscall.EpollCtl(io.epoll_fd, syscall.EPOLL_CTL_ADD, serverfd,
&syscall.EpollEvent{Events: syscall.EPOLLIN | syscall.EPOLLOUT |
syscall.EPOLLRDHUP, Fd: int32(serverfd)})
if err != nil {
goto ServerCleanup
}
// now proxy server becomes a bridge between client <-> server
add_sock_peer(io, clientfd, clientinfo, serverfd, serverinfo)
return clientinfo, nil
ServerCleanup:
syscall.Close(serverfd)
ServerError:
syscall.EpollCtl(io.epoll_fd, syscall.EPOLL_CTL_DEL, clientfd,
&syscall.EpollEvent{Events: syscall.EPOLLIN | syscall.EPOLLOUT |
syscall.EPOLLRDHUP, Fd: int32(clientfd)})
ClientCleanup:
syscall.Close(clientfd)
ClientError:
return nil, err
}
示例6: BeginWatch
func (p *pin) BeginWatch(edge Edge, callback IRQEvent) error {
p.SetMode(ModeInput)
if err := write([]byte(edge), p.edgePath); err != nil {
return err
}
var event syscall.EpollEvent
event.Events = syscall.EPOLLIN | (syscall.EPOLLET & 0xffffffff) | syscall.EPOLLPRI
fd := int(p.valueFile.Fd())
p.callback = callback
watchEventCallbacks[fd] = p
if err := syscall.SetNonblock(fd, true); err != nil {
return err
}
event.Fd = int32(fd)
if err := syscall.EpollCtl(epollFD, syscall.EPOLL_CTL_ADD, fd, &event); err != nil {
return err
}
return nil
}
示例7: newPollServer
func newPollServer() (s *pollServer, err os.Error) {
s = new(pollServer)
s.cr = make(chan *netFD, 1)
s.cw = make(chan *netFD, 1)
// s.pr and s.pw are indistinguishable.
if s.pr, s.pw, err = selfConnectedTCPSocket(); err != nil {
return nil, err
}
var e int
if e = syscall.SetNonblock(s.pr.Fd(), true); e != 0 {
Errno:
err = &os.PathError{"setnonblock", s.pr.Name(), os.Errno(e)}
Error:
s.pr.Close()
return nil, err
}
if s.poll, err = newpollster(); err != nil {
goto Error
}
if _, err = s.poll.AddFD(s.pr.Fd(), 'r', true); err != nil {
s.poll.Close()
goto Error
}
s.pending = make(map[int]*netFD)
go s.Run()
return s, nil
}
示例8: accept
// Wrapper around the accept system call that marks the returned file
// descriptor as nonblocking and close-on-exec.
func accept(fd int) (int, syscall.Sockaddr, error) {
nfd, sa, err := syscall.Accept4(fd, syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC)
// The accept4 system call was introduced in Linux 2.6.28. If
// we get an ENOSYS error, fall back to using accept.
if err == nil || err != syscall.ENOSYS {
return nfd, sa, err
}
// See ../syscall/exec_unix.go for description of ForkLock.
// It is okay to hold the lock across syscall.Accept
// because we have put fd.sysfd into non-blocking mode.
syscall.ForkLock.RLock()
nfd, sa, err = syscall.Accept(fd)
if err == nil {
syscall.CloseOnExec(nfd)
}
syscall.ForkLock.RUnlock()
if err != nil {
return -1, nil, err
}
if err = syscall.SetNonblock(nfd, true); err != nil {
syscall.Close(nfd)
return -1, nil, err
}
return nfd, sa, nil
}
示例9: Open
// Open opens an asynchronous communications port.
func Open(name string, options ...func(*Term) error) (*Term, error) {
fd, e := syscall.Open(name, syscall.O_NOCTTY|syscall.O_CLOEXEC|syscall.O_NDELAY|syscall.O_RDWR, 0666)
if e != nil {
return nil, &os.PathError{"open", name, e}
}
modules := [2]string{"ptem", "ldterm"}
for _, mod := range modules {
err := unix.IoctlSetInt(fd, C.I_PUSH, int(uintptr(unsafe.Pointer(syscall.StringBytePtr(mod)))))
if err != nil {
return nil, err
}
}
t := Term{name: name, fd: fd}
termios.Tcgetattr(uintptr(t.fd), &t.orig)
if err := termios.Tcgetattr(uintptr(t.fd), &t.orig); err != nil {
return nil, err
}
if err := t.SetOption(options...); err != nil {
return nil, err
}
return &t, syscall.SetNonblock(t.fd, false)
}
示例10: decref
// Remove a reference to this FD and close if we've been asked to do so (and
// there are no references left.
func (fd *netFD) decref() {
fd.sysmu.Lock()
fd.sysref--
// NOTE(rsc): On Unix we check fd.sysref == 0 here before closing,
// but on Windows we have no way to wake up the blocked I/O other
// than closing the socket (or calling Shutdown, which breaks other
// programs that might have a reference to the socket). So there is
// a small race here that we might close fd.sysfd and then some other
// goroutine might start a read of fd.sysfd (having read it before we
// write InvalidHandle to it), which might refer to some other file
// if the specific handle value gets reused. I think handle values on
// Windows are not reused as aggressively as file descriptors on Unix,
// so this might be tolerable.
if fd.closing && fd.sysfd != syscall.InvalidHandle {
// In case the user has set linger, switch to blocking mode so
// the close blocks. As long as this doesn't happen often, we
// can handle the extra OS processes. Otherwise we'll need to
// use the resultsrv for Close too. Sigh.
syscall.SetNonblock(fd.sysfd, false)
closesocket(fd.sysfd)
fd.sysfd = syscall.InvalidHandle
// no need for a finalizer anymore
runtime.SetFinalizer(fd, nil)
}
fd.sysmu.Unlock()
}
示例11: newFD
func newFD(sysfd int) (*FD, error) {
// Set sysfd to non-blocking mode
err := syscall.SetNonblock(sysfd, true)
if err != nil {
debugf("FD %03d: NF: SetNonBlock: %s", sysfd, err.Error())
return nil, err
}
// Check if sysfd is select-able
// NOTE(npat): Is this useful? Can it ever fail?
var rs fdSet
var tv syscall.Timeval
rs.Zero()
rs.Set(sysfd)
_, err = uxSelect(sysfd+1, &rs, nil, nil, &tv)
if err != nil {
debugf("FD %03d: NF: select(2): %s", sysfd, err.Error())
return nil, err
}
// Initialize FD. We don't generate arbitrary ids, instead we
// use sysfd as the fdMap id. In effect, fd.id is alays ==
// fd.sysfd. Remember that sysfd's can be reused; we must be
// carefull not to allow this to mess things up.
fd := &FD{id: sysfd, sysfd: sysfd}
fd.r.cond = sync.NewCond(&fd.r.mu)
fd.w.cond = sync.NewCond(&fd.w.mu)
// Add to fdMap
fdM.AddFD(fd)
debugf("FD %03d: NF: Ok", fd.id)
return fd, nil
}
示例12: sysSocket
// Wrapper around the socket system call that marks the returned file
// descriptor as nonblocking and close-on-exec.
func sysSocket(family, sotype, proto int) (int, error) { // 创建socket套接字
s, err := socketFunc(family, sotype|syscall.SOCK_NONBLOCK|syscall.SOCK_CLOEXEC, proto)
// On Linux the SOCK_NONBLOCK and SOCK_CLOEXEC flags were
// introduced in 2.6.27 kernel and on FreeBSD both flags were
// introduced in 10 kernel. If we get an EINVAL error on Linux
// or EPROTONOSUPPORT error on FreeBSD, fall back to using
// socket without them.
switch err {
case nil:
return s, nil
default:
return -1, os.NewSyscallError("socket", err)
case syscall.EPROTONOSUPPORT, syscall.EINVAL:
}
// See ../syscall/exec_unix.go for description of ForkLock.
syscall.ForkLock.RLock()
s, err = socketFunc(family, sotype, proto)
if err == nil {
syscall.CloseOnExec(s)
}
syscall.ForkLock.RUnlock()
if err != nil {
return -1, os.NewSyscallError("socket", err)
}
if err = syscall.SetNonblock(s, true); err != nil {
closeFunc(s)
return -1, os.NewSyscallError("setnonblock", err)
}
return s, nil
}
示例13: newFD
func newFD(sysfd int) (*FD, error) {
// Set sysfd to non-blocking mode
err := syscall.SetNonblock(sysfd, true)
if err != nil {
debugf("FD xxx: NF: sysfd=%d, err=%v", sysfd, err)
return nil, err
}
// Initialize FD
fd := &FD{sysfd: sysfd}
fd.id = fdM.GetID()
fd.r.cond = sync.NewCond(&fd.r.mu)
fd.w.cond = sync.NewCond(&fd.w.mu)
// Add to Epoll set. We may imediatelly start receiving events
// after this. They will be dropped since the FD is not yet in
// fdMap. It's ok. Nobody is waiting on this FD yet, anyway.
ev := syscall.EpollEvent{
Events: syscall.EPOLLIN |
syscall.EPOLLOUT |
syscall.EPOLLRDHUP |
(syscall.EPOLLET & 0xffffffff),
Fd: int32(fd.id)}
err = syscall.EpollCtl(epfd, syscall.EPOLL_CTL_ADD, fd.sysfd, &ev)
if err != nil {
debugf("FD %03d: NF: sysfd=%d, err=%v", fd.id, fd.sysfd, err)
return nil, err
}
// Add to fdMap
fdM.AddFD(fd)
debugf("FD %03d: NF: sysfd=%d", fd.id, fd.sysfd)
return fd, nil
}
示例14: setBaudRate
func (c *Connection) setBaudRate(rate uint32) error {
// Create the term IO settings structure
term := syscall.Termios{
Iflag: syscall.IGNPAR,
Cflag: syscall.CS8 | syscall.CREAD | syscall.CLOCAL | rate,
Cc: [32]uint8{syscall.VMIN: 0, syscall.VTIME: getVtime(connectionTimeout)},
Ispeed: rate,
Ospeed: rate,
}
// Make the IOCTL system call to configure the term
if _, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(c.file.Fd()),
uintptr(syscall.TCSETS),
uintptr(unsafe.Pointer(&term)),
); errno != 0 {
// TODO: include errno in this
return errors.New("Encountered error doing IOCTL syscall")
}
if err := syscall.SetNonblock(int(c.file.Fd()), false); err != nil {
return err
}
return nil
}
示例15: newFakeVTYServer
func newFakeVTYServer() (*fakeVTYServer, error) {
fds, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
if err != nil {
return nil, err
}
vtyClientFile := os.NewFile(uintptr(fds[1]), "vty-client")
vtyClientConn, err := net.FileConn(vtyClientFile)
if err != nil {
return nil, err
}
vtyServerFile := os.NewFile(uintptr(fds[0]), "vty-server")
vtyServerConn, err := net.FileConn(vtyServerFile)
if err != nil {
return nil, err
}
syscall.SetNonblock(fds[0], false)
vs := &fakeVTYServer{
clientConn: vtyClientConn,
serverConn: vtyServerConn,
clientFile: vtyClientFile,
serverFile: vtyServerFile,
send: make(chan []byte),
received: make(chan []byte),
}
go vs.read()
go vs.write()
return vs, nil
}