本文整理匯總了Golang中syscall.Connect函數的典型用法代碼示例。如果您正苦於以下問題:Golang Connect函數的具體用法?Golang Connect怎麽用?Golang Connect使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Connect函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Connect
// Conenct wraps syscall.Connect.
func (sw *Switch) Connect(s syscall.Handle, sa syscall.Sockaddr) (err error) {
so := sw.sockso(s)
if so == nil {
return syscall.Connect(s, sa)
}
sw.fmu.RLock()
f, _ := sw.fltab[FilterConnect]
sw.fmu.RUnlock()
af, err := f.apply(so)
if err != nil {
return err
}
so.Err = syscall.Connect(s, sa)
if err = af.apply(so); err != nil {
return err
}
sw.smu.Lock()
defer sw.smu.Unlock()
if so.Err != nil {
sw.stats.getLocked(so.Cookie).ConnectFailed++
return so.Err
}
sw.stats.getLocked(so.Cookie).Connected++
return nil
}
示例2: socket
// Generic socket creation.
func socket(net string, f, p, t int, la, ra syscall.Sockaddr, toAddr func(syscall.Sockaddr) Addr) (fd *netFD, err os.Error) {
// See ../syscall/exec.go for description of ForkLock.
syscall.ForkLock.RLock()
s, e := syscall.Socket(f, p, t)
if e != 0 {
syscall.ForkLock.RUnlock()
return nil, os.Errno(e)
}
syscall.CloseOnExec(s)
syscall.ForkLock.RUnlock()
// Allow reuse of recently-used addresses.
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
// Allow broadcast.
syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1)
if f == syscall.AF_INET6 {
// using ip, tcp, udp, etc.
// allow both protocols even if the OS default is otherwise.
syscall.SetsockoptInt(s, syscall.IPPROTO_IPV6, syscall.IPV6_V6ONLY, 0)
}
if la != nil {
e = syscall.Bind(s, la)
if e != 0 {
closesocket(s)
return nil, os.Errno(e)
}
}
if ra != nil {
e = syscall.Connect(s, ra)
for e == syscall.EINTR {
e = syscall.Connect(s, ra)
}
if e != 0 {
closesocket(s)
return nil, os.Errno(e)
}
}
sa, _ := syscall.Getsockname(s)
laddr := toAddr(sa)
sa, _ = syscall.Getpeername(s)
raddr := toAddr(sa)
fd, err = newFD(s, f, p, net, laddr, raddr)
if err != nil {
closesocket(s)
return nil, err
}
return fd, nil
}
示例3: connect
func (fd *netFD) connect(la, ra syscall.Sockaddr) error {
fd.wio.Lock()
defer fd.wio.Unlock()
if err := fd.pd.PrepareWrite(); err != nil {
return err
}
for {
err := syscall.Connect(fd.sysfd, ra)
if err == nil || err == syscall.EISCONN {
break
}
// On Solaris we can see EINVAL if the socket has
// already been accepted and closed by the server.
// Treat this as a successful connection--writes to
// the socket will see EOF. For details and a test
// case in C see http://golang.org/issue/6828.
if runtime.GOOS == "solaris" && err == syscall.EINVAL {
break
}
if err != syscall.EINPROGRESS && err != syscall.EALREADY && err != syscall.EINTR {
return err
}
if err = fd.pd.WaitWrite(); err != nil {
return err
}
}
return nil
}
示例4: connect
func (fd *netFD) connect(ra syscall.Sockaddr) (err os.Error) {
e := syscall.Connect(fd.sysfd, ra)
if e != 0 {
return os.Errno(e)
}
return nil
}
示例5: connect
func (fd *netFD) connect(la, ra syscall.Sockaddr) error {
// Do not need to call fd.writeLock here,
// because fd is not yet accessible to user,
// so no concurrent operations are possible.
if err := fd.pd.PrepareWrite(); err != nil {
return err
}
for {
err := syscall.Connect(fd.sysfd, ra)
if err == nil || err == syscall.EISCONN {
break
}
// On Solaris we can see EINVAL if the socket has
// already been accepted and closed by the server.
// Treat this as a successful connection--writes to
// the socket will see EOF. For details and a test
// case in C see http://golang.org/issue/6828.
if runtime.GOOS == "solaris" && err == syscall.EINVAL {
break
}
if err != syscall.EINPROGRESS && err != syscall.EALREADY && err != syscall.EINTR {
return err
}
if err = fd.pd.WaitWrite(); err != nil {
return err
}
}
return nil
}
示例6: connect
func (fd *netFD) connect(la, ra syscall.Sockaddr) error {
// Do not need to call fd.writeLock here,
// because fd is not yet accessible to user,
// so no concurrent operations are possible.
if !canUseConnectEx(fd.net) {
return syscall.Connect(fd.sysfd, ra)
}
// ConnectEx windows API requires an unconnected, previously bound socket.
if la == nil {
switch ra.(type) {
case *syscall.SockaddrInet4:
la = &syscall.SockaddrInet4{}
case *syscall.SockaddrInet6:
la = &syscall.SockaddrInet6{}
default:
panic("unexpected type in connect")
}
if err := syscall.Bind(fd.sysfd, la); err != nil {
return err
}
}
// Call ConnectEx API.
o := &fd.wop
o.sa = ra
_, err := wsrv.ExecIO(o, "ConnectEx", func(o *operation) error {
return syscall.ConnectEx(o.fd.sysfd, o.sa, nil, 0, nil, &o.o)
})
if err != nil {
return err
}
// Refresh socket properties.
return syscall.Setsockopt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_UPDATE_CONNECT_CONTEXT, (*byte)(unsafe.Pointer(&fd.sysfd)), int32(unsafe.Sizeof(fd.sysfd)))
}
示例7: connect
func (fd *netFD) connect(ra syscall.Sockaddr) error {
if !canUseConnectEx(fd.net) {
return syscall.Connect(fd.sysfd, ra)
}
// ConnectEx windows API requires an unconnected, previously bound socket.
var la syscall.Sockaddr
switch ra.(type) {
case *syscall.SockaddrInet4:
la = &syscall.SockaddrInet4{}
case *syscall.SockaddrInet6:
la = &syscall.SockaddrInet6{}
default:
panic("unexpected type in connect")
}
if err := syscall.Bind(fd.sysfd, la); err != nil {
return err
}
// Call ConnectEx API.
var o connectOp
o.Init(fd, 'w')
o.ra = ra
_, err := iosrv.ExecIO(&o, fd.wdeadline.value())
if err != nil {
return err
}
// Refresh socket properties.
return syscall.Setsockopt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_UPDATE_CONNECT_CONTEXT, (*byte)(unsafe.Pointer(&fd.sysfd)), int32(unsafe.Sizeof(fd.sysfd)))
}
示例8: socket
/*
c.sa = new(syscall.SockaddrInet4)
c.sa.Addr = [4]byte{10, 1, 1, 131}
c.sa.Port = port
*/
func (c *Worker) socket() {
begin := time.Now()
s, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
if err != nil {
syscall.Close(s)
return
}
err = syscall.Connect(s, nil) //TODO
if err != nil {
lg(err)
syscall.Close(s)
return
}
_, err = syscall.Write(s, message.content)
if err != nil {
syscall.Close(s)
return
}
_, err = syscall.Read(s, c.data)
if err != nil {
syscall.Close(s)
return
}
syscall.Close(s)
time.Now().Sub(begin)
}
示例9: connect
func (fd *netFD) connect(la, ra syscall.Sockaddr, deadline time.Time) error {
// Do not need to call fd.writeLock here,
// because fd is not yet accessible to user,
// so no concurrent operations are possible.
switch err := syscall.Connect(fd.sysfd, ra); err {
case syscall.EINPROGRESS, syscall.EALREADY, syscall.EINTR:
case nil, syscall.EISCONN:
if !deadline.IsZero() && deadline.Before(time.Now()) {
return errTimeout
}
if err := fd.init(); err != nil {
return err
}
return nil
case syscall.EINVAL:
// On Solaris we can see EINVAL if the socket has
// already been accepted and closed by the server.
// Treat this as a successful connection--writes to
// the socket will see EOF. For details and a test
// case in C see http://golang.org/issue/6828.
if runtime.GOOS == "solaris" {
return nil
}
fallthrough
default:
return err
}
if err := fd.init(); err != nil {
return err
}
if !deadline.IsZero() {
fd.setWriteDeadline(deadline)
defer fd.setWriteDeadline(noDeadline)
}
for {
// Performing multiple connect system calls on a
// non-blocking socket under Unix variants does not
// necessarily result in earlier errors being
// returned. Instead, once runtime-integrated network
// poller tells us that the socket is ready, get the
// SO_ERROR socket option to see if the connection
// succeeded or failed. See issue 7474 for further
// details.
if err := fd.pd.WaitWrite(); err != nil {
return err
}
nerr, err := syscall.GetsockoptInt(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_ERROR)
if err != nil {
return err
}
switch err := syscall.Errno(nerr); err {
case syscall.EINPROGRESS, syscall.EALREADY, syscall.EINTR:
case syscall.Errno(0), syscall.EISCONN:
return nil
default:
return err
}
}
}
示例10: 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
}
示例11: Dial
// Etablie une connexion via socket à l'aide des librairies POSIX (BSD)
func (c *Conn) Dial(port int, addr [4]byte) (err error) {
c.sa = s.SockaddrInet4{Port: port, Addr: addr}
c.sd, err = s.Socket(s.AF_INET, s.SOCK_STREAM, 0)
if err != nil {
return
}
s.Connect(c.sd, &c.sa)
return
}
示例12: bindLookupIP
// bindLookupIP implements the BindToDevice LookupIP case.
// To implement socket device binding, the lower-level syscall APIs are used.
// The sequence of syscalls in this implementation are taken from:
// https://code.google.com/p/go/issues/detail?id=6966
func bindLookupIP(host string, config *DialConfig) (addrs []net.IP, err error) {
// When the input host is an IP address, echo it back
ipAddr := net.ParseIP(host)
if ipAddr != nil {
return []net.IP{ipAddr}, nil
}
socketFd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, 0)
if err != nil {
return nil, ContextError(err)
}
defer syscall.Close(socketFd)
err = config.DeviceBinder.BindToDevice(socketFd)
if err != nil {
return nil, ContextError(fmt.Errorf("BindToDevice failed: %s", err))
}
// config.DnsServerGetter.GetDnsServer must return an IP address
ipAddr = net.ParseIP(config.DnsServerGetter.GetDnsServer())
if ipAddr == nil {
return nil, ContextError(errors.New("invalid IP address"))
}
// TODO: IPv6 support
var ip [4]byte
copy(ip[:], ipAddr.To4())
sockAddr := syscall.SockaddrInet4{Addr: ip, Port: DNS_PORT}
// Note: no timeout or interrupt for this connect, as it's a datagram socket
err = syscall.Connect(socketFd, &sockAddr)
if err != nil {
return nil, ContextError(err)
}
// Convert the syscall socket to a net.Conn, for use in the dns package
file := os.NewFile(uintptr(socketFd), "")
defer file.Close()
conn, err := net.FileConn(file)
if err != nil {
return nil, ContextError(err)
}
// Set DNS query timeouts, using the ConnectTimeout from the overall Dial
if config.ConnectTimeout != 0 {
conn.SetReadDeadline(time.Now().Add(config.ConnectTimeout))
conn.SetWriteDeadline(time.Now().Add(config.ConnectTimeout))
}
// TODO: make conn interruptible?
addrs, _, err = ResolveIP(host, conn)
return
}
示例13: connectSocket
// connectSocket makes the connection to the given IP address port
// for the given socket fd
func (conn *ProtectedConn) connectSocket() error {
sockAddr := syscall.SockaddrInet4{Addr: conn.ip, Port: conn.port}
errCh := make(chan error, 2)
time.AfterFunc(connectTimeOut, func() {
errCh <- errors.New("connect timeout")
})
go func() {
errCh <- syscall.Connect(conn.socketFd, &sockAddr)
}()
err := <-errCh
return err
}
示例14: connect
// this is close to the connect() function inside stdlib/net
func connect(fd int, ra syscall.Sockaddr, deadline time.Time) error {
switch err := syscall.Connect(fd, ra); err {
case syscall.EINPROGRESS, syscall.EALREADY, syscall.EINTR:
case nil, syscall.EISCONN:
if !deadline.IsZero() && deadline.Before(time.Now()) {
return errTimeout
}
return nil
default:
return err
}
poller, err := poll.New(fd)
if err != nil {
return err
}
defer poller.Close()
for {
if err = poller.WaitWrite(deadline); err != nil {
return err
}
// if err := fd.pd.WaitWrite(); err != nil {
// return err
// }
// i'd use the above fd.pd.WaitWrite to poll io correctly, just like net sockets...
// but of course, it uses the damn runtime_* functions that _cannot_ be used by
// non-go-stdlib source... seriously guys, this is not nice.
// we're relegated to using syscall.Select (what nightmare that is) or using
// a simple but totally bogus time-based wait. such garbage.
var nerr int
nerr, err = syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_ERROR)
if err != nil {
return err
}
switch err = syscall.Errno(nerr); err {
case syscall.EINPROGRESS, syscall.EALREADY, syscall.EINTR:
continue
case syscall.Errno(0), syscall.EISCONN:
if !deadline.IsZero() && deadline.Before(time.Now()) {
return errTimeout
}
return nil
default:
return err
}
}
}
示例15: system_dialer
func system_dialer(net string, laddr *net.TCPAddr) (fd int, err error) {
switch {
case laddr.IP.To4() != nil:
var addr [4]byte
copy(addr[:], laddr.IP[len(laddr.IP)-4:len(laddr.IP)])
return dialer_tcp_(tcp4_socket,
func(fd int) error {
return syscall.Connect(fd, &syscall.SockaddrInet4{Port: laddr.Port, Addr: addr})
})
case laddr.IP.To16() != nil:
var addr [16]byte
copy(addr[:], laddr.IP)
return dialer_tcp_(tcp6_socket,
func(fd int) error {
return syscall.Connect(fd, &syscall.SockaddrInet6{Port: laddr.Port, Addr: addr})
})
}
return -1, &E_UnknownProto{net}
}