我使用node js的net server,使用socket.on('data')函数接收数据。为了解析 TCP 消息,我使用解析缓冲区方法。这使用前 4 个字节作为 TCP 消息的长度,以便我可以从 TCP 流中读取并形成单独的命令。总而言之,高负载时会发生一些垃圾数据作为 TCP 流的一部分返回,这会导致问题。
function onConnect(client) {
var accumulatingBuffer = new Buffer(0);
var totalPacketLen = -1;
var accumulatingLen = 0;
var recvedThisTimeLen = 0;
client.on('data', function (data) {
parseBuffer(client, data, accumulatingBuffer, totalPacketLen, accumulatingLen, recvedThisTimeLen);
});
}
这里是 parsebuffer 方法。
function parseBuffer(client, data, accumulatingBuffer, totalPacketLen, accumulatingLen, recvedThisTimeLen) {
recvedThisTimeLen = Buffer.byteLength(data);
var tmpBuffer = new Buffer(accumulatingLen + recvedThisTimeLen);
accumulatingBuffer.copy(tmpBuffer);
data.copy(tmpBuffer, accumulatingLen); // offset for accumulating
accumulatingBuffer = tmpBuffer;
tmpBuffer = null;
accumulatingLen = accumulatingLen + recvedThisTimeLen;
if (accumulatingLen < PACKETHEADERLEN) {
return;
} else if (accumulatingLen === PACKETHEADERLEN) {
packetHeaderLen
return;
} else {
//a packet info is available..
if (totalPacketLen < 0) {
totalPacketLen = accumulatingBuffer.readUInt32BE(0);
}
}
while (accumulatingLen >= totalPacketLen + PACKETHEADERLEN) {
var aPacketBufExceptHeader = new Buffer(totalPacketLen); // a whole packet is available...
accumulatingBuffer.copy(aPacketBufExceptHeader, 0, PACKETHEADERLEN, PACKETHEADERLEN + totalPacketLen);
////////////////////////////////////////////////////////////////////
//process packet data
var stringData = aPacketBufExceptHeader.toString();
try {
var JSONObject = JSON.parse(stringData);
handler(client, JSONObject);
var newBufRebuild = new Buffer(accumulatingBuffer.length - (totalPacketLen + PACKETHEADERLEN)); // we can reduce size of allocatin
accumulatingBuffer.copy(newBufRebuild, 0, totalPacketLen + PACKETHEADERLEN, accumulatingBuffer.length);
//init
accumulatingLen = accumulatingLen - (totalPacketLen + PACKETHEADERLEN); //totalPacketLen+4
accumulatingBuffer = newBufRebuild;
newBufRebuild = null;
totalPacketLen = -1;
//For a case in which multiple packets are transmitted at once.
if (accumulatingLen <= PACKETHEADERLEN) {
//need to get more data -> wait..
return;
} else {
totalPacketLen = accumulatingBuffer.readUInt32BE(0);
}
} catch (ex) {
console.log(ex + ' unable to process data');
return;
}
}
}
一切都很好,直到使用大量客户端快速发送消息的高模拟负载。在 ParseBuffer 方法内的那个时间点,第一行“data.length”返回的长度超过了 TCP 数据的长度。这会导致代码将垃圾读取为 UInt32BE,这会导致 totalpacketlength 的值非常高(它告诉下一个数据包长度)。这会导致消息丢失。我错过了什么吗?请帮忙。
最佳答案
当您在 parseBuffer() 函数中执行此操作时:
accumulatingBuffer = tmpBuffer;
这只是将 tmpBuffer 分配给名为 accumulatingBuffer 的函数参数。它不会更改 onConnect() 方法中的 accumulatingBuffer 变量。因此,当您获得部分缓冲区时,您会丢失累积的部分。您传递给 parseBuffer() 的其他参数也存在同样的问题。在 parseBuffer() 中分配给它们不会更改 onConnect() 中的同名变量。
可能有更简单的方法来编写它,但保持相同结构的最简单方法是不传递单个变量,而是传递具有这些变量作为对象属性的单个对象。然后,当您分配给属性时,您可以从 onConnect() 中获取这些新值。
一般结构如下所示:
function onConnect(client) {
var args = {};
args.accumulatingBuffer = new Buffer(0);
args.totalPacketLen = -1;
args.accumulatingLen = 0;
args.recvedThisTimeLen = 0;
client.on('data', function (data) {
parseBuffer(client, data, args);
});
}
然后,在 parseBuffer() 中进行相应的更改,以将参数作为 args 对象的属性进行访问。由于对象是通过指针传递的,因此当您从 parseBuffer 中分配给 args 对象的属性时,这些属性将在 onConnect 方法的 args 对象中可见。
仅供引用,我没有遵循函数其他地方的整个逻辑,因此也可能存在其他错误。这段代码看起来相当复杂,因为它尝试执行的相当常见的任务有很多缓冲区副本。这也是一种可能已经编写过很多次的代码,甚至可能存在于一些预构建的库中。
关于node.js - Node js TCP 服务器,socket.on ('data' ) - 数据缓冲区包含高负载时的垃圾数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31078956/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="