Class: UV::Loop

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Includes:
Assertions, Resource
Defined in:
lib/uv/loop.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from ClassMethods

create, default, new

Methods included from Resource

#check_result, #check_result!

Methods included from Assertions

#assert_arity, #assert_block, #assert_boolean, #assert_type

Constructor Details

#initialize(pointer) ⇒ Loop

Internal: Initialize a loop using an FFI::Pointer

Returns nothing



35
36
37
38
# File 'lib/uv/loop.rb', line 35

def initialize(pointer) # :notnew:
  @pointer = pointer
  @loop = self
end

Instance Method Details

#async(&block) ⇒ Object

Public: Get a new Async handle

Returns UV::Async

Raises ArgumentError if block is not given and is not expecting one argument exactly



233
234
235
236
237
238
239
240
241
242
# File 'lib/uv/loop.rb', line 233

def async(&block)
  assert_block(block)
  assert_arity(1, block)

  async_ptr = UV.create_handle(:uv_async)
  async     = Async.new(self, async_ptr, &block)

  check_result! UV.async_init(@pointer, async_ptr, async.callback(:on_async))
  async
end

#checkObject

Public: Get a new Check handle

Returns UV::Check



211
212
213
214
215
216
# File 'lib/uv/loop.rb', line 211

def check
  check_ptr = UV.create_handle(:uv_check)

  check_result! UV.check_init(@pointer, check_ptr)
  Check.new(self, check_ptr)
end

#fsObject

Public: Get a new Filesystem instance

Returns UV::Filesystem



265
266
267
# File 'lib/uv/loop.rb', line 265

def fs
  Filesystem.new(self)
end

#fs_event(path, &block) ⇒ Object

Public: Get a new FSEvent instance

Returns UV::FSEvent



272
273
274
275
276
277
278
279
280
281
# File 'lib/uv/loop.rb', line 272

def fs_event(path, &block)
  assert_block(block)
  assert_arity(3, block)

  fs_event_ptr = UV.create_handle(:uv_fs_event)
  fs_event     = FSEvent.new(self, fs_event_ptr, &block)

  check_result! UV.fs_event_init(@pointer, fs_event_ptr, path, fs_event.callback(:on_fs_event), 0)
  fs_event
end

#idleObject

Public: Get a new Idle handle

Returns UV::Handle



221
222
223
224
225
226
# File 'lib/uv/loop.rb', line 221

def idle
  idle_ptr = UV.create_handle(:uv_idle)

  check_result! UV.idle_init(@pointer, idle_ptr)
  Idle.new(self, idle_ptr)
end

#lookup_error(err) ⇒ Object

Internal: Get last error from the loop

Returns one of UV::Error or nil



125
126
127
128
129
130
131
132
# File 'lib/uv/loop.rb', line 125

def lookup_error(err)
  name = UV.err_name(err)
  msg  = UV.strerror(err)

  return nil if name == "OK"

  Error.const_get(name.to_sym).new(msg)
end

#nowObject

Public: Get current time in microseconds

Returns timestamp with microseconds as an Integer



118
119
120
# File 'lib/uv/loop.rb', line 118

def now
  UV.now(@pointer)
end

#pipe(ipc = false) ⇒ Object

Public: Get a new Pipe instance

ipc - Boolean wether handle will be used for ipc, useful for sharing tcp socket

between processes

Returns UV::Pipe



189
190
191
192
193
194
195
196
# File 'lib/uv/loop.rb', line 189

def pipe(ipc = false)
  assert_boolean(ipc)

  pipe_ptr = UV.create_handle(:uv_pipe)

  check_result! UV.pipe_init(@pointer, pipe_ptr, ipc ? 1 : 0)
  Pipe.new(self, pipe_ptr)
end

#prepareObject

Public: Get a new Prepare handle

Returns UV::Prepare



201
202
203
204
205
206
# File 'lib/uv/loop.rb', line 201

def prepare
  prepare_ptr = UV.create_handle(:uv_prepare)

  check_result! UV.prepare_init(@pointer, prepare_ptr)
  Prepare.new(self, prepare_ptr)
end

#run(run_type = :UV_RUN_DEFAULT) ⇒ Object

Public: Run the actual event loop. This method will block for the duration of event loop

Returns nothing.

Raises UV::Error::UNKNOWN Raises UV::Error::EOF Raises UV::Error::EADDRINFO Raises UV::Error::EACCES Raises UV::Error::EAGAIN Raises UV::Error::EADDRINUSE Raises UV::Error::EADDRNOTAVAIL Raises UV::Error::EAFNOSUPPORT Raises UV::Error::EALREADY Raises UV::Error::EBADF Raises UV::Error::EBUSY Raises UV::Error::ECONNABORTED Raises UV::Error::ECONNREFUSED Raises UV::Error::ECONNRESET Raises UV::Error::EDESTADDRREQ Raises UV::Error::EFAULT Raises UV::Error::EHOSTUNREACH Raises UV::Error::EINTR Raises UV::Error::EINVAL Raises UV::Error::EISCONN Raises UV::Error::EMFILE Raises UV::Error::EMSGSIZE Raises UV::Error::ENETDOWN Raises UV::Error::ENETUNREACH Raises UV::Error::ENFILE Raises UV::Error::ENOBUFS Raises UV::Error::ENOMEM Raises UV::Error::ENOTDIR Raises UV::Error::EISDIR Raises UV::Error::ENONET Raises UV::Error::ENOTCONN Raises UV::Error::ENOTSOCK Raises UV::Error::ENOTSUP Raises UV::Error::ENOENT Raises UV::Error::ENOSYS Raises UV::Error::EPIPE Raises UV::Error::EPROTO Raises UV::Error::EPROTONOSUPPORT Raises UV::Error::EPROTOTYPE Raises UV::Error::ETIMEDOUT Raises UV::Error::ECHARSE Raises UV::Error::EAIFAMNOSUPPORT Raises UV::Error::EAISERVICE Raises UV::Error::EAISOCKTYPE Raises UV::Error::ESHUTDOWN Raises UV::Error::EEXIST Raises UV::Error::ESRCH Raises UV::Error::ENAMETOOLONG Raises UV::Error::EPERM Raises UV::Error::ELOOP Raises UV::Error::EXDEV Raises UV::Error::ENOTEMPTY Raises UV::Error::ENOSPC



97
98
99
# File 'lib/uv/loop.rb', line 97

def run(run_type = :UV_RUN_DEFAULT)
  check_result! UV.run(@pointer, run_type)
end

#run_onceObject

Public: (Deprecated - use loop.run with a run_type specified) Runs outstanding events once, yields control back

Returns nothing



104
105
106
# File 'lib/uv/loop.rb', line 104

def run_once
  run(:UV_RUN_ONCE)
end

#tcpObject

Public: Get a new TCP instance

Returns UV::TCP instance



147
148
149
150
151
152
# File 'lib/uv/loop.rb', line 147

def tcp
  tcp_ptr = UV.create_handle(:uv_tcp)

  check_result! UV.tcp_init(@pointer, tcp_ptr)
  TCP.new(self, tcp_ptr)
end

#timerObject

Public: Get a new timer instance

Returns UV::Timer



137
138
139
140
141
142
# File 'lib/uv/loop.rb', line 137

def timer
  timer_ptr = UV.create_handle(:uv_timer)

  check_result! UV.timer_init(@pointer, timer_ptr)
  Timer.new(self, timer_ptr)
end

#to_ptrObject

Internal: Get a hold of internal loop pointer instance

Returns FFI::Pointer



286
287
288
# File 'lib/uv/loop.rb', line 286

def to_ptr
  @pointer
end

#tty(fileno, readable = false) ⇒ Object

Public: Get a new TTY instance

fileno - Integer file descriptor of a tty device. readable - Boolean wether TTY is readable or not

Returns UV::TTY

Raises ArgumentError if fileno argument is not an Integer Raises Argument error if readable is not a Boolean



173
174
175
176
177
178
179
180
181
# File 'lib/uv/loop.rb', line 173

def tty(fileno, readable = false)
  assert_type(Integer, fileno, "io#fileno must return an integer file descriptor, #{fileno.inspect} given")
  assert_boolean(readable)

  tty_ptr = UV.create_handle(:uv_tty)

  check_result! UV.tty_init(@pointer, tty_ptr, fileno, readable ? 1 : 0)
  TTY.new(self, tty_ptr)
end

#udpObject

Public: Get a new UDP instance

Returns UV::UDP instance



157
158
159
160
161
162
# File 'lib/uv/loop.rb', line 157

def udp
  udp_ptr = UV.create_handle(:uv_udp)

  check_result! UV.udp_init(@pointer, udp_ptr)
  UV::UDP.new(self, udp_ptr)
end

#update_timeObject

Public: forces loop time update, useful for getting more granular times

Returns nothing



111
112
113
# File 'lib/uv/loop.rb', line 111

def update_time
  UV.update_time(@pointer)
end

#work(callback = nil, op = nil, &block) ⇒ Object

Public: Do some work in the libuv thread pool

Returns UV::Work

Raises ArgumentError if block is not given and if the block or optional callback are expecting any arguments



249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/uv/loop.rb', line 249

def work(callback = nil, op = nil, &block)
  block = block || op
  assert_block(block)
  assert_arity(0, block)

  if not callback.nil?
    assert_block(callback)
    assert_arity(0, callback)
  end

  work = Work.new(self, block, callback)
end