Пример #1
0
func (db *DB) send(param string, req interface{}, reply replyHandler) error {
	data, err := asn1.MarshalWithParams(req, param)
	if err != nil {
		return err
	}

	db.lk.Lock()
	defer db.lk.Unlock()

	if db.closed {
		return ErrClosed
	}

	if db.conn == nil {
		rw, conn, err := open(db.url, db.cfg)
		if err != nil {
			return err
		}

		db.conn = conn
		db.rw = rw
		go db.rxThread(rw, conn)
	}

	id := db.nextId
	db.nextId++

	msg := message{
		Id: id,
		Data: asn1.RawValue{
			FullBytes: data,
		},
	}

	out, err := asn1.Marshal(msg)
	if err != nil {
		return err
	}

	if _, err := db.rw.Write(out); err != nil {
		return err
	}

	if reply != nil {
		db.replies[id] = reply
	}

	return nil
}
Пример #2
0
func dosend(rw io.ReadWriter, param string, req interface{}) error {
	rdata, err := asn1.MarshalWithParams(req, param)
	if err != nil {
		return err
	}

	msg := message{
		Id: 0,
		Data: asn1.RawValue{
			FullBytes: rdata,
		},
	}

	mdata, err := asn1.Marshal(msg)
	if err != nil {
		return err
	}

	if _, err := rw.Write(mdata); err != nil {
		return err
	}

	return nil
}