示例#1
0
func HandleEvent(tags *event.EventHeaderTags, ev event.Event, ctx appengine.Context, sender EventSendService) error {
	res, err := handleRecvEvent(tags, ev, ctx)
	if nil != err {
		ctx.Errorf("Failed to handle event[%d:%d] for reason:%v", ev.GetType(), ev.GetVersion(), err)
		return err
	}
	if nil == res {
		var empty bytes.Buffer
		sender.Send(&empty)
		return nil
	}
	res.SetHash(ev.GetHash())
	compressType := Cfg.CompressType
	if httpres, ok := res.(*event.HTTPResponseEvent); ok {
		v := httpres.GetHeader("Content-Type")
		if len(v) > 0 && Cfg.CompressType != event.COMPRESSOR_NONE {
			if isContentTypeInCompressFilter(v) {
				compressType = event.COMPRESSOR_NONE
			}
		}
	}
	x := new(event.CompressEvent)
	x.SetHash(ev.GetHash())
	x.CompressType = compressType
	x.Ev = res
	y := new(event.EncryptEvent)
	y.SetHash(ev.GetHash())
	y.EncryptType = Cfg.EncryptType
	y.Ev = x
	var buf bytes.Buffer
	tags.Encode(&buf)
	event.EncodeEvent(&buf, y)
	sender.Send(&buf)
	return nil
}
示例#2
0
文件: c4s.go 项目: juju2013/gsnova
func rosck_write_routine(user string, index int) {
	send_ev := send_evs[user][index]
	for {
		select {
		case ev := <-send_ev:
			conns, ok := rsock_conns[user]
			if !ok {
				continue
			}
			if nil == ev {
				return
			}
			var buf bytes.Buffer
			event.EncodeEvent(&buf, ev)
			length := uint32(buf.Len())
			conn := conns[int(ev.GetHash())%len(conns)]
			er := binary.Write(conn, binary.BigEndian, &length)
			if nil != er {
				log.Printf("write len error %v\n", er)
			}
			//log.Printf("Write Event length is %d\n", length)
			_, err := conn.Write(buf.Bytes())
			if nil != err {

			}
		}
	}
}
示例#3
0
文件: c4s.go 项目: juju2013/gsnova
func InvokeCallback(w http.ResponseWriter, req *http.Request) {
	body, err := ioutil.ReadAll(req.Body)
	if nil != err {
	}
	user := req.Header.Get("UserToken")
	index := req.Header.Get("FetcherIndex")
	if len(index) == 0 {
		index = "0:1"
	}
	var idx, poolSize uint32
	fmt.Sscanf(index, "%d:%d", &idx, &poolSize)
	send_ev_array, exist := send_evs[user]
	if !exist {
		send_evs[user] = make([]chan event.Event, poolSize)
		for i, _ := range send_evs[user] {
			send_evs[user][i] = make(chan event.Event, 1024)
		}
	}
	recv_ev, ok := recv_evs[user]
	if !ok {
		recv_ev = make(chan event.Event, 4096)
		recv_evs[user] = recv_ev
		go recvEventLoop(user)
	}
	//log.Printf("#####%d\n",len(body))
	buf := bytes.NewBuffer(body)
	for {
		if buf.Len() == 0 {
			break
		}
		err, ev := event.DecodeEvent(buf)
		if nil != err {
			log.Printf("Decode event  error:%v", err)
			break
		}
		recv_ev <- ev
		//recv_evs[]
		//getProxySession(user, ev.GetHash()).recv_evs <- ev
	}
	var send_content bytes.Buffer
	start := time.Now().UnixNano()
	tick := time.NewTicker(10 * time.Millisecond)
	expectedData := true
	for expectedData {
		select {
		case <-tick.C:
			if time.Now().UnixNano()-start >= 100000*1000 {
				expectedData = false
				break
			}
			continue
		case ev := <-send_ev_array[idx]:
			if nil == ev {
				expectedData = false
				break
			}
			if sessionExist(user, ev.GetHash()) {
				event.EncodeEvent(&send_content, ev)
			}
			if send_content.Len() >= 16*1024 {
				expectedData = false
				break
			}
		}
	}
	//strconv.Itoa()
	w.Header().Set("Content-Length", strconv.Itoa(send_content.Len()))
	w.Write(send_content.Bytes())
}
示例#4
0
文件: gae.go 项目: juju2013/gsnova
func (gae *GAEHttpConnection) requestEvent(client *http.Client, conn *SessionConnection, ev event.Event) (err error, res event.Event) {
	auth := gae.gaeAuth
	if nil == auth {
		auth = gae.manager.auths.Select().(*GAEAuth)
	}
	domain := auth.appid + ".appspot.com"
	if strings.Contains(auth.appid, ".") {
		domain = auth.appid
	}
	addr, _ := getLocalHostMapping(domain)
	scheme := MODE_HTTP
	if strings.EqualFold(MODE_HTTPS, gae_cfg.ConnectionMode) {
		scheme = MODE_HTTPS
	}
	var buf bytes.Buffer
	var tags event.EventHeaderTags
	tags.Token = auth.token
	tags.Encode(&buf)
	if ev.GetType() == event.HTTP_REQUEST_EVENT_TYPE {
		var compress event.CompressEvent
		compress.SetHash(ev.GetHash())
		compress.Ev = ev
		compress.CompressType = gae_cfg.Compressor
		var encrypt event.EncryptEvent
		encrypt.SetHash(ev.GetHash())
		encrypt.EncryptType = gae_cfg.Encrypter
		encrypt.Ev = &compress
		event.EncodeEvent(&buf, &encrypt)
	} else {
		var encrypt event.EncryptEvent
		encrypt.SetHash(ev.GetHash())
		encrypt.EncryptType = gae_cfg.Encrypter
		encrypt.Ev = ev
		event.EncodeEvent(&buf, &encrypt)
	}
	req := &http.Request{
		Method:        "POST",
		URL:           &url.URL{Scheme: scheme, Host: addr, Path: "/invoke"},
		ProtoMajor:    1,
		ProtoMinor:    1,
		Host:          addr,
		Header:        make(http.Header),
		Body:          ioutil.NopCloser(&buf),
		ContentLength: int64(buf.Len()),
	}

	if len(gae_cfg.UA) > 0 {
		req.Header.Set("User-Agent", gae_cfg.UA)
	}
	req.Close = false
	req.Header.Set("Connection", "keep-alive")
	req.Header.Set("Content-Type", "image/jpeg")

	var response *http.Response
	response, err = gaeHttpClient.Do(req)
	if nil != err {
		log.Printf("Failed to request data from GAE:%s for:%s\n", domain, err.Error())
		return err, nil
	} else {
		if response.StatusCode != 200 {
			log.Printf("Session[%d]Invalid response:%d\n", ev.GetHash(), response.StatusCode)
			return fmt.Errorf("Invalid response:%d", response.StatusCode), nil
		} else {
			var buf bytes.Buffer
			n, err := io.Copy(&buf, response.Body)
			if int64(n) < response.ContentLength {
				return fmt.Errorf("No sufficient space in body."), nil
			}
			if nil != err {
				return err, nil
			}
			response.Body.Close()
			if !tags.Decode(&buf) {
				return fmt.Errorf("Failed to decode event tag"), nil
			}
			err, res = event.DecodeEvent(&buf)
			if nil == err {
				res = event.ExtractEvent(res)
			}
			return err, res
		}
	}

	return nil, nil
}
示例#5
0
文件: c4_ws.go 项目: juju2013/gsnova
func wsC4Routine(server string, index int, ch chan event.Event) error {
	var ws net.Conn
	u, err := url.Parse(server)
	if nil != err {
		return err
	}
	checkWsConnection := func() bool {
		if nil == ws {
			if len(u.Path) == 0 {
				u.Path = "/"
			}
			request := fmt.Sprintf("GET / HTTP/1.1\r\nUpgrade: WebSocket\r\nHost: %s\r\nConnection: Upgrade\r\nConnectionIndex:%d\r\nUserToken:%s\r\nKeep-Alive: %d\r\n\r\n", u.Host, index, userToken, c4_cfg.WSConnKeepAlive)
			addr := u.Host
			if !strings.Contains(u.Host, ":") {
				addr = net.JoinHostPort(u.Host, "80")
			}
			c, err := net.Dial("tcp", addr)
			if nil != err {
				log.Printf("[ERROR]Failed to connect websocket server:%v\n", err)
				return false
			}
			c.Write([]byte(request))
			response, err := http.ReadResponse(bufio.NewReader(c), new(http.Request))
			//			tmpbuf := new(bytes.Buffer)
			//			if nil != response {
			//				response.Write(tmpbuf)
			//				log.Printf("Recv response:%s\n", tmpbuf.String())
			//			}
			if nil != err || response.StatusCode != 101 {
				log.Printf("[ERROR]Failed to handshake websocket server:%v with response:%v\n", err, response)
				return false
			}
			ws = c
			go wsReadTask(c, ch)
		}
		return true
	}
	checkWsConnection()
	for {
		select {
		case ev := <-ch:
			if nil == ev {
				if nil != ws {
					ws.Close()
					ws = nil
				}
				checkWsConnection()
				log.Printf("Lost websocket connection:%d\n", index)
				continue
			}
			buf := new(bytes.Buffer)
			event.EncodeEvent(buf, ev)
			chunkLen := int32(buf.Len())
			var lenheader bytes.Buffer
			binary.Write(&lenheader, binary.BigEndian, &chunkLen)
			var allbuf bytes.Buffer
			allbuf.Write(lenheader.Bytes())
			allbuf.Write(buf.Bytes())
			data := allbuf.Bytes()
			for {
				for !checkWsConnection() {
					time.Sleep(500 * time.Millisecond)
				}
				n, err := ws.Write(data)
				if nil != err {
					log.Printf("[ERROR]Failed to write websocket server:%v\n", err)
					ws.Close()
					ws = nil
					continue
				}
				if n < len(data) {
					log.Printf("[ERROR]Unsent bytes:%d\n", len(data)-n)
				}
				break
			}
		}
	}
	return nil
}