WebSocket.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #include "WebSocket.h"
  2. #include "HttpRequest.h"
  3. #include <iostream>
  4. using namespace Network;
  5. using namespace WebSocket;
  6. using namespace Framework;
  7. __declspec( dllexport ) Frame &Frame::operator+=( const Frame &b ) // baut frames zusammen welche zu einer nachricht gehören
  8. {
  9. fin = b.fin;
  10. if( opcode == 0 )
  11. opcode = b.opcode;
  12. dataLength += b.dataLength;
  13. char *data = new char[ dataLength ];
  14. memcpy( data, this->data, dataLength - b.dataLength );
  15. memcpy( data + dataLength, b.data, b.dataLength );
  16. delete[] this->data;
  17. this->data = data;
  18. return *this;
  19. }
  20. __declspec( dllexport ) WebSocketClient::WebSocketClient( const char *path, const char *host, unsigned short port )
  21. : Thread()
  22. {
  23. queue = new Array< Frame >();
  24. callback = 0;
  25. klient = 0;
  26. this->path = path;
  27. this->host = host;
  28. this->port = port;
  29. lastPingFrame = 0;
  30. nextClose = 0;
  31. }
  32. WebSocketClient::~WebSocketClient()
  33. {
  34. disconnect();
  35. while( queue->getEintragAnzahl() )
  36. {
  37. Frame f = queue->get( 0 );
  38. delete[] f.data;
  39. queue->remove( 0 );
  40. }
  41. queue->release();
  42. }
  43. __declspec( dllexport ) void WebSocketClient::setMessageCallback( std::function< void( WebSocketClient *, __int64 size, const char *data, DataType typ ) > callback )
  44. {
  45. this->callback = callback;
  46. }
  47. __declspec( dllexport ) bool WebSocketClient::connect()
  48. {
  49. char allowedKeyChars[] = { 'a', 'b','c','d','e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  50. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  51. '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
  52. 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 0 };
  53. __int64 numKeyChars = strlen( allowedKeyChars );
  54. if( !klient )
  55. {
  56. Text message = "GET ";
  57. message += path;
  58. message += " HTTP/1.1\n";
  59. message += "Host: ";
  60. message += host;
  61. message += "\nUpgrade: websocket\n";
  62. message += "Connection: Upgrade\n";
  63. char *key = new char[ 25 ];
  64. key[ 24 ] = 0;
  65. key[ 23 ] = '=';
  66. key[ 22 ] = '=';
  67. for( int i = 0; i < 22; i++ )
  68. key[ i ] = allowedKeyChars[ (int)( gen.rand() * numKeyChars ) ];
  69. message += "Sec-WebSocket-Key: ";
  70. message += key;
  71. delete[] key;
  72. message += "\nSec-WebSocket-Version: 13\n\n";
  73. klient = new Klient();
  74. if( !klient->verbinde( port, host ) )
  75. {
  76. klient = klient->release();
  77. return 0;
  78. }
  79. klient->sende( message, message.getLength() );
  80. Text answer;
  81. int br = 0;
  82. do
  83. {
  84. char buff[ 2 ];
  85. buff[ 1 ] = 0;
  86. if( klient->getNachricht( buff, 1 ) )
  87. answer += buff;
  88. else
  89. break;
  90. if( buff[ 0 ] == '\n' )
  91. br++;
  92. else if( buff[ 0 ] != '\r' )
  93. br = 0;
  94. } while( br < 2 && klient->hatNachricht( 1000 ) );
  95. HTTP::Answer *handshakeResponse = new HTTP::Answer( answer );
  96. if( handshakeResponse->getStatusCode() != 101 )
  97. {
  98. handshakeResponse->release();
  99. klient = klient->release();
  100. return 0;
  101. }
  102. handshakeResponse->release();
  103. start();
  104. return 1;
  105. }
  106. else
  107. return 1;
  108. }
  109. __declspec( dllexport ) bool WebSocketClient::send( __int64 size, const char *data, DataType typ )
  110. {
  111. Frame f;
  112. f.fin = 1;
  113. f.rsv1 = 0;
  114. f.rsv2 = 0;
  115. f.rsv3 = 0;
  116. f.mask = 1;
  117. if( typ == TEXT )
  118. f.opcode = 1;
  119. else
  120. f.opcode = 2;
  121. f.dataLength = size;
  122. f.data = new char[ f.dataLength ];
  123. memcpy( f.data, data, f.dataLength );
  124. f.key[ 0 ] = (unsigned char)(gen.rand() * 256);
  125. f.key[ 1 ] = (unsigned char)(gen.rand() * 256);
  126. f.key[ 2 ] = (unsigned char)(gen.rand() * 256);
  127. f.key[ 3 ] = (unsigned char)(gen.rand() * 256);
  128. c.lock();
  129. queue->add( f );
  130. c.unlock();
  131. return 1;
  132. }
  133. __declspec( dllexport ) void WebSocketClient::thread()
  134. {
  135. while( klient )
  136. {
  137. c2.lock();
  138. if( !klient )
  139. {
  140. c2.unlock();
  141. return;
  142. }
  143. if( klient->hatNachricht( 100 ) )
  144. {
  145. c2.unlock();
  146. bool first = 1;
  147. Frame m;
  148. unsigned char byte;
  149. do
  150. {
  151. c2.lock();
  152. if( !klient )
  153. {
  154. c2.unlock();
  155. return;
  156. }
  157. Frame message;
  158. klient->getNachricht( (char*)&byte, 1 );
  159. message.fin = ( byte & 0x80 ) != 0;
  160. message.rsv1 = ( byte & 0x40 ) != 0;
  161. message.rsv2 = ( byte & 0x20 ) != 0;
  162. message.rsv3 = ( byte & 0x10 ) != 0;
  163. message.opcode = byte & 0xF;
  164. klient->getNachricht( (char*)&byte, 1 );
  165. message.mask = ( byte & 0x80 ) != 0;
  166. message.dataLength = byte & 0x7F;
  167. if( message.dataLength == 126 )
  168. {
  169. klient->getNachricht( (char*)&byte, 1 );
  170. message.dataLength = byte << 8;
  171. klient->getNachricht( (char*)&byte, 1 );
  172. message.dataLength |= byte;
  173. }
  174. else if( message.dataLength == 127 )
  175. {
  176. klient->getNachricht( (char*)&byte, 1 );
  177. message.dataLength = (__int64)byte << 56;
  178. klient->getNachricht( (char*)&byte, 1 );
  179. message.dataLength |= (__int64)byte << 48;
  180. klient->getNachricht( (char*)&byte, 1 );
  181. message.dataLength |= (__int64)byte << 40;
  182. klient->getNachricht( (char*)&byte, 1 );
  183. message.dataLength |= (__int64)byte << 32;
  184. klient->getNachricht( (char*)&byte, 1 );
  185. message.dataLength |= (__int64)byte << 24;
  186. klient->getNachricht( (char*)&byte, 1 );
  187. message.dataLength |= (__int64)byte << 16;
  188. klient->getNachricht( (char*)&byte, 1 );
  189. message.dataLength |= (__int64)byte << 8;
  190. klient->getNachricht( (char*)&byte, 1 );
  191. message.dataLength |= (__int64)byte;
  192. }
  193. if( message.mask )
  194. {
  195. klient->getNachricht( (char*)&byte, 1 );
  196. message.key[ 0 ] = byte;
  197. klient->getNachricht( (char*)&byte, 1 );
  198. message.key[ 1 ] = byte;
  199. klient->getNachricht( (char*)&byte, 1 );
  200. message.key[ 2 ] = byte;
  201. klient->getNachricht( (char*)&byte, 1 );
  202. message.key[ 3 ] = byte;
  203. }
  204. message.data = 0;
  205. if( message.dataLength )
  206. message.data = new char[ message.dataLength ];
  207. for( int i = 0; i < message.dataLength; i++ )
  208. {
  209. klient->getNachricht( (char*)&byte, 1 );
  210. if( message.mask )
  211. message.data[ i ] = byte ^ message.key[ i % 4 ];
  212. else
  213. message.data[ i ] = byte;
  214. }
  215. c2.unlock();
  216. if( first )
  217. m = message;
  218. else
  219. {
  220. m += message;
  221. delete[] message.data;
  222. }
  223. first = 0;
  224. } while( !m.fin );
  225. if( m.opcode == 0x9 )
  226. {
  227. m.opcode = 0xA;
  228. m.key[ 0 ] = (unsigned char)( gen.rand() * 256 );
  229. m.key[ 1 ] = (unsigned char)( gen.rand() * 256 );
  230. m.key[ 2 ] = (unsigned char)( gen.rand() * 256 );
  231. m.key[ 3 ] = (unsigned char)( gen.rand() * 256 );
  232. queue->add( m );
  233. }
  234. else if( m.opcode != 0xA )
  235. {
  236. delete[] m.data;
  237. }
  238. else if( m.opcode == 0x8 )
  239. {
  240. if( nextClose )
  241. {
  242. delete[] m.data;
  243. c2.lock();
  244. klient->trenne();
  245. klient = klient->release();
  246. c2.unlock();
  247. return;
  248. }
  249. else
  250. {
  251. m.key[ 0 ] = (unsigned char)( gen.rand() * 256 );
  252. m.key[ 1 ] = (unsigned char)( gen.rand() * 256 );
  253. m.key[ 2 ] = (unsigned char)( gen.rand() * 256 );
  254. m.key[ 3 ] = (unsigned char)( gen.rand() * 256 );
  255. queue->add( m );
  256. nextClose = 1;
  257. }
  258. }
  259. else if( callback )
  260. {
  261. callback( this, m.dataLength, m.data, m.opcode == 1 ? TEXT : BINARY );
  262. }
  263. }
  264. else
  265. {
  266. c2.unlock();
  267. c.lock();
  268. while( queue->getEintragAnzahl() )
  269. {
  270. Frame f = queue->get( 0 );
  271. c.unlock();
  272. unsigned char byte = ( f.fin ? 1 : 0 ) << 7;
  273. byte |= ( f.rsv1 ? 1 : 0 ) << 6;
  274. byte |= ( f.rsv2 ? 1 : 0 ) << 5;
  275. byte |= ( f.rsv3 ? 1 : 0 ) << 4;
  276. byte |= f.opcode & 0xF;
  277. c2.lock();
  278. if( !klient )
  279. {
  280. c2.unlock();
  281. return;
  282. }
  283. klient->sende( (char*)&byte, 1 );
  284. byte = ( f.mask ? 1 : 0 ) << 7;
  285. if( f.dataLength < 126 )
  286. byte |= (unsigned char)f.dataLength & 0x7F;
  287. else if( f.dataLength <= 32767 )
  288. byte |= 126;
  289. else
  290. byte |= 127;
  291. klient->sende( (char*)&byte, 1 );
  292. if( f.dataLength >= 126 )
  293. {
  294. if( f.dataLength <= 32767 )
  295. {
  296. byte = (unsigned char)( f.dataLength >> 8 );
  297. klient->sende( (char*)&byte, 1 );
  298. byte = (unsigned char)f.dataLength & 0xFF;
  299. klient->sende( (char*)&byte, 1 );
  300. }
  301. else
  302. {
  303. byte = (unsigned char)( f.dataLength >> 56 );
  304. klient->sende( (char*)&byte, 1 );
  305. byte = (unsigned char)( f.dataLength >> 48 );
  306. klient->sende( (char*)&byte, 1 );
  307. byte = (unsigned char)( f.dataLength >> 40 );
  308. klient->sende( (char*)&byte, 1 );
  309. byte = (unsigned char)( f.dataLength >> 32 );
  310. klient->sende( (char*)&byte, 1 );
  311. byte = (unsigned char)( f.dataLength >> 24 );
  312. klient->sende( (char*)&byte, 1 );
  313. byte = (unsigned char)( f.dataLength >> 16 );
  314. klient->sende( (char*)&byte, 1 );
  315. byte = (unsigned char)( f.dataLength >> 8 );
  316. klient->sende( (char*)&byte, 1 );
  317. byte = (unsigned char)f.dataLength & 0xFF;
  318. klient->sende( (char*)&byte, 1 );
  319. }
  320. }
  321. if( f.mask )
  322. {
  323. byte = (unsigned char)f.key[ 0 ];
  324. klient->sende( (char*)&byte, 1 );
  325. byte = (unsigned char)f.key[ 1 ];
  326. klient->sende( (char*)&byte, 1 );
  327. byte = (unsigned char)f.key[ 2 ];
  328. klient->sende( (char*)&byte, 1 );
  329. byte = (unsigned char)f.key[ 3 ];
  330. klient->sende( (char*)&byte, 1 );
  331. }
  332. if( f.dataLength )
  333. {
  334. for( int i = 0; i < f.dataLength; i++ )
  335. {
  336. if( f.mask )
  337. byte = (unsigned char)f.data[ i ] ^ f.key[ i % 4 ];
  338. else
  339. byte = (unsigned char)f.data[ i ];
  340. klient->sende( (char*)&byte, 1 );
  341. }
  342. }
  343. c2.unlock();
  344. delete[] f.data;
  345. c.lock();
  346. queue->remove( 0 );
  347. if( f.opcode == 0x8 )
  348. {
  349. if( nextClose )
  350. {
  351. c2.lock();
  352. klient->trenne();
  353. klient = klient->release();
  354. c2.unlock();
  355. return;
  356. }
  357. else
  358. {
  359. nextClose = 1;
  360. }
  361. }
  362. }
  363. c.unlock();
  364. }
  365. }
  366. }
  367. __declspec( dllexport ) void WebSocketClient::disconnect()
  368. {
  369. if( !klient )
  370. return;
  371. c2.lock();
  372. klient->trenne();
  373. klient = klient->release();
  374. c2.unlock();
  375. warteAufThread( 1000 );
  376. }
  377. __declspec( dllexport ) bool WebSocketClient::isConnected() const
  378. {
  379. return klient != 0;
  380. }