Sha256: 3bc30a2695018b399ea9da89f60afb3f42b528c677c7229c755a32431ff39657

Contents?: true

Size: 1.25 KB

Versions: 17

Compression:

Stored size: 1.25 KB

Contents

# http://www.mudynamics.com
# http://labs.mudynamics.com
# http://www.pcapr.net

module Mu
class Pcap

# For emulating of a pair of connected sockets. Bytes written 
# with #write to one side are returned by a subsequent #read on 
# the other side.
#
# Use Pair.stream_pair to get a pair with stream semantics.
# Use Pair.packet_pair to get a pair with packet semantics.
class IOPair
    attr_reader :read_queue
    attr_accessor :other

    def initialize
        raise NotImplementedError
    end

    def self.stream_pair
        io1 = Stream.new
        io2 = Stream.new
        io1.other = io2
        io2.other = io1
        return io1, io2
    end

    def self.packet_pair
        io1 = Packet.new
        io2 = Packet.new
        io1.other = io2
        io2.other = io1
        return io1, io2
    end

    def write bytes
        @other.read_queue << bytes
        bytes.size
    end

    class Stream < IOPair
        def initialize 
            @read_queue = ""
        end

        def read n=nil
            n ||= @read_queue.size
            @read_queue.slice!(0,n)
        end
    end

    class Packet < IOPair
        def initialize 
            @read_queue = []
        end

        def read 
            @read_queue.shift
        end
    end

end
end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
DIY-pcap-0.4.1 lib/diy/parser/mu/pcap/io_pair.rb
DIY-pcap-0.3.8 lib/diy/parser/mu/pcap/io_pair.rb
DIY-pcap-0.3.6 lib/diy/parser/mu/pcap/io_pair.rb
DIY-pcap-0.3.5 lib/diy/parser/mu/pcap/io_pair.rb
DIY-pcap-0.3.4 lib/diy/parser/mu/pcap/io_pair.rb
DIY-pcap-0.3.3 lib/diy/parser/mu/pcap/io_pair.rb
DIY-pcap-0.3.2 lib/diy/parser/mu/pcap/io_pair.rb
DIY-pcap-0.3.1 lib/diy/parser/mu/pcap/io_pair.rb
DIY-pcap-0.3.0 lib/diy/parser/mu/pcap/io_pair.rb
DIY-pcap-0.2.8 lib/diy/parser/mu/pcap/io_pair.rb
DIY-pcap-0.2.7 lib/diy/parser/mu/pcap/io_pair.rb
DIY-pcap-0.2.6 lib/diy/parser/mu/pcap/io_pair.rb
pcapr-local-0.2.0 lib/mu/pcap/io_pair.rb
pcapr-local-0.1.13 lib/mu/pcap/io_pair.rb
pcapr-local-0.1.12 lib/mu/pcap/io_pair.rb
pcapr-local-0.1.11 lib/mu/pcap/io_pair.rb
pcapr-local-0.1.10 lib/mu/pcap/io_pair.rb