Merge branch 'cleaning' of JunkOS/ipc.cr into master
This commit is contained in:
commit
c4d82297c6
114
src/ipc.cr
114
src/ipc.cr
@ -16,12 +16,6 @@ lib LibIPC
|
|||||||
size : LibC::Int
|
size : LibC::Int
|
||||||
end
|
end
|
||||||
|
|
||||||
enum MessageType
|
|
||||||
ServerClose
|
|
||||||
Error
|
|
||||||
Data
|
|
||||||
end
|
|
||||||
|
|
||||||
enum Errors
|
enum Errors
|
||||||
None = 0
|
None = 0
|
||||||
NotEnoughMemory
|
NotEnoughMemory
|
||||||
@ -85,6 +79,12 @@ lib LibIPC
|
|||||||
MessageEmptyEmptyMessageList
|
MessageEmptyEmptyMessageList
|
||||||
end
|
end
|
||||||
|
|
||||||
|
enum MessageType
|
||||||
|
ServerClose
|
||||||
|
Error
|
||||||
|
Data
|
||||||
|
end
|
||||||
|
|
||||||
struct Message
|
struct Message
|
||||||
type : UInt8
|
type : UInt8
|
||||||
user_type : UInt8
|
user_type : UInt8
|
||||||
@ -137,21 +137,31 @@ class IPC::Exception < ::Exception
|
|||||||
end
|
end
|
||||||
|
|
||||||
class IPC::Message
|
class IPC::Message
|
||||||
getter type : UInt8
|
getter mtype : UInt8 # libipc message type
|
||||||
getter user_type : UInt8
|
getter type : UInt8 # libipc user message type
|
||||||
getter payload : String
|
getter payload : Bytes
|
||||||
|
|
||||||
def initialize(m : Pointer(LibIPC::Message))
|
def initialize(message : Pointer(LibIPC::Message))
|
||||||
message = m.unsafe_as(Pointer(LibIPC::Message)).value
|
if message.null?
|
||||||
@type = message.type
|
@mtype = LibIPC::MessageType::Error.to_u8
|
||||||
@user_type = message.user_type
|
@type = 0
|
||||||
@payload = String.new message.payload, message.length
|
@payload = Bytes.new "".to_unsafe, 0
|
||||||
|
else
|
||||||
|
m = message.value
|
||||||
|
@mtype = m.type
|
||||||
|
@type = m.user_type
|
||||||
|
@payload = Bytes.new m.payload, m.length
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(type, user_type, length, payload)
|
def initialize(mtype, type, payload : Bytes)
|
||||||
@type = type.to_u8
|
@mtype = mtype.to_u8
|
||||||
@user_type = user_type
|
@type = type
|
||||||
@payload = String.new payload, length
|
@payload = payload
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(mtype, type, payload : String)
|
||||||
|
initialize(mtype, type, Bytes.new(payload.to_unsafe, payload.bytesize))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -205,8 +215,8 @@ class IPC::Connection
|
|||||||
close
|
close
|
||||||
end
|
end
|
||||||
|
|
||||||
def send(user_type : UInt8, payload : String)
|
def send(type : UInt8, payload : Bytes)
|
||||||
message = LibIPC::Message.new type: LibIPC::MessageType::Data.to_u8, user_type: user_type, length: payload.bytesize, payload: payload.to_unsafe
|
message = LibIPC::Message.new type: LibIPC::MessageType::Data.to_u8, user_type: type, length: payload.bytesize, payload: payload.to_unsafe
|
||||||
|
|
||||||
r = LibIPC.ipc_write(pointerof(@connection), pointerof(message))
|
r = LibIPC.ipc_write(pointerof(@connection), pointerof(message))
|
||||||
if r != 0
|
if r != 0
|
||||||
@ -215,6 +225,14 @@ class IPC::Connection
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def send(type : UInt8, payload : String)
|
||||||
|
send(type, Bytes.new(payload.to_unsafe, payload.bytesize))
|
||||||
|
end
|
||||||
|
|
||||||
|
def send(message : IPC::Message)
|
||||||
|
send(message.type, message.payload)
|
||||||
|
end
|
||||||
|
|
||||||
def read
|
def read
|
||||||
message = LibIPC::Message.new
|
message = LibIPC::Message.new
|
||||||
r = LibIPC.ipc_read(pointerof(@connection), pointerof(message))
|
r = LibIPC.ipc_read(pointerof(@connection), pointerof(message))
|
||||||
@ -223,7 +241,7 @@ class IPC::Connection
|
|||||||
raise Exception.new "error reading a message: #{m}"
|
raise Exception.new "error reading a message: #{m}"
|
||||||
end
|
end
|
||||||
|
|
||||||
IPC::Message.new message.type, message.user_type, message.length, message.payload
|
IPC::Message.new message.mtype, message.type, message.payload
|
||||||
end
|
end
|
||||||
|
|
||||||
def close
|
def close
|
||||||
@ -239,6 +257,8 @@ class IPC::Connection
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
alias Events = IPC::Event::Connection | IPC::Event::Disconnection | IPC::Event::Message | IPC::Event::ExtraSocket
|
||||||
|
|
||||||
class IPC::Service
|
class IPC::Service
|
||||||
@closed = false
|
@closed = false
|
||||||
@connections = LibIPC::Connections.new
|
@connections = LibIPC::Connections.new
|
||||||
@ -255,9 +275,7 @@ class IPC::Service
|
|||||||
at_exit { close }
|
at_exit { close }
|
||||||
end
|
end
|
||||||
|
|
||||||
alias Events = IPC::Event::Connection | IPC::Event::Disconnection | IPC::Event::Message | IPC::Event::ExtraSocket
|
def initialize(name : String, &block : Proc(Events|Exception, Nil))
|
||||||
|
|
||||||
def initialize(name : String, &block : Proc(Events, Nil))
|
|
||||||
initialize name
|
initialize name
|
||||||
loop &block
|
loop &block
|
||||||
close
|
close
|
||||||
@ -296,26 +314,31 @@ class IPC::Service
|
|||||||
close
|
close
|
||||||
end
|
end
|
||||||
|
|
||||||
def loop(&block)
|
def wait_event(&block) : Tuple(LibIPC::EventType, IPC::Message, IPC::Connection)
|
||||||
|
event = LibIPC::Event.new
|
||||||
|
|
||||||
|
r = LibIPC.ipc_wait_event pointerof(@connections), pointerof(@service_info), pointerof(event)
|
||||||
|
if r != 0
|
||||||
|
m = String.new LibIPC.ipc_errors_get (r)
|
||||||
|
yield IPC::Exception.new "error waiting for a new event: #{m}"
|
||||||
|
end
|
||||||
|
|
||||||
|
connection = IPC::Connection.new event.origin.unsafe_as(Pointer(LibIPC::Connection)).value
|
||||||
|
|
||||||
|
pp! event
|
||||||
|
message = event.message.unsafe_as(Pointer(LibIPC::Message))
|
||||||
|
unless message.null?
|
||||||
|
pp! message.value
|
||||||
|
end
|
||||||
|
|
||||||
|
return event.type, IPC::Message.new(message), connection
|
||||||
|
end
|
||||||
|
|
||||||
|
def loop(&block : Proc(Events|Exception, Nil))
|
||||||
::loop do
|
::loop do
|
||||||
event = LibIPC::Event.new
|
type, message, connection = wait_event &block
|
||||||
|
|
||||||
r = LibIPC.ipc_wait_event pointerof(@connections), pointerof(@service_info), pointerof(event)
|
case type
|
||||||
|
|
||||||
if r != 0
|
|
||||||
m = String.new LibIPC.ipc_errors_get (r)
|
|
||||||
yield IPC::Exception.new "error waiting for a new event: #{m}"
|
|
||||||
end
|
|
||||||
|
|
||||||
connection = IPC::Connection.new event.origin.unsafe_as(Pointer(LibIPC::Connection)).value
|
|
||||||
|
|
||||||
pp! event
|
|
||||||
message = event.message.unsafe_as(Pointer(LibIPC::Message))
|
|
||||||
unless message.null?
|
|
||||||
pp! message.value
|
|
||||||
end
|
|
||||||
|
|
||||||
case event.type
|
|
||||||
when LibIPC::EventType::Connection
|
when LibIPC::EventType::Connection
|
||||||
yield IPC::Event::Connection.new connection
|
yield IPC::Event::Connection.new connection
|
||||||
|
|
||||||
@ -326,13 +349,10 @@ class IPC::Service
|
|||||||
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
|
||||||
message = event.message.unsafe_as(Pointer(LibIPC::Message)).value
|
yield IPC::Event::ExtraSocket.new message, connection
|
||||||
yield IPC::Event::ExtraSocket.new IPC::Message.new(message.type, message.user_type, message.length, message.payload), connection
|
|
||||||
|
|
||||||
when LibIPC::EventType::Message
|
when LibIPC::EventType::Message
|
||||||
# message = event.message.unsafe_as(Pointer(LibIPC::Message)).value
|
yield IPC::Event::Message.new message, connection
|
||||||
# yield IPC::Event::Message.new IPC::Message.new(message.type, message.length, message.payload), connection
|
|
||||||
yield IPC::Event::Message.new IPC::Message.new(event.message), connection
|
|
||||||
|
|
||||||
when LibIPC::EventType::Disconnection
|
when LibIPC::EventType::Disconnection
|
||||||
yield IPC::Event::Disconnection.new connection
|
yield IPC::Event::Disconnection.new connection
|
||||||
|
Reference in New Issue
Block a user