简体   繁体   English

Node.js Net TCP缓冲内存泄漏

[英]Node.js net tcp buffering memory leak

I'm writing a TCP game server in Node.js and am having issues with splitting the TCP stream into messages. 我在Node.js中编写一个TCP游戏服务器,但在将TCP流拆分为消息时遇到了问题。 As i want to read numbers and floats from the buffer i cannot find a suitable module to outsource to as all the ones i've found deal with simple strings ending with a new line delimiter. 因为我想读取数字并从缓冲区中浮出,所以我找不到合适的模块外包给我,因为我发现的所有模块都以简单的字符串开头,并以新的行定界符结尾。 I decided to go with prefixing each message with the length in bytes of the message. 我决定为每个消息加上消息字节的长度作为前缀。 I did this and wrote a simple program to spam the server with random messages ( well constructed with a UInt16LE prefix depicting the length of the message ). 我这样做了,并编写了一个简单的程序用随机消息向服务器发送垃圾邮件(使用表示消息长度的UInt16LE前缀构造良好)。 I noticed that the longer I leave the programs running my actual server keeps using up more and more memory. 我注意到,让程序运行我的实际服务器的时间越长,就会越来越消耗越来越多的内存。 I tried using a debugging tool to trace the memory allocation with no success so I figured i'd post my code here and hope for a reply. 我尝试使用调试工具来跟踪内存分配,但没有成功,因此我想将代码发布在这里,希望得到答复。 So here is my code... any tips or pointers as to where I'm going wrong or what I can do differently/more efficiently would be amazing! 所以这是我的代码...关于我要去哪里的错误或我可以做的不同/更有效的事情的任何提示或指示都将是惊人的! Thanks. 谢谢。

server.on("connection", function(socket) {
var session = new sessionCS(socket);
console.log("Connection from " + session.address);

// data buffering variables
var currentBuffer = new Buffer(args.bufSize);
var bufWrite = 0;
var bufRead = 0;
var mSize = null;
var i = 0;

socket.on("data", function(dataBuffer) {

    // check if buffer risk of overflow
    if (bufWrite + dataBuffer.length > args.bufSize-1) {
        var newBufWrite = 0;
        var newBuffer = new Buffer(args.bufSize);

        while(bufRead < bufWrite) {
            newBuffer[newBufWrite] = currentBuffer[bufRead];
            newBufWrite++;
            bufRead++;
        }

        currentBuffer = newBuffer;
        bufWrite = newBufWrite;
        bufRead = 0;
        newBufWrite = null;
    }

    // appending buffer
    for (i=0; i<dataBuffer.length; i++) {
        currentBuffer[bufWrite] = dataBuffer[i];
        bufWrite ++;
    }

    // if beginning of message not acknowleged
    if (mSize === null && (bufWrite - bufRead) >= 2) {
        mSize = currentBuffer.readUInt16LE(bufRead);
    }

    // if difference between read and write is greater or equal to message mSize + 2
    // +2 for the integer holding the message size
    // this means that a full message is in the buffer and needs to be extracted
    while ((bufWrite - bufRead) >= mSize+2) {
        bufRead += 2;
        var messageBuffer = new Buffer(mSize);

        for(i=0; i<messageBuffer.length; i++) {
            messageBuffer[i] = currentBuffer[bufRead];
            bufRead++;
        }

        // this is where the message buffer would be passed to the router
        router(session, messageBuffer);
        messageBuffer = null;

        // seeinf if another message length indicator is in the buffer
        if ((bufWrite - bufRead) >= 2) {
            mSize = currentBuffer.readUInt16LE(bufRead);
        }
        else {
            mSize = null;
        }
    }
});
}

缓冲区帧序列化协议(BUFSP) https://github.com/teambition/bufsp可能是您想要的:将消息编码为缓冲区,写入TCP,接收TCP流缓冲区并将其拆分为消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM