Sha256: b15fef75e5769baf1d3a63b02397b89c721fc3eaaa2bc945a19dfb95cf8c164f

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require_relative '../lib/uringmachine'

PORT = 1234

@machine = UM.new
@bgid = @machine.setup_buffer_ring(4096, 1024)

@counter = 0

def handle_connection(fd)
  buf = +''
  loop do
    res = @machine.recv(fd, buf, 8192, 0)
    break if res == 0

    @machine.write(fd, buf)
    @counter += 2
  end
ensure
  @machine.close(fd) rescue nil
end

def run_client
  fd = @machine.socket(UM::AF_INET, UM::SOCK_STREAM, 0, 0)
  @machine.connect(fd, '127.0.0.1', PORT)
  msg = 'foo' * 30
  buf = +''
  loop do
    @machine.send(fd, msg, msg.bytesize, 0)
    res = @machine.recv(fd, buf, 8192, 0)
    @counter += 2
    
    break if res == 0
    raise "Got #{res} bytes instead of #{msg.bytesize}" if res != msg.bytesize
  end
end

trap('SIGINT') { exit }

server_fd = @machine.socket(UM::AF_INET, UM::SOCK_STREAM, 0, 0)
@machine.setsockopt(server_fd, UM::SOL_SOCKET, UM::SO_REUSEADDR, true)
@machine.bind(server_fd, '127.0.0.1', PORT)
@machine.listen(server_fd, UM::SOMAXCONN)
puts "Listening on port #{PORT}"

at_exit { @machine.close(server_fd) rescue nil }

20.times do
  @machine.spin { run_client }
end

@machine.spin do
  @machine.accept_each(server_fd) do |fd|
    @machine.spin(fd) { handle_connection _1 }
  end
end

t0 = Time.now
@machine.sleep 3
t1 = Time.now  
elapsed = t1 - t0
puts "Did #{@counter} ops in #{elapsed} seconds (#{(@counter / elapsed)} ops/s)"

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
uringmachine-0.5.1 examples/server_client.rb
uringmachine-0.5 examples/server_client.rb