96 lines
2.8 KiB
C++
96 lines
2.8 KiB
C++
#ifndef _CLIENT_
|
|
#define _CLIENT_
|
|
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <stack>
|
|
#include <queue>
|
|
#include <signal.h>
|
|
//#include <pthread.h>
|
|
#include <string>
|
|
#include <libwebsockets.h>
|
|
#include "zjdtypes.h"
|
|
|
|
using namespace std;
|
|
|
|
#define MAX_PAYLOAD_SIZE 20 * 1024
|
|
|
|
typedef enum
|
|
{
|
|
CLIENT_IDLE,
|
|
CLIENT_CONNECTING,
|
|
CLIENT_CONNECTED,
|
|
CLIENT_AWAITING_SEND,
|
|
ClIENT_COMPLETE_RECV,
|
|
CLIENT_CLOSED
|
|
} TSTAT;
|
|
|
|
typedef struct session_data {
|
|
int msg_count;
|
|
unsigned char buf[LWS_PRE + MAX_PAYLOAD_SIZE];
|
|
int len;
|
|
} session_data, *SESSIONDATA;
|
|
|
|
class CWSClient
|
|
{
|
|
public:
|
|
CWSClient(string url);
|
|
CWSClient(void);
|
|
~CWSClient();
|
|
|
|
void init();
|
|
void init(std::string url);
|
|
int set_ssl(const char* ca_filepath, const char* server_cert_filepath, const char*server_private_key_filepath, int ssl_conn);
|
|
bool create();
|
|
bool connect(int ssl_conn);
|
|
bool reconnect(void);
|
|
bool run(int wait_time);
|
|
int send(string data);
|
|
int recv(std::string data);
|
|
int recvMsg(void);
|
|
int recvFrame(std::string);
|
|
int getRecvSize();
|
|
string getRecvMsg();
|
|
void popRecvMsg();
|
|
#if 0
|
|
void setLogLevel(int level);
|
|
#endif
|
|
TSTAT getConnStat();
|
|
void destroy();
|
|
void setEnd(bool flag);
|
|
|
|
DWORD getConnectTime(void) { return m_reconnect_attempts; }
|
|
void setConnectTime(DWORD tm) { m_reconnect_attempts = tm; }
|
|
|
|
friend void* WSpthreadFunc(void* arg);
|
|
friend int callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len);
|
|
|
|
private:
|
|
|
|
string m_cacheFile;
|
|
bool m_end;
|
|
int m_waittime;
|
|
pthread_t m_threadID;
|
|
queue<string> m_sendMsgQueue;
|
|
queue<string> m_recvMsgQueue;
|
|
queue<string> m_recvFrameQueue;
|
|
SESSIONDATA m_session_data;
|
|
string m_url;
|
|
const char* m_prot;
|
|
const char* m_ads;
|
|
const char* m_path;
|
|
int m_port;
|
|
TSTAT m_stat;
|
|
int m_logLevel;
|
|
DWORD m_reconnect_attempts;
|
|
struct lws_protocols * m_protocols;
|
|
struct lws_context_creation_info m_ctx_info; /* 用于创建vhost或者context的参数 */
|
|
struct lws_context * m_context;
|
|
struct lws_client_connect_info m_conn_info;
|
|
struct lws * m_wsi;
|
|
pthread_mutex_t m_rmutex;
|
|
pthread_mutex_t m_smutex;
|
|
};
|
|
|
|
#endif
|