-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathWebSocketConn.cpp
More file actions
136 lines (127 loc) · 4.36 KB
/
WebSocketConn.cpp
File metadata and controls
136 lines (127 loc) · 4.36 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* =====================================================================================
*
* Filename: WebSocketConn.cpp
*
* Description:
*
* Version: 1.0
* Created: 06/23/16 09:59:19
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include <WebSocketConn.h>
void WebSocketConn::OnRead()
{
if(m_socket_type == NORMALSOCKET) {
CMsgConn::OnRead();
return;
}
for (;;)
{
uint32_t free_buf_len = m_in_buf.GetAllocSize() - m_in_buf.GetWriteOffset();
if (free_buf_len < READ_BUF_SIZE)
m_in_buf.Extend(READ_BUF_SIZE);
int ret = netlib_recv(m_handle, m_in_buf.GetBuffer() + m_in_buf.GetWriteOffset(), READ_BUF_SIZE);
if (ret <= 0)
break;
m_recv_bytes += ret;
m_in_buf.IncWriteOffset(ret);
m_last_recv_tick = get_tick_count();
}
/*
if(m_websocket == NULL)
{
m_websocket = new WebSocket;
}
*/
WebSocket websocket;
if(m_websocket_inited)
{
CImPdu *pPdu = NULL;
int data_size = m_in_buf.GetWriteOffset();
unsigned char *dataBuffer = (unsigned char*)malloc(data_size);
try{
int data_length = 0;
int use_length = 0;
int read_count = 0;
WebSocketFrameType frameType = websocket.getFrame(m_in_buf.GetBuffer() ,m_in_buf.GetWriteOffset() ,dataBuffer ,data_size, data_length,use_length);
//log("len:%d use:%d",m_in_buf.GetWriteOffset(),use_length);
while(frameType == BINARY_FRAME || frameType == TEXT_FRAME)
{
read_count = read_count + use_length;
while((pPdu = CImPdu::ReadPdu((uchar_t*)dataBuffer,data_length)))
{
uint32_t pdu_len = pPdu->GetLength();
HandlePdu(pPdu);
delete pPdu;
pPdu = NULL;
break;
}
m_in_buf.Read(NULL, use_length);
if(read_count >= data_size) {
break;
}
data_length = 0;
use_length = 0;
frameType = websocket.getFrame(m_in_buf.GetBuffer(),m_in_buf.GetWriteOffset() ,dataBuffer ,data_size, data_length,use_length);
}
//m_in_buf.~CSimpleBuffer();
}catch(CPduException& ex)
{
}
free(dataBuffer);
}else
{
WebSocketFrameType type = websocket.parseHandshake(m_in_buf.GetBuffer(),m_in_buf.GetWriteOffset());
if(type == OPENING_FRAME) {
log("this is a websocket");
string answer = websocket.answerHandshake();
CMsgConn::Send((void *)answer.c_str(), answer.size());
m_in_buf.Read(NULL, m_in_buf.GetWriteOffset());
// m_in_buf.~CSimpleBuffer();
m_websocket_inited = true;
}else {
m_socket_type = NORMALSOCKET;
log("this is a normal socket");
CImPdu *pPdu = NULL;
try
{
while ((pPdu = CImPdu::ReadPdu(m_in_buf.GetBuffer(), m_in_buf.GetWriteOffset())))
{
uint32_t pdu_len = pPdu->GetLength();
HandlePdu(pPdu);
m_in_buf.Read(NULL, pdu_len);
delete pPdu;
pPdu = NULL;
}
} catch (CPduException& ex) {
log("!!!catch exception, sid=%u, cid=%u, err_code=%u, err_msg=%s, close the connection ",
ex.GetServiceId(), ex.GetCommandId(), ex.GetErrorCode(), ex.GetErrorMsg());
if (pPdu) {
delete pPdu;
pPdu = NULL;
}
OnClose();
}
//OnClose();
}
}
}
int WebSocketConn::Send(void *data, int len)
{
if(m_socket_type == NORMALSOCKET){
return CMsgConn::Send(data ,len);
}
WebSocket websocket;
unsigned char* outData = (unsigned char*)malloc(len + 14);
int out_len = websocket.makeFrame(BINARY_FRAME, (unsigned char*)data,len,outData ,len + 14);
int result = CMsgConn::Send(outData ,out_len);
free(outData);
return result;
}