Sha256: bd8a661063dafc0155fa508dbedcda89a18f2d34347651aadbf2bb37a9499fd6

Contents?: true

Size: 827 Bytes

Versions: 1

Compression:

Stored size: 827 Bytes

Contents

#! /usr/bin/env ruby

require 'io/epoll'
require 'socket'

server = TCPServer.open(4000)

response = [
  "HTTP/1.0 200 OK\r\n",
  "Content-Length: 5\r\n",
  "Content-Type: text/html\r\n",
  "\r\n",
  "HELLO\r\n",
].join("")

Epoll = IO::Epoll
ep = Epoll.create
ep.add server, Epoll::IN

Signal.trap(:INT) {
  ep.close
  server.close
}

loop do
  ep.wait.each do |ev|
    data = ev.data
    events = ev.events

    if data == server
      socket = server.accept
      ep.add socket, Epoll::IN|Epoll::ET
    elsif (events & Epoll::IN) != 0
      data.recv(1024)
      ep.mod data, Epoll::OUT|Epoll::ET
    elsif (events & Epoll::OUT) != 0
      data.puts response
      ep.del data
      data.close
    elsif (events & (Epoll::HUP|Epoll::ERR)) != 0
      p "Epoll::HUP|Epoll::ERR"
    else
      raise IOError
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
io-epoll-0.1.0 examples/epoll_server.rb