示例#1
0
func TunnelConn(context appengine.Context, tags *event.EventHeaderTags, ev *event.SocketConnectWithDataEvent) event.Event {
	close_ev := ganerateCloseEvent(ev.GetHash())
	close_ev.Addr = ev.Addr
	if c, created, err := getCreateTunnelConn(context, tags.UserToken, ev); nil == err {
		if len(ev.Content) > 0 {
			_, err = c.c.Write(ev.Content)
			if nil != err {
				context.Errorf("[%d]Failed totunnel write initial data for reason:%v", ev.GetHash(), err)
				return close_ev
			}
		}
		if created {
			connected_ev := close_ev
			connected_ev.Status = event.TCP_CONN_OPENED
			return connected_ev
		} else {
			return nil
		}
	}
	return close_ev
}
示例#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
}