0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

mrubyでEchoServerをつくってみる

Last updated at Posted at 2017-02-12

https://docs.ruby-lang.org/ja/latest/class/TCPServer.html のecho serverのサンプルをmrubyで動かしてみました。

経緯

ThreadなしでIOの並列処理やる方法について調べてたところ、selectやpollを使うということを知りました。
mrubyで試そうと思い、まずmruby-socketのTCPServerがCRubyのTCPServerと同じように使えるのか調べようとしたところ、Rubyリファレンスマニュアルにいきなりやりたいことそのままなサンプルコードが載ってたのでmrubyで動くか試してみました。

mgemの設定

mgemでmruby-ioとmruby-socketを追加します。

conf.gem :mgem => 'mruby-io'
conf.gem :mgem => 'mruby-socket'

ソースコード

mruby-ioを追加してもCRubyのようにKernelにselectが追加されるわけではないので、IO.selectで呼び出しています。
その他、好みでputs<<を使っている以外はリファレンスマニュアルのコードそのままです。

#
# echo_server.rb
# inspired from https://docs.ruby-lang.org/ja/latest/class/TCPServer.html
#
gs = TCPServer.open("127.0.0.1", 12345)
socks = [gs]
addr = gs.addr
addr.shift
puts "server is on #{addr.join(":")}"

loop do
  nsock = IO.select(socks)
  next if nsock == nil
  for s in nsock[0]
    if s == gs
      socks << s.accept
      puts "#{s} is accepted"
    else
      if s.eof?
        puts "#{s} is gone"
        socks.delete(s)
      else
        str = s.gets
        puts "#{s}: #{str}"
        s.write(str)
      end
    end
  end
end

使ってみる

./path/to/mruby echo_server.rb

で起動後、telnetからアクセスしてみます

$ telnet 127.0.0.1 12345
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
vn9arh9ahv9pfbhp0dzb dzb o
vn9arh9ahv9pfbhp0dzb dzb o
Hello, mruby!
Hello, mruby!

地味ですね。

IO.selectが使えるクラスはどれ?

IOを継承しているクラスなら使えると思うんですが、mruby-socketのソースコード追えなかったのでmirbで確かめてみました(というかrubyなら先にそれ試すか普通・・・)

$ ./mruby/bin/mirb 
mirb - Embeddable Interactive Ruby Shell

> TCPServer
 => TCPServer
> TCPServer.class
 => Class
> TCPServer.superclass
 => TCPSocket
> TCPSocket.superclass
 => IPSocket
> IPSocket.superclass
 => BasicSocket
> BasicSocket.superclass
 => IO

TCPServerばっちりIOのサブクラスのようです。

その他参考にしたページ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?