Archived
3
0
This repository has been archived on 2024-06-18. You can view files and clone it, but cannot push or open issues or pull requests.
libipc-old/core/ipc.cr

250 lines
5.4 KiB
Crystal
Raw Normal View History

2018-10-04 12:57:05 +02:00
@[Link("ipc")]
lib LibIPC
struct Service
version : LibC::UInt
index : LibC::UInt
spath : LibC::Char[4096] # [PATH_MAX]
fd : LibC::Int
end
struct Client
version : LibC::UInt
index : LibC::UInt
fd : LibC::Int
end
2018-11-09 17:41:54 +01:00
enum MessageType
ServerClose
Error
Data
end
2018-10-04 12:57:05 +02:00
struct Message
2018-11-09 17:41:54 +01:00
type : UInt8
length : LibC::UInt
2018-10-04 12:57:05 +02:00
payload : LibC::Char*
end
2018-10-05 22:57:08 +02:00
struct Clients
2018-10-04 12:57:05 +02:00
clients : Client**
size : LibC::Int
end
2018-11-09 17:41:54 +01:00
enum EventType
NotSet
Error
Stdin
Connection
Disconnection
Message
end
struct Event
type : EventType
origin : Client*
m : Message*
end
2018-10-04 12:57:05 +02:00
# FIXME: IPC.initialize:
# - throw exceptions on error.
# - Make most arguments optional.
2018-10-09 16:36:51 +02:00
fun ipc_server_init(env : LibC::Char**, service : Service*, sname : LibC::Char*) : LibC::Int
2018-10-04 12:57:05 +02:00
# FIXME: IPC.(destroy?)
fun ipc_server_close(Service*) : LibC::Int
fun ipc_server_close_client(Client*) : LibC::Int
fun ipc_server_accept(Service*, Client*) : LibC::Int
fun ipc_server_read(Client*, Message*) : LibC::Int
fun ipc_server_write(Client*, Message*) : LibC::Int
2018-10-05 22:57:08 +02:00
fun ipc_server_select(Clients*, Service*, Clients*, LibC::Int*) : LibC::Int
2018-10-04 12:57:05 +02:00
2018-11-09 17:41:54 +01:00
fun ipc_service_poll_event(Clients*, Service*, Event*) : LibC::Int
2018-10-09 16:36:51 +02:00
fun ipc_application_connection(LibC::Char**, Service*, LibC::Char*) : LibC::Int
2018-10-04 22:53:51 +02:00
fun ipc_application_close(Service*) : LibC::Int
fun ipc_application_read(Service*, Message*) : LibC::Int
fun ipc_application_write(Service*, Message*) : LibC::Int
2018-10-05 22:57:08 +02:00
fun ipc_client_add(Clients*, Client*) : LibC::Int
fun ipc_client_del(Clients*, Client*) : LibC::Int
2018-10-04 22:53:51 +02:00
fun ipc_server_client_copy(Client*) : Client*
fun ipc_server_client_eq(Client*, Client*) : LibC::Int
fun ipc_server_client_gen(Client*, LibC::UInt, LibC::UInt)
2018-10-05 22:57:08 +02:00
fun ipc_clients_free(Clients*)
2018-10-04 22:53:51 +02:00
end
class IPC::Exception < ::Exception
2018-10-04 12:57:05 +02:00
end
class IPC::Service
2018-10-04 22:53:51 +02:00
@closed = false
2018-10-05 22:57:08 +02:00
@clients = LibIPC::Clients.new
2018-10-04 12:57:05 +02:00
# FIXME: getter only as long as proper bindings are unfinished
getter service = LibIPC::Service.new
def initialize(name : String)
2018-10-09 16:36:51 +02:00
if LibIPC.ipc_server_init(LibC.environ, pointerof(@service), name) < 0
2018-10-04 22:53:51 +02:00
raise Exception.new "ipc_server_init < 0" # FIXME: Add proper descriptions here.
2018-10-04 12:57:05 +02:00
end
2018-10-04 22:53:51 +02:00
# Very important as there are filesystem side-effects.
at_exit { close }
2018-10-04 12:57:05 +02:00
end
2018-10-11 11:31:48 +02:00
def initialize(name : String, &block : Proc(IPC::Event::Connection | IPC::Event::Disconnection | IPC::Event::Message, Nil))
initialize name
loop &block
close
end
2018-10-04 22:53:51 +02:00
def close
return if @closed
# FIXME: Probably check its not been closed already.
2018-10-04 12:57:05 +02:00
if LibIPC.ipc_server_close(pointerof(@service)) < 0
2018-10-04 22:53:51 +02:00
raise Exception.new "ipc_server_close < 0"
2018-10-04 12:57:05 +02:00
end
2018-10-04 22:53:51 +02:00
@closed = true
end
def finalize
close
end
def accept
2018-10-09 16:36:51 +02:00
::IPC::Server::Client.new pointerof(@service)
2018-10-04 12:57:05 +02:00
end
2018-10-05 22:57:08 +02:00
def loop(&block)
::loop do
2018-11-09 17:41:54 +01:00
event = LibIPC::Event.new
2018-10-05 22:57:08 +02:00
2018-11-09 17:41:54 +01:00
r = LibIPC.ipc_service_poll_event pointerof(@clients), pointerof(@service), pointerof(event)
2018-10-05 22:57:08 +02:00
2018-11-09 17:41:54 +01:00
if r < 0
raise Exception.new "ipc_service_poll_event < 0"
2018-10-05 22:57:08 +02:00
end
2018-11-09 17:41:54 +01:00
client = IPC::RemoteClient.new event.origin.unsafe_as(Pointer(LibIPC::Client)).value
2018-10-05 22:57:08 +02:00
2018-11-09 17:41:54 +01:00
pp! event
message = event.m.unsafe_as(Pointer(LibIPC::Message))
unless message.null?
pp! message.value
end
2018-10-05 22:57:08 +02:00
2018-11-09 17:41:54 +01:00
case event.type
when LibIPC::EventType::Connection
yield IPC::Event::Connection.new client
when LibIPC::EventType::Message
message = event.m.unsafe_as(Pointer(LibIPC::Message)).value
2018-10-05 22:57:08 +02:00
2018-11-09 17:41:54 +01:00
yield IPC::Event::Message.new IPC::Message.new(message.type, message.length, message.payload), client
when LibIPC::EventType::Disconnection
yield IPC::Event::Disconnection.new client
2018-10-05 22:57:08 +02:00
end
end
end
2018-10-04 12:57:05 +02:00
end
2018-10-04 22:53:51 +02:00
class IPC::Message
enum Type
2018-10-09 16:36:51 +02:00
CLOSE
CONNECTION
SYN
ACK
2018-10-04 22:53:51 +02:00
DATA
end
2018-10-04 12:57:05 +02:00
2018-10-04 22:53:51 +02:00
getter type : UInt8
getter payload : String
2018-10-04 12:57:05 +02:00
2018-10-04 22:53:51 +02:00
def initialize(type, length, payload)
2018-11-09 17:41:54 +01:00
@type = type.to_u8
2018-10-04 22:53:51 +02:00
@payload = String.new payload, length
end
end
2018-10-04 12:57:05 +02:00
2018-11-09 17:41:54 +01:00
class IPC::RemoteClient
getter client : LibIPC::Client
2018-10-04 12:57:05 +02:00
2018-10-05 22:57:08 +02:00
def initialize(@client)
end
2018-10-04 22:53:51 +02:00
def send(type : UInt8, payload : String)
message = LibIPC::Message.new type: type, length: payload.size, payload: payload.to_unsafe
if LibIPC.ipc_server_write(pointerof(@client), pointerof(message)) < 0
2018-10-05 22:57:08 +02:00
raise Exception.new "ipc_server_write < 0"
2018-10-04 22:53:51 +02:00
end
end
2018-10-04 12:57:05 +02:00
end
2018-10-05 22:57:08 +02:00
class IPC::Event
class Connection
2018-11-09 17:41:54 +01:00
getter client : IPC::RemoteClient
2018-10-05 22:57:08 +02:00
def initialize(@client)
end
end
2018-10-04 22:53:51 +02:00
2018-10-05 22:57:08 +02:00
class Disconnection
2018-11-09 17:41:54 +01:00
getter client : IPC::RemoteClient
2018-10-05 22:57:08 +02:00
def initialize(@client)
end
end
class Message
getter message : ::IPC::Message
2018-11-09 17:41:54 +01:00
getter client : IPC::RemoteClient
2018-10-05 22:57:08 +02:00
def initialize(@message, @client)
end
end
end
2018-10-04 22:53:51 +02:00
2018-10-09 16:36:51 +02:00
class IPC::Client
@service = LibIPC::Service.new
def initialize(@service_name : String)
if LibIPC.ipc_application_connection(LibC.environ, pointerof(@service), @service_name) < 0
raise Exception.new "ipc_application_connection < 0"
end
end
def initialize(name, &block)
initialize(name)
yield self
close
end
def send(type, payload : String)
message = LibIPC::Message.new type: type, length: payload.size, payload: payload.to_unsafe
if LibIPC.ipc_application_write(pointerof(@service), pointerof(message)) < 0
raise Exception.new "ipc_application_write < 0"
end
end
2018-10-05 22:57:08 +02:00
2018-10-09 16:36:51 +02:00
def read
message = LibIPC::Message.new
if LibIPC.ipc_application_read(pointerof(@service), pointerof(message)) < 0
raise Exception.new "ipc_application_read < 0"
end
IPC::Message.new message.type, message.length, message.payload
end
def close
if LibIPC.ipc_application_close(pointerof(@service)) < 0
raise Exception.new "ipc_application_close < 0"
end
2018-10-04 22:53:51 +02:00
end
end
2018-10-04 12:57:05 +02:00