WebSocket.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include "WebSocket.h"
  2. #include <Random.h>
  3. #include "HttpRequest.h"
  4. #include <iostream>
  5. using namespace Network;
  6. using namespace WebSocket;
  7. using namespace Framework;
  8. __declspec( dllexport ) Frame &Frame::operator+=( const Frame &b ) // baut frames zusammen welche zu einer nachricht gehören
  9. {
  10. fin = b.fin;
  11. if( opcode == 0 )
  12. opcode = b.opcode;
  13. dataLength += b.dataLength;
  14. char *data = new char[ dataLength ];
  15. memcpy( data, this->data, dataLength - b.dataLength );
  16. memcpy( data + dataLength, b.data, b.dataLength );
  17. delete[] this->data;
  18. this->data = data;
  19. return *this;
  20. }
  21. __declspec( dllexport ) WebSocketClient::WebSocketClient( const char *path, const char *host, unsigned short port )
  22. : Thread()
  23. {
  24. queue = new Array< Frame >();
  25. callback = 0;
  26. klient = 0;
  27. this->path = path;
  28. this->host = host;
  29. this->port = port;
  30. lastPingFrame = 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. RandomGenerator gen;
  55. if( !klient )
  56. {
  57. Text message = "GET ";
  58. message += path;
  59. message += " HTTP/1.1\n";
  60. message += "Host: ";
  61. message += host;
  62. message += "\nUpgrade: websocket\n";
  63. message += "Connection: Upgrade\n";
  64. char *key = new char[ 25 ];
  65. key[ 24 ] = 0;
  66. key[ 23 ] = '=';
  67. key[ 22 ] = '=';
  68. for( int i = 0; i < 22; i++ )
  69. key[ i ] = allowedKeyChars[ (int)( gen.rand() * numKeyChars ) ];
  70. message += "Sec-WebSocket-Key: ";
  71. message += key;
  72. delete[] key;
  73. message += "\nSec-WebSocket-Version: 13\n\n";
  74. klient = new Klient();
  75. if( !klient->verbinde( port, host ) )
  76. {
  77. klient = klient->release();
  78. return 0;
  79. }
  80. klient->sende( message, message.getLength() );
  81. Text answer;
  82. int br = 0;
  83. do
  84. {
  85. char buff[ 2 ];
  86. buff[ 1 ] = 0;
  87. if( klient->getNachricht( buff, 1 ) )
  88. answer += buff;
  89. else
  90. break;
  91. if( buff[ 0 ] == '\n' )
  92. br++;
  93. else if( buff[ 0 ] != '\r' )
  94. br = 0;
  95. } while( br < 2 && klient->hatNachricht( 1000 ) );
  96. HTTP::Answer *handshakeResponse = new HTTP::Answer( answer );
  97. if( handshakeResponse->getStatusCode() != 101 )
  98. {
  99. handshakeResponse->release();
  100. klient = klient->release();
  101. return 0;
  102. }
  103. handshakeResponse->release();
  104. start();
  105. return 1;
  106. }
  107. else
  108. return 1;
  109. }
  110. __declspec( dllexport ) bool WebSocketClient::send( __int64 size, const char *data, DataType typ )
  111. {
  112. Frame f;
  113. f.fin = 1;
  114. f.rsv1 = 0;
  115. f.rsv2 = 0;
  116. f.rsv3 = 0;
  117. f.mask = 1;
  118. if( typ == TEXT )
  119. f.opcode = 1;
  120. else
  121. f.opcode = 2;
  122. f.dataLength = size;
  123. f.data = new char[ f.dataLength ];
  124. memcpy( f.data, data, f.dataLength );
  125. f.key[ 0 ] = 1;
  126. f.key[ 1 ] = 2;
  127. f.key[ 2 ] = 3;
  128. f.key[ 3 ] = 4;
  129. c.lock();
  130. queue->add( f );
  131. c.unlock();
  132. return 1;
  133. }
  134. __declspec( dllexport ) void WebSocketClient::thread()
  135. {
  136. while( klient )
  137. {
  138. c2.lock();
  139. if( !klient )
  140. {
  141. c2.unlock();
  142. return;
  143. }
  144. if( klient->hatNachricht( 100 ) )
  145. {
  146. c2.unlock();
  147. bool first = 1;
  148. Frame m;
  149. unsigned char byte;
  150. do
  151. {
  152. c2.lock();
  153. if( !klient )
  154. {
  155. c2.unlock();
  156. return;
  157. }
  158. Frame message;
  159. klient->getNachricht( (char*)&byte, 1 );
  160. message.fin = ( byte & 0x80 ) != 0;
  161. message.rsv1 = ( byte & 0x40 ) != 0;
  162. message.rsv2 = ( byte & 0x20 ) != 0;
  163. message.rsv3 = ( byte & 0x10 ) != 0;
  164. message.opcode = byte & 0xF;
  165. klient->getNachricht( (char*)&byte, 1 );
  166. message.mask = ( byte & 0x80 ) != 0;
  167. message.dataLength = byte & 0x7F;
  168. if( message.dataLength == 126 )
  169. {
  170. klient->getNachricht( (char*)&byte, 1 );
  171. message.dataLength = byte << 8;
  172. klient->getNachricht( (char*)&byte, 1 );
  173. message.dataLength |= byte;
  174. }
  175. else if( message.dataLength == 127 )
  176. {
  177. klient->getNachricht( (char*)&byte, 1 );
  178. message.dataLength = (__int64)byte << 56;
  179. klient->getNachricht( (char*)&byte, 1 );
  180. message.dataLength |= (__int64)byte << 48;
  181. klient->getNachricht( (char*)&byte, 1 );
  182. message.dataLength |= (__int64)byte << 40;
  183. klient->getNachricht( (char*)&byte, 1 );
  184. message.dataLength |= (__int64)byte << 32;
  185. klient->getNachricht( (char*)&byte, 1 );
  186. message.dataLength |= (__int64)byte << 24;
  187. klient->getNachricht( (char*)&byte, 1 );
  188. message.dataLength |= (__int64)byte << 16;
  189. klient->getNachricht( (char*)&byte, 1 );
  190. message.dataLength |= (__int64)byte << 8;
  191. klient->getNachricht( (char*)&byte, 1 );
  192. message.dataLength |= (__int64)byte;
  193. }
  194. if( message.mask )
  195. {
  196. klient->getNachricht( (char*)&byte, 1 );
  197. message.key[ 0 ] = byte;
  198. klient->getNachricht( (char*)&byte, 1 );
  199. message.key[ 1 ] = byte;
  200. klient->getNachricht( (char*)&byte, 1 );
  201. message.key[ 2 ] = byte;
  202. klient->getNachricht( (char*)&byte, 1 );
  203. message.key[ 3 ] = byte;
  204. }
  205. message.data = 0;
  206. if( message.dataLength )
  207. message.data = new char[ message.dataLength ];
  208. for( int i = 0; i < message.dataLength; i++ )
  209. {
  210. klient->getNachricht( (char*)&byte, 1 );
  211. if( message.mask )
  212. message.data[ i ] = byte ^ message.key[ i % 4 ];
  213. else
  214. message.data[ i ] = byte;
  215. }
  216. c2.unlock();
  217. if( first )
  218. m = message;
  219. else
  220. {
  221. m += message;
  222. delete[] message.data;
  223. }
  224. first = 0;
  225. } while( !m.fin );
  226. if( callback )
  227. callback( this, m.dataLength, m.data, m.opcode == 1 ? TEXT : BINARY );
  228. delete[] m.data;
  229. }
  230. else
  231. {
  232. c2.unlock();
  233. c.lock();
  234. while( queue->getEintragAnzahl() )
  235. {
  236. Frame f = queue->get( 0 );
  237. c.unlock();
  238. unsigned char byte = ( f.fin ? 1 : 0 ) << 7;
  239. byte |= ( f.rsv1 ? 1 : 0 ) << 6;
  240. byte |= ( f.rsv2 ? 1 : 0 ) << 5;
  241. byte |= ( f.rsv3 ? 1 : 0 ) << 4;
  242. byte |= f.opcode & 0xF;
  243. c2.lock();
  244. if( !klient )
  245. {
  246. c2.unlock();
  247. return;
  248. }
  249. klient->sende( (char*)&byte, 1 );
  250. byte = ( f.mask ? 1 : 0 ) << 7;
  251. if( f.dataLength < 126 )
  252. byte |= (unsigned char)f.dataLength & 0x7F;
  253. else if( f.dataLength <= 32767 )
  254. byte |= 126;
  255. else
  256. byte |= 127;
  257. klient->sende( (char*)&byte, 1 );
  258. if( f.dataLength >= 126 )
  259. {
  260. if( f.dataLength <= 32767 )
  261. {
  262. byte = (unsigned char)( f.dataLength >> 8 );
  263. klient->sende( (char*)&byte, 1 );
  264. byte = (unsigned char)f.dataLength & 0xFF;
  265. klient->sende( (char*)&byte, 1 );
  266. }
  267. else
  268. {
  269. byte = (unsigned char)( f.dataLength >> 56 );
  270. klient->sende( (char*)&byte, 1 );
  271. byte = (unsigned char)( f.dataLength >> 48 );
  272. klient->sende( (char*)&byte, 1 );
  273. byte = (unsigned char)( f.dataLength >> 40 );
  274. klient->sende( (char*)&byte, 1 );
  275. byte = (unsigned char)( f.dataLength >> 32 );
  276. klient->sende( (char*)&byte, 1 );
  277. byte = (unsigned char)( f.dataLength >> 24 );
  278. klient->sende( (char*)&byte, 1 );
  279. byte = (unsigned char)( f.dataLength >> 16 );
  280. klient->sende( (char*)&byte, 1 );
  281. byte = (unsigned char)( f.dataLength >> 8 );
  282. klient->sende( (char*)&byte, 1 );
  283. byte = (unsigned char)f.dataLength & 0xFF;
  284. klient->sende( (char*)&byte, 1 );
  285. }
  286. }
  287. if( f.mask )
  288. {
  289. byte = (unsigned char)f.key[ 0 ];
  290. klient->sende( (char*)&byte, 1 );
  291. byte = (unsigned char)f.key[ 1 ];
  292. klient->sende( (char*)&byte, 1 );
  293. byte = (unsigned char)f.key[ 2 ];
  294. klient->sende( (char*)&byte, 1 );
  295. byte = (unsigned char)f.key[ 3 ];
  296. klient->sende( (char*)&byte, 1 );
  297. }
  298. if( f.dataLength )
  299. {
  300. for( int i = 0; i < f.dataLength; i++ )
  301. {
  302. if( f.mask )
  303. byte = (unsigned char)f.data[ i ] ^ f.key[ i % 4 ];
  304. else
  305. byte = (unsigned char)f.data[ i ];
  306. klient->sende( (char*)&byte, 1 );
  307. }
  308. }
  309. c2.unlock();
  310. delete[] f.data;
  311. c.lock();
  312. queue->remove( 0 );
  313. }
  314. c.unlock();
  315. }
  316. }
  317. }
  318. __declspec( dllexport ) void WebSocketClient::disconnect()
  319. {
  320. if( !klient )
  321. return;
  322. c2.lock();
  323. klient->trenne();
  324. klient = klient->release();
  325. c2.unlock();
  326. warteAufThread( 1000 );
  327. }
  328. __declspec( dllexport ) bool WebSocketClient::isConnected() const
  329. {
  330. return klient != 0;
  331. }