amf.h 3.55 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2 3
 * MIT License
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
xzl committed
26 27 28 29
#ifndef __amf_h
#define __amf_h

#include <assert.h>
xiongzilaing committed
30
#include <string>
xzl committed
31
#include <vector>
xiongzilaing committed
32
#include <unordered_map>
xiongziliang committed
33
#include <map>
34
#include <stdexcept>
xiongziliang committed
35 36 37
#include <functional>
using namespace std;

xzl committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
enum AMFType {
	AMF_NUMBER,
	AMF_INTEGER,
	AMF_BOOLEAN,
	AMF_STRING,
	AMF_OBJECT,
	AMF_NULL,
	AMF_UNDEFINED,
	AMF_ECMA_ARRAY,
	AMF_STRICT_ARRAY,
};

class AMFValue;

class AMFValue {
public:
xiongziliang committed
54 55 56
    friend class AMFEncoder;
    typedef std::map<std::string, AMFValue> mapType;
    typedef std::vector<AMFValue> arrayType;
xzl committed
57

xiongziliang committed
58
	AMFValue(AMFType type = AMF_NULL);
xzl committed
59 60 61 62 63 64 65 66 67 68 69
	AMFValue(const char *s);
	AMFValue(const std::string &s);
	AMFValue(double n);
	AMFValue(int i);
	AMFValue(bool b);
	AMFValue(const AMFValue &from);
	AMFValue(AMFValue &&from);
	AMFValue &operator =(const AMFValue &from);
	AMFValue &operator =(AMFValue &&from);
	~AMFValue();

xiongziliang committed
70 71 72 73 74 75
	void clear();
	AMFType type() const ;
	const std::string &as_string() const;
	double as_number() const;
	int as_integer() const;
    bool as_boolean() const;
xiongziliang committed
76
	string to_string() const;
xiongziliang committed
77 78 79 80 81 82 83 84 85 86
	const AMFValue &operator[](const char *str) const;
	void object_for_each(const function<void(const string &key, const AMFValue &val)> &fun) const ;
	operator bool() const;
	void set(const std::string &s, const AMFValue &val);
	void add(const AMFValue &val);
private:
    const mapType &getMap() const;
    const arrayType &getArr() const;
    void destroy();
    void init();
xzl committed
87
private:
88
	AMFType _type;
xzl committed
89 90 91 92 93 94 95
	union {
		std::string *string;
		double number;
		int integer;
		bool boolean;
		mapType *object;
		arrayType *array;
96
	} _value;
xzl committed
97 98 99 100
};

class AMFDecoder {
public:
xiongziliang committed
101
	AMFDecoder(const std::string &buf, size_t pos, int version = 0);
xzl committed
102 103 104 105 106 107 108 109 110
	template<typename TP>
	TP load();
private:
	std::string load_key();
	AMFValue load_object();
	AMFValue load_ecma();
	AMFValue load_arr();
	uint8_t front();
	uint8_t pop_front();
xiongziliang committed
111 112 113 114
private:
	const std::string &buf;
	size_t pos;
	int version;
xzl committed
115 116 117 118 119 120 121 122 123 124 125
};

class AMFEncoder {
public:
	AMFEncoder & operator <<(const char *s);
	AMFEncoder & operator <<(const std::string &s);
	AMFEncoder & operator <<(std::nullptr_t);
	AMFEncoder & operator <<(const int n);
	AMFEncoder & operator <<(const double n);
	AMFEncoder & operator <<(const bool b);
	AMFEncoder & operator <<(const AMFValue &value);
xiongziliang committed
126 127
	const std::string& data() const ;
	void clear() ;
xzl committed
128 129 130
private:
	void write_key(const std::string &s);
	AMFEncoder &write_undefined();
xiongziliang committed
131 132
private:
    std::string buf;
xzl committed
133 134 135 136
};


#endif