Реализуем WebSocket сервер на c++. Рукопожатие проходит без проблем. Полученные данные успешно декодируются на сервере. Проблема при отправке данных браузеру, постоянно вылетает ошибка undefined и соединение закрывается с ошибкой 1006. Подскажите, что мы делаем не так. Код функций:
int WEBSOCKET_set_content( const char *data, int data_length, unsigned char *dst)
{
unsigned char *message = ( unsigned char * )malloc( 65535 * sizeof( char ) );
int i;
int data_start_index;
message[0] = 129;
if( data_length <= 125 )
{
message[1] = ( unsigned char )data_length;
data_start_index = 2;
}
else
if( data_length > 125 && data_length <= 65535 )
{
message[1] = 126;
message[2] = ( unsigned char )( ( data_length >> 8 ) & 255 );
message[3] = ( unsigned char )( ( data_length ) & 255 );
data_start_index = 4;
}
else
{
message[1] = 127;
message[2] = ( unsigned char )( ( data_length >> 56 ) & 255 );
message[3] = ( unsigned char )( ( data_length >> 48 ) & 255 );
message[4] = ( unsigned char )( ( data_length >> 40 ) & 255 );
message[5] = ( unsigned char )( ( data_length >> 32 ) & 255 );
message[6] = ( unsigned char )( ( data_length >> 24 ) & 255 );
message[7] = ( unsigned char )( ( data_length >> 16 ) & 255 );
message[8] = ( unsigned char )( ( data_length >> 8 ) & 255 );
message[9] = ( unsigned char )( ( data_length ) & 255 );
data_start_index = 10;
}
for( i = 0; i < data_length; i++ )
{
message[ data_start_index + i ] = ( unsigned char )data[i];
}
for( i = 0; i < data_length+ data_start_index; i++ )
{
dst[i] = ( unsigned char )message[ i ];
}
if( message )
{
free( message );
message = NULL;
}
return i;
}
int WEBSOCKET_get_content( const char *data, int data_length, unsigned char *dst)
{
unsigned int i, j;
unsigned char mask[4];
unsigned int packet_length = 0;
unsigned int length_code = 0;
int index_first_mask = 0;
int index_first_data_byte = 0;
if( ( unsigned char )data[0] != 129 )
{
dst = NULL;
if( ( unsigned char )data[0] == 136 )
{
/* WebSocket client disconnected */
return -2;
}
/* Unknown error */
return -1;
}
length_code = ((unsigned char) data[1]) & 127;
if( length_code <= 125 )
{
index_first_mask = 2;
mask[0] = data[2];
mask[1] = data[3];
mask[2] = data[4];
mask[3] = data[5];
}
else
if( length_code == 126 )
{
index_first_mask = 4;
mask[0] = data[4];
mask[1] = data[5];
mask[2] = data[6];
mask[3] = data[7];
}
else
if( length_code == 127 )
{
index_first_mask = 10;
mask[0] = data[10];
mask[1] = data[11];
mask[2] = data[12];
mask[3] = data[13];
}
index_first_data_byte = index_first_mask + 4;
packet_length = data_length - index_first_data_byte;
for( i = index_first_data_byte, j = 0; i < data_length; i++, j++ )
{
dst[ j ] = ( unsigned char )data[ i ] ^ mask[ j % 4];
}
return packet_length;
}