Nodejs net 接受包 並解碼,第一次使用了 protobuf

第一次使用 net 模塊的 buffer 類型

對 buffer.copy 開始不瞭解 走了彎路,調用的對象是 sourece 一直以爲是 dest

對包進行分割 包的結構爲 :包內容長度[byte0 byte1] 包內容(protobuf arry)[byte2,byte3 ...],客戶端使用 libevent 做

// 將net模塊 引入進來
var net = require("net");
var path = require("path");
var protoBufJs=require("protobufjs");
var pbRoot = protoBufJs.loadSync(path.join(__dirname,"./pb/elabdata.proto"));
var eLabMessageType = pbRoot.lookupType("ELabMessage");

//創建一個cli 用於輸出
var cli={};

//解碼接收到的消息
function DecodeNetMsg(sock,msg)
{
	if(msg.netcmd==0)
	{
		if(cli[msg.account]!=undefined)
		{
			cli[msg.account].write("GameOver");
			cli[msg.account].end();
			delete cli[msg.account];
		}
		cli[msg.account]=sock;
		sock.room=msg.account;
		console.log(msg.account+"is login!");
	}
}

// 創建一個net.Server用來監聽,當連接進來的時候,就會調用我們的函數
// client_sock,就是我們的與客戶端通訊建立連接配對的socket
// client_sock 就是與客戶端通訊的net.Socket
var server = net.createServer(function(client_sock) { 
	console.log("client comming", client_sock.remoteAddress, client_sock.remotePort);
	// 設置你接受的格式, 
	// client_sock.setEncoding("utf8");
	// client_sock.setEncoding("hex"); // 轉成二進制的文本編碼
	// 
	// 客戶端斷開連接的時候處理,用戶斷線離開了
	client_sock.on("close", function() {
		console.log("close socket");
	});
	// 接收到客戶端的數據,調用這個函數
	// data 默認是Buffer對象,如果你強制設置爲utf8,那麼底層會先轉換成utf8的字符串,傳給你
	// hex 底層會把這個Buffer對象轉成二進制字符串傳給你
	// 如果你沒有設置任何編碼 <Buffer 48 65 6c 6c 6f 57 6f 72 6c 64 21>
	// utf8 --> HelloWorld!!!   hex--> "48656c6c6f576f726c6421"
	client_sock.on("data", function(data) {
		var readpos=0;
		if(client_sock.recbuf==undefined)
		{	
			client_sock.recbuf=Buffer.alloc(2048,0);
			client_sock.recsize=0;
		}
		do{
			//包分割
			var readsize;
			readsize=data.length-readpos;
			if(readsize>client_sock.recbuf.length-client_sock.recbuf.recsize)	readsize=client_sock.recbuf.length-client_sock.recbuf.recsize;
			data.copy(client_sock.recbuf,client_sock.recpos,readpos,readpos+readsize);
			readpos+=readsize;
			client_sock.recsize+=readsize;
			do{
				var len=client_sock.recbuf[0]+(client_sock.recbuf[1]<<8);
				if(len+2<=client_sock.recsize)
				{
					var tmp=Buffer.alloc(len);
					client_sock.recbuf.copy(tmp,0,2,2+len);
					var msg=eLabMessageType.decode(tmp);
					DecodeNetMsg(client_sock,msg);
					client_sock.recbuf.copy(client_sock.recbuf,0,len+2,client_sock.recsize);
					client_sock.recsize-=len+2;
				}
				else break;
			}while(1);
		}while(data.length!=readpos);
	});
	
	client_sock.on("error", function(err) {
		console.log("error", err);
	});
});
 
// 當我開始監聽的時候就會調用這個回掉函數
server.on("listening", function() {
	console.log("start listening...");
});
 
 
// 監聽發生錯誤的時候調用
server.on("error", function() {
	console.log("listen error");
});
 
server.on("close", function() {
	console.log("server stop listener");
});
/*
server.on("connection", function(client_sock) {
	console.log("client comming 22222");
});
*/
// 編寫代碼,指示這個server監聽到哪個端口上面。
// 127.0.0.1: 6080
// node就會來監聽我們的server,等待連接接入
server.listen({
	port: 1920,
    //host: "127.0.0.1",
    host: "192.168.183.1",
	exclusive: true,
});
 
// 停止node對server的監聽事件處理,那麼node就沒有其他的事件要處理,所以就退出了。
// server.unref(); // 取消node,對server的事件的監聽;
// server.close(); // 主動的掉這個server.close纔會觸發這個net.Server的close事件
module.exports=cli;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章