v0.4: ipc_wait_event* functions now reveive a timeout value
This commit is contained in:
parent
9793d5ac6a
commit
5b134a8c1b
@ -93,6 +93,8 @@ class IPC::StandAloneConnection
|
|||||||
end
|
end
|
||||||
|
|
||||||
class IPC::Connections
|
class IPC::Connections
|
||||||
|
property base_timer : Int64 = 0
|
||||||
|
property timer : Int64 = 0
|
||||||
getter connections : LibIPC::Connections
|
getter connections : LibIPC::Connections
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@ -143,27 +145,42 @@ class IPC::Connections
|
|||||||
serverp = server.pointer
|
serverp = server.pointer
|
||||||
end
|
end
|
||||||
|
|
||||||
r = LibIPC.ipc_wait_event self.pointer, serverp, pointerof(event)
|
r = LibIPC.ipc_wait_event self.pointer, serverp, pointerof(event), pointerof(@timer)
|
||||||
if r != 0
|
if r != 0
|
||||||
m = String.new LibIPC.ipc_errors_get (r)
|
m = String.new LibIPC.ipc_errors_get (r)
|
||||||
yield IPC::Exception.new "error waiting for a new event: #{m}"
|
yield IPC::Exception.new "error waiting for a new event: #{m}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
eventtype = event.type.unsafe_as(LibIPC::EventType)
|
||||||
|
|
||||||
|
# if event type is Timer, there is no connection nor message
|
||||||
|
case eventtype
|
||||||
|
when LibIPC::EventType::Timer
|
||||||
|
return eventtype, nil, nil
|
||||||
|
end
|
||||||
|
|
||||||
connection = IPC::Connection.new event.origin.unsafe_as(Pointer(LibIPC::Connection)).value
|
connection = IPC::Connection.new event.origin.unsafe_as(Pointer(LibIPC::Connection)).value
|
||||||
|
|
||||||
eventtype = event.type.unsafe_as(LibIPC::EventType)
|
|
||||||
message = event.message.unsafe_as(Pointer(LibIPC::Message))
|
message = event.message.unsafe_as(Pointer(LibIPC::Message))
|
||||||
|
|
||||||
return eventtype, IPC::Message.new(message), connection
|
return eventtype, IPC::Message.new(message), connection
|
||||||
end
|
end
|
||||||
|
|
||||||
def loop(server : IPC::Connection | IPC::Server | ::Nil, &block : Proc(Events|Exception, Nil))
|
def loop(server : IPC::Connection | IPC::Server | ::Nil, &block : Proc(Events|Exception, Nil))
|
||||||
|
if @base_timer > 0 && @timer == 0
|
||||||
|
@timer = @base_timer
|
||||||
|
end
|
||||||
|
|
||||||
::loop do
|
::loop do
|
||||||
type, message, connection = wait_event server, &block
|
type, message, connection = wait_event server, &block
|
||||||
|
|
||||||
case type
|
case type
|
||||||
|
when LibIPC::EventType::Timer
|
||||||
|
# reset timer
|
||||||
|
@timer = @base_timer
|
||||||
|
yield IPC::Event::Timer.new
|
||||||
when LibIPC::EventType::Connection
|
when LibIPC::EventType::Connection
|
||||||
yield IPC::Event::Connection.new connection
|
yield IPC::Event::Connection.new connection.not_nil!
|
||||||
|
|
||||||
when LibIPC::EventType::NotSet
|
when LibIPC::EventType::NotSet
|
||||||
yield IPC::Exception.new "even type not set"
|
yield IPC::Exception.new "even type not set"
|
||||||
@ -172,13 +189,13 @@ class IPC::Connections
|
|||||||
yield IPC::Exception.new "even type indicates an error"
|
yield IPC::Exception.new "even type indicates an error"
|
||||||
|
|
||||||
when LibIPC::EventType::ExtraSocket
|
when LibIPC::EventType::ExtraSocket
|
||||||
yield IPC::Event::ExtraSocket.new message, connection
|
yield IPC::Event::ExtraSocket.new message.not_nil!, connection.not_nil!
|
||||||
|
|
||||||
when LibIPC::EventType::Switch
|
when LibIPC::EventType::Switch
|
||||||
yield IPC::Event::Switch.new message, connection
|
yield IPC::Event::Switch.new message.not_nil!, connection.not_nil!
|
||||||
|
|
||||||
when LibIPC::EventType::Message
|
when LibIPC::EventType::Message
|
||||||
yield IPC::Event::Message.new message, connection
|
yield IPC::Event::Message.new message.not_nil!, connection.not_nil!
|
||||||
|
|
||||||
# for now, the libipc does not provide lookup events
|
# for now, the libipc does not provide lookup events
|
||||||
# networkd uses a simple LibIPC::EventType::Message
|
# networkd uses a simple LibIPC::EventType::Message
|
||||||
@ -186,7 +203,7 @@ class IPC::Connections
|
|||||||
# yield IPC::Event::LookUp.new message, connection
|
# yield IPC::Event::LookUp.new message, connection
|
||||||
|
|
||||||
when LibIPC::EventType::Disconnection
|
when LibIPC::EventType::Disconnection
|
||||||
yield IPC::Event::Disconnection.new connection
|
yield IPC::Event::Disconnection.new connection.not_nil!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -3,6 +3,9 @@ require "./message"
|
|||||||
require "./connection"
|
require "./connection"
|
||||||
|
|
||||||
class IPC::Event
|
class IPC::Event
|
||||||
|
class Timer
|
||||||
|
end
|
||||||
|
|
||||||
class Connection
|
class Connection
|
||||||
getter connection : IPC::Connection
|
getter connection : IPC::Connection
|
||||||
def initialize(@connection)
|
def initialize(@connection)
|
||||||
@ -32,5 +35,5 @@ class IPC::Event
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
alias Events = IPC::Event::Connection | IPC::Event::Disconnection | IPC::Event::Message | IPC::Event::ExtraSocket | IPC::Event::Switch | IPC::Event::LookUp
|
alias Events = IPC::Event::Timer | IPC::Event::Connection | IPC::Event::Disconnection | IPC::Event::Message | IPC::Event::ExtraSocket | IPC::Event::Switch | IPC::Event::LookUp
|
||||||
|
|
||||||
|
@ -111,6 +111,7 @@ lib LibIPC
|
|||||||
Disconnection
|
Disconnection
|
||||||
Message
|
Message
|
||||||
LookUp
|
LookUp
|
||||||
|
Timer
|
||||||
end
|
end
|
||||||
|
|
||||||
struct Event
|
struct Event
|
||||||
@ -129,7 +130,7 @@ lib LibIPC
|
|||||||
fun ipc_read(Connection*, Message*) : LibC::Int
|
fun ipc_read(Connection*, Message*) : LibC::Int
|
||||||
fun ipc_write(Connection*, Message*) : LibC::Int
|
fun ipc_write(Connection*, Message*) : LibC::Int
|
||||||
|
|
||||||
fun ipc_wait_event(Connections*, Connection*, Event*) : LibC::Int
|
fun ipc_wait_event(Connections*, Connection*, Event*, LibC::Long*) : LibC::Int
|
||||||
|
|
||||||
fun ipc_add(Connections*, Connection*) : LibC::Int
|
fun ipc_add(Connections*, Connection*) : LibC::Int
|
||||||
fun ipc_del(Connections*, Connection*) : LibC::Int
|
fun ipc_del(Connections*, Connection*) : LibC::Int
|
||||||
@ -148,7 +149,7 @@ lib LibIPC
|
|||||||
|
|
||||||
|
|
||||||
# networkd-related functions
|
# networkd-related functions
|
||||||
fun ipc_wait_event_networkd(Connections*, Connection*, Event*, Switchings*) : LibC::Int
|
fun ipc_wait_event_networkd(Connections*, Connection*, Event*, Switchings*, LibC::Long*) : LibC::Int
|
||||||
|
|
||||||
fun ipc_receive_fd (sock : LibC::Int, fd : LibC::Int*) : LibC::Int
|
fun ipc_receive_fd (sock : LibC::Int, fd : LibC::Int*) : LibC::Int
|
||||||
fun ipc_provide_fd (sock : LibC::Int, fd : LibC::Int) : LibC::Int
|
fun ipc_provide_fd (sock : LibC::Int, fd : LibC::Int) : LibC::Int
|
||||||
|
@ -70,22 +70,29 @@ class IPC::SwitchingService < IPC::Service
|
|||||||
@switch.del fd
|
@switch.del fd
|
||||||
end
|
end
|
||||||
|
|
||||||
def wait_event(server : IPC::Connection , &block) : Tuple(LibIPC::EventType, IPC::Message, IPC::Connection)
|
def wait_event(server : IPC::Connection , &block) : Tuple(LibIPC::EventType, IPC::Message, IPC::Connection) | Tuple(LibIPC::EventType, Nil, Nil)
|
||||||
event = LibIPC::Event.new
|
event = LibIPC::Event.new
|
||||||
|
|
||||||
serverp = server.pointer
|
serverp = server.pointer
|
||||||
r = LibIPC.ipc_wait_event_networkd self.pointer, serverp, pointerof(event), @switch.pointer
|
r = LibIPC.ipc_wait_event_networkd self.pointer, serverp, pointerof(event), @switch.pointer, pointerof(@timer)
|
||||||
|
|
||||||
if r != 0
|
if r != 0
|
||||||
m = String.new LibIPC.ipc_errors_get (r)
|
m = String.new LibIPC.ipc_errors_get (r)
|
||||||
yield IPC::Exception.new "error waiting for a new event: #{m}"
|
yield IPC::Exception.new "error waiting for a new event: #{m}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
eventtype = event.type.unsafe_as(LibIPC::EventType)
|
||||||
|
|
||||||
|
# if event type is Timer, there is no connection nor message
|
||||||
|
case eventtype
|
||||||
|
when LibIPC::EventType::Timer
|
||||||
|
return eventtype, nil, nil
|
||||||
|
end
|
||||||
|
|
||||||
connection = IPC::Connection.new event.origin.unsafe_as(Pointer(LibIPC::Connection)).value
|
connection = IPC::Connection.new event.origin.unsafe_as(Pointer(LibIPC::Connection)).value
|
||||||
|
|
||||||
message = event.message.unsafe_as(Pointer(LibIPC::Message))
|
message = event.message.unsafe_as(Pointer(LibIPC::Message))
|
||||||
|
|
||||||
eventtype = event.type.unsafe_as(LibIPC::EventType)
|
|
||||||
|
|
||||||
return eventtype, IPC::Message.new(message), connection
|
return eventtype, IPC::Message.new(message), connection
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user