#pragma once
#include "ozApi.h"
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <time.h>
#include <vector>
#include <map>
#include <fstream>
#include <queue>
#include <exception>
#pragma comment(lib, "wininet.lib")
#pragma comment(lib, "Ws2_32.lib")

using namespace std;

#define KEEPALIVETIMER 30000 // 30 sec
#define BUFFLEN  1024
#define STX '\x02'
#define ETX '\x03'


DWORD WINAPI receiveThread(LPVOID param);

class ozConnection : public ozApi
{
private:
	queue<string> msgQueue;
	
	WSADATA wsaData;
	sockaddr_in clientService; 
	SOCKET ConnectSocket;
	int transactionId;
	bool run;
	HANDLE hThreadReceive;
	HANDLE hThreadKeepAlive;

	int sendPdu(string pdu);
	string randomString(int size, bool lowerCase);
	string makeMessageId();
	int nextTransactionId();
	string encode(string text);
	int startReceiveThread();
	int keepAlive();
	DWORD  keepAliveThread(void);
	static DWORD WINAPI skeepAliveThread(void* param);
	DWORD  receiveThread(void);
	static DWORD WINAPI sreceiveThread(void* param);
	void SplitPdu(string pdu, vector<string> &results);
	void SplitString(string str, string delim, vector<string> &results);


public:
	ozConnection(void);
	virtual int open(string host, int port);
	virtual bool login(string username, string password);
	virtual int close();
	virtual string sendMessage(string receiver, string message,string sender, string messagetype, string serviceProvider);

protected:
	~ozConnection(void);

};
