Module: Networking
- Defined in:
- lib/networking.rb,
lib/networking/tcp.rb,
lib/networking/version.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"1.2.0"
Class Method Summary collapse
-
.read_from_stream(stream) ⇒ Object
Returns pair [status(true/false), obj].
-
.write_to_stream(stream, obj) ⇒ Object
TODO(kolman): To get robustness, just use try catch + return 0 bytes on write + return false on status on read.
Class Method Details
.read_from_stream(stream) ⇒ Object
Returns pair [status(true/false), obj]
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/networking/tcp.rb', line 34 def Networking.read_from_stream(stream) Log.debug3('Read from stream.') begin return [false, nil] unless size_of_data = stream.read(4) size_of_data = size_of_data.unpack("l")[0] Log.debug2("Reading data size:%s", size_of_data) data = stream.read(size_of_data) rescue Exception => e Log.warning("Could not read tcp/ip stream, #{e.to_s}.") begin stream.close() rescue IOError => e Log.warning("Could not close stream, #{e.to_s}.") end return [false, nil] end unmarshalled_data = Marshal.load(data) Log.debug2('Read good.') return [true, unmarshalled_data] end |
.write_to_stream(stream, obj) ⇒ Object
TODO(kolman): To get robustness, just use try catch + return 0 bytes on write + return false on status on read.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/networking/tcp.rb', line 10 def Networking.write_to_stream(stream, obj) Log.debug3('Writing to stream.') marshal_data = Marshal.dump(obj) Log.debug2("Writing data size: %s", marshal_data.length) data_size = [marshal_data.length].pack("l") if data_size.nil? || marshal_data.nil? Log.debug2('Send data size is nil!') end begin bytes_written = stream.write data_size bytes_written += stream.write marshal_data return bytes_written rescue Exception => e Log.warning("Could not write tcp/ip stream, #{e.to_s}") begin stream.close() rescue IOError => e Log.warning("Could not close stream, #{e.to_s}.") end return 0 end end |