amf.h 2.92 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
xiongziliang committed
9
 */
xiongziliang committed
10

xzl committed
11 12 13 14
#ifndef __amf_h
#define __amf_h

#include <assert.h>
xiongzilaing committed
15
#include <string>
xzl committed
16
#include <vector>
xiongzilaing committed
17
#include <unordered_map>
xiongziliang committed
18
#include <map>
19
#include <stdexcept>
xiongziliang committed
20
#include <functional>
21
#include "Network/Buffer.h"
xiongziliang committed
22
using namespace std;
23
using namespace toolkit;
xiongziliang committed
24

xzl committed
25
enum AMFType {
26 27 28 29 30 31 32 33 34
    AMF_NUMBER,
    AMF_INTEGER,
    AMF_BOOLEAN,
    AMF_STRING,
    AMF_OBJECT,
    AMF_NULL,
    AMF_UNDEFINED,
    AMF_ECMA_ARRAY,
    AMF_STRICT_ARRAY,
xzl committed
35 36 37 38 39 40
};

class AMFValue;

class AMFValue {
public:
xiongziliang committed
41 42 43
    friend class AMFEncoder;
    typedef std::map<std::string, AMFValue> mapType;
    typedef std::vector<AMFValue> arrayType;
xzl committed
44

45
    ~AMFValue();
46 47 48 49 50 51 52
    AMFValue(AMFType type = AMF_NULL);
    AMFValue(const char *s);
    AMFValue(const std::string &s);
    AMFValue(double n);
    AMFValue(int i);
    AMFValue(bool b);
    AMFValue(const AMFValue &from);
53
    AMFValue &operator = (const AMFValue &from);
xzl committed
54

55 56 57 58 59
    void clear();
    AMFType type() const ;
    const std::string &as_string() const;
    double as_number() const;
    int as_integer() const;
xiongziliang committed
60
    bool as_boolean() const;
61 62 63 64 65 66
    string to_string() const;
    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);
xiongziliang committed
67 68 69 70 71
private:
    const mapType &getMap() const;
    const arrayType &getArr() const;
    void destroy();
    void init();
xzl committed
72
private:
73 74 75 76 77 78 79 80 81
    AMFType _type;
    union {
        std::string *string;
        double number;
        int integer;
        bool boolean;
        mapType *object;
        arrayType *array;
    } _value;
xzl committed
82 83 84 85
};

class AMFDecoder {
public:
86
    AMFDecoder(const BufferLikeString &buf, size_t pos, int version = 0);
87 88
    template<typename TP>
    TP load();
xzl committed
89
private:
90 91 92 93 94 95
    std::string load_key();
    AMFValue load_object();
    AMFValue load_ecma();
    AMFValue load_arr();
    uint8_t front();
    uint8_t pop_front();
xiongziliang committed
96
private:
97
    const BufferLikeString &buf;
98 99
    size_t pos;
    int version;
xzl committed
100 101 102 103
};

class AMFEncoder {
public:
104 105 106 107 108 109 110 111 112
    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);
    const std::string& data() const ;
    void clear() ;
xzl committed
113
private:
114 115
    void write_key(const std::string &s);
    AMFEncoder &write_undefined();
xiongziliang committed
116 117
private:
    std::string buf;
xzl committed
118 119 120 121
};


#endif