Sha256: 37b518fbb70478192e115d345d286d5934c013d3e9e6a8eab1006c74280f92bc

Contents?: true

Size: 839 Bytes

Versions: 1

Compression:

Stored size: 839 Bytes

Contents

#! /usr/bin/env ruby

require 'epoll'
require 'socket'

server = TCPServer.open(4000)
puts "run http://127.0.0.1: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("")

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
epoll-0.3.0 examples/epoll_server.rb