示例#1
0
文件: conn.go 项目: qwo/abelana-gcp
// DialTimeout acts like Dial but takes timeouts for establishing the
// connection to the server, writing a command and reading a reply.  lv3 AppEngine
func DialTimeout(cx appengine.Context, network, address string, connectTimeout, readTimeout,
	writeTimeout time.Duration) (Conn, error) {
	var c *socket.Conn
	var err error
	if connectTimeout > 0 {
		c, err = socket.DialTimeout(cx, network, address, connectTimeout)
	} else {
		c, err = socket.Dial(cx, network, address)
	}
	if err != nil {
		return nil, err
	}
	return NewConn(c, readTimeout, writeTimeout), nil
}
示例#2
0
func getCreateTunnelConn(context appengine.Context, uid string, ev *event.SocketConnectWithDataEvent) (*tunnelConn, bool, error) {
	created := false
	if c, exist := getTunnelConn(uid, ev.GetHash()); exist {
		if c.Addr == ev.Addr {
			return c, created, nil
		} else {
			closeTunnelConn(uid, ev.GetHash())
		}
	}
	c := &tunnelConn{Net: ev.Net, Addr: ev.Addr}
	conn, err := socket.DialTimeout(context, ev.Net, ev.Addr, time.Duration(ev.Timeout)*time.Second)
	if nil == err {
		c.c = conn
		created = true
		saveTunnelConn(uid, ev.GetHash(), c)
		return c, created, nil
	}
	context.Errorf("[%d]Failed to connect %s for reason:%v", ev.GetHash(), ev.Addr, err)
	return nil, created, err
}