user_type
This commit is contained in:
parent
4cf5c9cf49
commit
bee1390382
32
src/ipc.cr
32
src/ipc.cr
@ -86,9 +86,10 @@ lib LibIPC
|
|||||||
end
|
end
|
||||||
|
|
||||||
struct Message
|
struct Message
|
||||||
type : UInt8
|
type : UInt8
|
||||||
length : LibC::UInt
|
user_type : UInt8
|
||||||
payload : LibC::Char*
|
length : LibC::UInt
|
||||||
|
payload : LibC::Char*
|
||||||
end
|
end
|
||||||
|
|
||||||
enum EventType
|
enum EventType
|
||||||
@ -113,7 +114,6 @@ lib LibIPC
|
|||||||
# connection to a service
|
# connection to a service
|
||||||
fun ipc_connection(LibC::Char**, Connection*, LibC::Char*) : LibC::Int
|
fun ipc_connection(LibC::Char**, Connection*, LibC::Char*) : LibC::Int
|
||||||
|
|
||||||
fun ipc_accept(Connection*, Connection*) : LibC::Int
|
|
||||||
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
|
||||||
|
|
||||||
@ -138,10 +138,19 @@ end
|
|||||||
|
|
||||||
class IPC::Message
|
class IPC::Message
|
||||||
getter type : UInt8
|
getter type : UInt8
|
||||||
|
getter user_type : UInt8
|
||||||
getter payload : String
|
getter payload : String
|
||||||
|
|
||||||
def initialize(type, length, payload)
|
def initialize(m : Pointer(LibIPC::Message))
|
||||||
|
message = m.unsafe_as(Pointer(LibIPC::Message)).value
|
||||||
|
@type = message.type
|
||||||
|
@user_type = message.user_type
|
||||||
|
@payload = String.new message.payload, message.length
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(type, user_type, length, payload)
|
||||||
@type = type.to_u8
|
@type = type.to_u8
|
||||||
|
@user_type = user_type
|
||||||
@payload = String.new payload, length
|
@payload = String.new payload, length
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -196,8 +205,8 @@ class IPC::Connection
|
|||||||
close
|
close
|
||||||
end
|
end
|
||||||
|
|
||||||
def send(type : LibIPC::MessageType, payload : String)
|
def send(user_type : UInt8, payload : String)
|
||||||
message = LibIPC::Message.new type: type.to_u8, length: payload.bytesize, payload: payload.to_unsafe
|
message = LibIPC::Message.new type: LibIPC::MessageType::Data.to_u8, user_type: user_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
|
||||||
@ -214,7 +223,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.length, message.payload
|
IPC::Message.new message.type, message.user_type, message.length, message.payload
|
||||||
end
|
end
|
||||||
|
|
||||||
def close
|
def close
|
||||||
@ -318,11 +327,12 @@ class IPC::Service
|
|||||||
|
|
||||||
when LibIPC::EventType::ExtraSocket
|
when LibIPC::EventType::ExtraSocket
|
||||||
message = event.message.unsafe_as(Pointer(LibIPC::Message)).value
|
message = event.message.unsafe_as(Pointer(LibIPC::Message)).value
|
||||||
yield IPC::Event::ExtraSocket.new IPC::Message.new(message.type, message.length, message.payload), 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
|
# message = event.message.unsafe_as(Pointer(LibIPC::Message)).value
|
||||||
yield IPC::Event::Message.new IPC::Message.new(message.type, message.length, message.payload), 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