kgio RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <normalperson@yhbt.net>
To: kgio@librelist.org
Subject: Re: Using Kgio::TCPSocket.start with EventMachine (in non blocking way)
Date: Mon, 9 Apr 2012 03:17:44 +0000	[thread overview]
Message-ID: <20120409031744.GB24089@dcvr.yhbt.net> (raw)
In-Reply-To: CALiegfnw_-4407ZqiwsCpwjVU6mv47xgptMCrCbW+01krXAbgg@mail.gmail.com

Iñaki Baz Castillo <ibc@aliax.net> wrote:
> Now the first issue is: how to do a non-blocking TCP connection
> (needed within EM environment)? So I found
> "Kgio::TCPSocket.start('127.0.0.1', 80)":

> I basically need a callback "on_connected" to be called when such a
> TCP connection is completed, but honestly I have no idea since it
> seems to require using select/poll (at Ruby level) working on top of a
> EventMachine code, really complex IMHO. Or maybe I miss something.

Kgio::TCPSocket.start is correct.  I'm not sure what you need the
"on_connected" callback for, but you can just do whatever you want
to do on the initial write.  You can keep track of the initial write
vs subsequent writes with an instance variable.

Just try writing, and if you get :wait_writable, tell EM to have it
watch for writability somehow.


  sock = Kgio::TCPSocket.start(...)
  buf = "TEST"
  case buf = sock.kgio_trywrite(buf)
  when :wait_writable
    # Tell EM to wait for sock to be writable
    # It's been a while since I've used EM
    # so I'm not familiar with the API anymore.
  when nil
    # initial write completed

    # otherwise, keep looping if we have an unwritten string
    # (we'll probably hit :wait_writable at the next iteration)
  end while String === buf

  parent reply	other threads:[~2012-04-09  3:18 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-06 14:47 Using Kgio::TCPSocket.start with EventMachine (in non blocking way) Iñaki Baz Castillo
2012-04-06 15:10 ` Iñaki Baz Castillo
2012-04-09  3:17 ` Eric Wong [this message]
2012-04-09 18:12   ` Iñaki Baz Castillo
2012-04-10 12:35     ` Iñaki Baz Castillo
2012-04-10 20:09       ` Eric Wong
2012-04-10 20:56         ` Iñaki Baz Castillo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/kgio/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120409031744.GB24089@dcvr.yhbt.net \
    --to=normalperson@yhbt.net \
    --cc=kgio@librelist.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/kgio.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).