summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/WhatsAPI++/BinTreeNodeWriter.h
blob: 66d9cbb99117481862c43c6b361f66ef93d8da55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * BinTreeNodeWriter.h
 *
 *  Created on: 26/06/2012
 *      Author: Antonio
 */


#ifndef BINTREENODEWRITER_H_
#define BINTREENODEWRITER_H_

#include <string>
#include "ProtocolTreeNode.h"
#include "ISocketConnection.h"
#include "ByteArray.h"
#include "IMutex.h"
#include "WAConnection.h"

using namespace std;

#define STREAM_START 1
#define	STREAM_END 2
#define LIST_EMPTY 0
#define	LIST_8 248
#define	LIST_16 249
#define	JID_PAIR 250
#define	BINARY_8 252
#define	BINARY_24 253
#define	TOKEN_8 254

#include <map>

class WAConnection;

class BinTreeNodeWriter {
private:
	WAConnection* conn;
	ISocketConnection *realOut;
	ByteArrayOutputStream *out;
	IMutex* mutex;
	int dataBegin;
	bool bLoggedIn;

	void writeListStart(int i);
	void writeInt8(int v);
	void writeInt16(int v, ISocketConnection* out);
	void writeInt16(int v, ByteArrayOutputStream* out);
	void writeInt16(int v);
	void writeAttributes(std::map<string, string>* attributes);
	void writeString(const std::string& tag);
	void writeJid(std::string* user, const std::string& server);
	void writeToken(int intValue);
	void writeBytes(unsigned char* bytes, int length);
	void writeInt24(int v);
	void writeInternal(const ProtocolTreeNode &node);
	void writeDummyHeader();
	void processBuffer();

public:
	BinTreeNodeWriter(WAConnection* conn, ISocketConnection* connection, IMutex* mutex);
	virtual ~BinTreeNodeWriter();

	void streamStart(std::string domain, std::string resource);
	void flushBuffer(bool flushNetwork);
	void flushBuffer(bool flushNetwork, int startingOffset);
	void streamEnd();
	void write(const ProtocolTreeNode &node);
	void write(const ProtocolTreeNode &node, bool needsFlush);

	void setLoggedIn() { bLoggedIn = true; }
};

#endif /* BINTREENODEWRITER_H_ */