<commit_before>var http = require('http');
var crypto = require('crypto');
var url = require('url');
var path = require('path');
var fs = require('fs');

var srv = http.createServer();
var serverName="liuguang/ws",socketArr=[];

function sendWsMessage(socket,frameData){
	//console.log(frameData);
	//console.log(frameData.payloadData.toString());
	var frameArr=[],tmp,i;
	frameArr.push((frameData.fin<<7)+(frameData.rsv1<<6)+(frameData.rsv2<<5)+(frameData.rsv3<<4)+frameData.opcode);
	tmp=(frameData.mask<<7);
	if((frameData.payloadLength>=0)&&(frameData.payloadLength<=125)){
		frameArr.push(tmp+frameData.payloadLength);
	}
	else if(frameData.payloadLength<parseInt("1111"+"1111"+"1111"+"1111",2)){
		frameArr.push(tmp+126);
		frameArr.push(frameData.payloadLength>>8);
		frameArr.push(frameData.payloadLength&parseInt("1111"+"1111",2));
	}
	else if(frameData.payloadLength>parseInt("1111"+"1111"+"1111"+"1111",2)){
		frameArr.push(tmp+127);
		frameArr.push(frameData.payloadLength>>24);
		frameArr.push((frameData.payloadLength&parseInt("1111"+"1111"+"1111"+"1111"+"1111"+"1111",2))>>16);
		frameArr.push((frameData.payloadLength&parseInt("1111"+"1111"+"1111"+"1111",2))>>8);
		frameArr.push(frameData.payloadLength&parseInt("1111"+"1111",2));
	}
	if(frameData.mask!=0){
		for(i=0;i<4;i++){
			frameArr.push(frameData.maskingKey[i]);
		}
	}
	for(i=0;i<frameData.payloadLength;i++){
		tmp=frameData.payloadData[i];
		if(frameData.mask!=0)
			tmp=tmp&frameData.maskingKey[i%4];
		frameArr.push(tmp);
	}
	socket.write(new Buffer(frameArr));
}
/**
 * 广播
 */
function broadcastMsg(buff,noSocket){
	var key,socket;
	for(key in socketArr){
		socket=socketArr[key];
		if((socket==null)||(socket==noSocket))
			continue;
		sendWsMessage(socket,{
			"fin":1,
			"rsv1":0,
			"rsv2":0,
			"rsv3":0,
			"opcode":1,
			"mask":0,
			"payloadLength":buff.length,
			"payloadData":buff
		});
	}
}
//将socket从socketArr中删除
function removeSocket(socket){
	var key;
	for(key in socketArr){
		if(socket==socketArr[key])
			delete socketArr[key];
	}
}
/**
 * 处理浏览器发来的数据帧
 */
function socketSrv(socket,data){
	var i=-1,tmp,frameData={};
	tmp=data[++i];
	frameData.fin=((tmp&parseInt("1000"+"0000",2))==0)?0:1;
	frameData.rsv1=((tmp&parseInt("100"+"0000",2))==0)?0:1,
	frameData.rsv2=((tmp&parseInt("10"+"0000",2))==0)?0:1,
	frameData.rsv3=((tmp&parseInt("1"+"0000",2))==0)?0:1;
	frameData.opcode=tmp&parseInt("1111",2);
	tmp=data[++i];
	frameData.mask=((tmp&parseInt("1000"+"0000",2))==0)?0:1;
	frameData.payloadLength=tmp&parseInt("111"+"1111",2);
	if(frameData.payloadLength==126){
		frameData.payloadLength=data[++i]<<8;
		frameData.payloadLength+=data[++i];
	}
	else if(frameData.payloadLength==127){
		frameData.payloadLength=data[++i]<<24;
		frameData.payloadLength+=(data[++i]<<16);
		frameData.payloadLength+=(data[++i]<<8);
		frameData.payloadLength+=data[++i];
	}
	var j;
	if(frameData.mask!=0){
		frameData.maskingKey =new Buffer(4);
		for(j=0;j<4;j++){
			frameData.maskingKey[j]=data[++i];
		}
	}
	frameData.payloadData=[];
	for(j=0;j<frameData.payloadLength;j++){
		tmp=data[++i];
		frameData.payloadData.push(tmp^frameData.maskingKey[j%4]);
	}
	frameData.payloadData=new Buffer(frameData.payloadData);
	//console.log(frameData);
	//console.log(frameData.payloadData.toString());
	//文本帧
	if(frameData.opcode==1){
		broadcastMsg(frameData.payloadData,socket);
	}
	//close帧
	else if(frameData.opcode==8){
		removeSocket(socket);
		socket.end();
	}
	//ping帧
	else if(frameData.opcode==9){
		removeSocket(socket);
		sendWsMessage(socket,{
			"fin":1,
			"rsv1":0,
			"rsv2":0,
			"rsv3":0,
			"opcode":10,
			"mask":0,
			"payloadLength":frameData.payloadLength,
			"payloadData":frameData.payloadData
		});
	}<commit_msg>修复掩码的问题<commit_after>var http = require('http');
var crypto = require('crypto');
var url = require('url');
var path = require('path');
var fs = require('fs');

var srv = http.createServer();
var serverName="liuguang/ws",socketArr=[];

function sendWsMessage(socket,frameData){
	//console.log(frameData);
	//console.log(frameData.payloadData.toString());
	var frameArr=[],tmp,i;
	frameArr.push((frameData.fin<<7)+(frameData.rsv1<<6)+(frameData.rsv2<<5)+(frameData.rsv3<<4)+frameData.opcode);
	tmp=(frameData.mask<<7);
	if((frameData.payloadLength>=0)&&(frameData.payloadLength<=125)){
		frameArr.push(tmp+frameData.payloadLength);
	}
	else if(frameData.payloadLength<parseInt("1111"+"1111"+"1111"+"1111",2)){
		frameArr.push(tmp+126);
		frameArr.push(frameData.payloadLength>>8);
		frameArr.push(frameData.payloadLength&parseInt("1111"+"1111",2));
	}
	else if(frameData.payloadLength>parseInt("1111"+"1111"+"1111"+"1111",2)){
		frameArr.push(tmp+127);
		frameArr.push(frameData.payloadLength>>24);
		frameArr.push((frameData.payloadLength&parseInt("1111"+"1111"+"1111"+"1111"+"1111"+"1111",2))>>16);
		frameArr.push((frameData.payloadLength&parseInt("1111"+"1111"+"1111"+"1111",2))>>8);
		frameArr.push(frameData.payloadLength&parseInt("1111"+"1111",2));
	}
	if(frameData.mask!=0){
		for(i=0;i<4;i++){
			frameArr.push(frameData.maskingKey[i]);
		}
	}
	for(i=0;i<frameData.payloadLength;i++){
		tmp=frameData.payloadData[i];
		if(frameData.mask!=0)
			tmp=tmp&frameData.maskingKey[i%4];
		frameArr.push(tmp);
	}
	socket.write(new Buffer(frameArr));
}
/**
 * 广播
 */
function broadcastMsg(buff,noSocket){
	var key,socket;
	for(key in socketArr){
		socket=socketArr[key];
		if((socket==null)||(socket==noSocket))
			continue;
		sendWsMessage(socket,{
			"fin":1,
			"rsv1":0,
			"rsv2":0,
			"rsv3":0,
			"opcode":1,
			"mask":0,
			"payloadLength":buff.length,
			"payloadData":buff
		});
	}
}
//将socket从socketArr中删除
function removeSocket(socket){
	var key;
	for(key in socketArr){
		if(socket==socketArr[key])
			delete socketArr[key];
	}
}
/**
 * 处理浏览器发来的数据帧
 */
function socketSrv(socket,data){
	var i=-1,tmp,frameData={};
	tmp=data[++i];
	frameData.fin=((tmp&parseInt("1000"+"0000",2))==0)?0:1;
	frameData.rsv1=((tmp&parseInt("100"+"0000",2))==0)?0:1,
	frameData.rsv2=((tmp&parseInt("10"+"0000",2))==0)?0:1,
	frameData.rsv3=((tmp&parseInt("1"+"0000",2))==0)?0:1;
	frameData.opcode=tmp&parseInt("1111",2);
	tmp=data[++i];
	frameData.mask=((tmp&parseInt("1000"+"0000",2))==0)?0:1;
	frameData.payloadLength=tmp&parseInt("111"+"1111",2);
	if(frameData.payloadLength==126){
		frameData.payloadLength=data[++i]<<8;
		frameData.payloadLength+=data[++i];
	}
	else if(frameData.payloadLength==127){
		frameData.payloadLength=data[++i]<<24;
		frameData.payloadLength+=(data[++i]<<16);
		frameData.payloadLength+=(data[++i]<<8);
		frameData.payloadLength+=data[++i];
	}
	var j;
	if(frameData.mask!=0){
		frameData.maskingKey =new Buffer(4);
		for(j=0;j<4;j++){
			frameData.maskingKey[j]=data[++i];
		}
	}
	frameData.payloadData=[];
	for(j=0;j<frameData.payloadLength;j++){
		tmp=data[++i];
		/*有掩码*/
		if(frameData.mask!=0)
			frameData.payloadData.push(tmp^frameData.maskingKey[j%4]);
		else
			frameData.payloadData.push(tmp);
	}
	frameData.payloadData=new Buffer(frameData.payloadData);
	//console.log(frameData);
	//console.log(frameData.payloadData.toString());
	//文本帧
	if(frameData.opcode==1){
		broadcastMsg(frameData.payloadData,socket);
	}
	//close帧
	else if(frameData.opcode==8){
		removeSocket(socket);
		socket.end();
	}
	//ping帧
	else if(frameData.opcode==9){
		removeSocket(socket);
		sendWsMessage(socket,{
			"fin":1,
			"rsv1":0,
			"rsv2":0,
			"rsv3":0,
			"opcode":10,
			"mask":0,
			"payloadLength":frameData.payloadLength,
			"payloadData":frameData.payloadData
		});
	}
{'fraction_non_alphanumeric': 0.2211846318036286, 'fraction_numerical': 0.06283351120597652, 'mean_word_length': 6.809375, 'pattern_counts': {'":': 32, '<': 51, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4382297', 'n_tokens_mistral': 3382, 'n_tokens_neox': 3039, 'n_words': 290}