javascript - Node.js TCP server incoming buffer -


i have 2 node processes speak each other. call them [node server] , [node sender]. [node sender] continually processes information , writes message on tcp connection [node server]. [node server] writes status message.

example of [node sender]:

var message = "test message"; [node sender].connection.write(message); 

example of [node server]:

[node server].socket.on("data", function(p_data) {     this.write("ok");      // work p_data } 

this works without issue, p_data contains "test message" when sent @ above 5 milliseconds. however, if speed [node sender] write every millisecond, p_data ends "test messagetest messagetes".

i understand buffer in [node sender] filling faster write command sending it. there way force one-to-one ratio in sending messages while still remaining asynchronous?

i can add terminator message , fill buffer in [node server], wanted make sure there wasn't obvious missing.

no, you're not missing , yes, need add form of termination messages.

there 2 basic problems here:

  1. the tcp protocol stream-oriented, not message-oriented; has no intrinsic knowledge of might constitute "message".

  2. the data event fired node.js net library indicates some data has arrived without idea of message might contain, cannot indicate has received specific data.

so sending messages faster node can process them, socket recv buffer fills multiple "messages".

a typical solution problem add line-termination, can found in https://github.com/baudehlo/haraka/blob/master/connection.js @ lines 32-34:

self.client.on('data', function (data) {     self.process_data(data); }); 

and lines 110-134:

connection.prototype.process_data = function (data) {   if (this.disconnected) {     logger.logwarn("data after disconnect " + this.remote_ip);     return;   }    this.current_data += data;   this._process_data(); };  connection.prototype._process_data = function() {   var results;   while (results = line_regexp.exec(this.current_data)) {     var this_line = results[1];     if (this.state === 'pause') {         this.early_talker = 1;         var self = this;         // if talk early, we're going give delay         settimeout(function() { self._process_data() }, this.early_talker_delay);         break;     }     this.current_data = this.current_data.slice(this_line.length);     this.process_line(this_line);   } }; 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -