Example #1
0
func (client *_Client) sendLoop(conn io.ReadWriteCloser) {
	stream := NewStream(conn, conn)

	for {

		msg, err := client.pipeline.Sending()

		if err != nil {
			client.closeconn(conn)
			client.E("%s send err \n%s", client.name, err)
			break
		}

		client.V("write message[%s] :%v", msg.Code, msg.Content)

		err = WriteMessage(stream, msg)

		gserrors.Assert(err == nil, "check WriteMessage")

		_, err = stream.Flush()

		if err != nil {
			client.closeconn(conn)
			client.E("%s send err \n%s", client.name, err)
			break
		}
	}
}
Example #2
0
// Get get value by key
func (ring *HashRing) Get(key string) (interface{}, bool) {

	id := ring.hash([]byte(key))

	ring.RLock()
	defer ring.RUnlock()

	if len(ring.circle) == 0 {
		return nil, false
	}

	i := sort.Search(len(ring.circle), func(x int) bool {
		return ring.circle[x] >= id
	})

	if i == len(ring.circle) {
		i = 0
	}

	val, ok := ring.values[ring.circle[i]]

	gserrors.Assert(ok, "values must exists")

	return val, true
}
Example #3
0
//nextChar read next utf-8 character
func (lexer *Lexer) nextChar() error {

	c, err := lexer.reader.ReadByte()

	if err != nil {
		if err == io.EOF {
			lexer.curr = rune(TokenEOF)
			return nil
		}
		return err
	}

	lexer.offset++
	//not ASCII
	if c >= utf8.RuneSelf {
		lexer.buff[0] = c
		lexer.buffPos = 1
		for !utf8.FullRune(lexer.buff[0:lexer.buffPos]) {
			//continue read rest utf8 char bytes
			c, err = lexer.reader.ReadByte()
			if err != nil {
				if err == io.EOF {
					lexer.curr = rune(TokenEOF)
					return nil
				}
				return err
			}

			lexer.buff[lexer.buffPos] = c
			lexer.buffPos++

			gserrors.Assert(
				lexer.buffPos < len(lexer.buff),
				"utf8.UTFMax must << len(lexer.buff)",
			)
		}

		c, width := utf8.DecodeRune(lexer.buff[0:lexer.buffPos])

		if c == utf8.RuneError && width == 1 {
			return lexer.newerror("illegal utf8 character")

		}

		lexer.curr = c
	} else {
		lexer.curr = rune(c)
	}

	lexer.position.Column++

	return nil
}
Example #4
0
func (linker *_Linker) linkExpr(script *ast.Script, expr ast.Expr) {

	gserrors.Assert(expr != nil, "input arg expr can't be nil")

	linker.startLinkNode(expr)

	switch expr.(type) {
	case *ast.ArgsTable:
		linker.linkArgsTable(script, expr.(*ast.ArgsTable))
	case *ast.NamedArg:
		linker.linkNameArg(script, expr.(*ast.NamedArg))
	case *ast.ConstantRef:
		linker.linkConstantRef(script, expr.(*ast.ConstantRef))
	case *ast.NewObj:
		linker.linkNewObj(script, expr.(*ast.NewObj))
	case *ast.UnaryOp:
		linker.linkUnaryOp(script, expr.(*ast.UnaryOp))
	case *ast.BinaryOp:
		linker.linkBinaryOp(script, expr.(*ast.BinaryOp))
	}

	linker.endLinkNode(expr)
}
Example #5
0
func (channel *_Channel) sendLoop() {
	stream := NewStream(channel.conn, channel.conn)

	for {

		msg, err := channel.pipeline.Sending()

		if err != nil {

			channel.I(err.Error())

			if err != ErrClosed {
				channel.E("[%s] recv message error \n%s", channel.name, err)
			}

			channel.close()

			break
		}

		channel.V("send message %s", msg.Code)

		err = WriteMessage(stream, msg)

		gserrors.Assert(err == nil, "check WriteMessage")

		_, err = stream.Flush()

		if err != nil {
			channel.close()
			channel.E("[%s] recv message error \n%s", channel.name, err)
			break
		}

		channel.V("send message %s -- success", msg.Code)
	}
}