Sha256: ebbeaefd23978e0a03bfb82bdca39b890f2e6ff91bac526c9bb1840833ed5a4c
Contents?: true
Size: 750 Bytes
Versions: 2
Compression:
Stored size: 750 Bytes
Contents
# -*- coding: utf-8 -*- # Copyright (C) 2011 Rocky Bernstein <rockyb@rubyforge.net> # Subsidiary routines used to "pack" and "unpack" TCP messages. module Trepanning module TCPPacking TCP_MAX_PACKET = 8192 # Largest size for a recv LOG_MAX_MSG = Math.log10(TCP_MAX_PACKET).ceil def pack_msg(msg) fmt = '%%%dd' % LOG_MAX_MSG # A funny way of writing: '%4d' (fmt % msg.size) + msg end def unpack_msg(buf) length = Integer(buf[0...LOG_MAX_MSG]) data = buf[LOG_MAX_MSG..LOG_MAX_MSG+length] buf = buf[LOG_MAX_MSG+length..-1] [buf, data] end end end # Demo if __FILE__ == $0 include Trepanning::TCPPacking msg = "Hi there!" puts unpack_msg(pack_msg(msg))[1] == msg end
Version data entries
2 entries across 2 versions & 2 rubygems
Version | Path |
---|---|
rbx-trepanning-0.0.4-universal-rubinius-1.2 | io/tcpfns.rb |
trepanning-0.1.0 | io/tcpfns.rb |