Documentation ¶
Overview ¶
Package dispatch provides structs and interfaces to organize thread-safe communication between different modules of the program.
Index ¶
- Constants
- type CallbackHandler
- type Looper
- type LooperImpl
- func (l *LooperImpl) Execute(item WorkItem)
- func (l *LooperImpl) GetMessageDispatcher() MessageDispatcher
- func (l *LooperImpl) GetWorkDispatcher() WorkDispatcher
- func (l *LooperImpl) Loop()
- func (l *LooperImpl) RemoveMessageHandler(what int)
- func (l *LooperImpl) SendMessage(message *Message)
- func (l *LooperImpl) SetMessageHandler(what int, handler MessageHandlerFunc)
- type Message
- func NewMessage(what int) *Message
- func NewMessageFrom(from interface{}, what int) *Message
- func NewMessageFromWithAnyData(from interface{}, what int, data interface{}) *Message
- func NewMessageWithAnyData(what int, data interface{}) *Message
- func NewMessageWithStringData(what int, data string) *Message
- type MessageDispatcher
- type MessageHandler
- type MessageHandlerFunc
- type MessageQueue
- func (q *MessageQueue) Close()
- func (q *MessageQueue) Dequeue() *Message
- func (q *MessageQueue) DequeueBlocking() *Message
- func (q *MessageQueue) Enqueue(message *Message)
- func (q *MessageQueue) GetSize() uint
- func (q *MessageQueue) IsEmpty() bool
- func (q *MessageQueue) LockMainChannel()
- func (q *MessageQueue) UnlockMainChannel()
- func (q *MessageQueue) WaitForMessage(what int)
- type WorkDispatcher
- type WorkItem
- type WorkItemFunc
Constants ¶
const ( MessageEmpty = -2147483648 + iota MessageWorkExec MessageExit )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CallbackHandler ¶
type CallbackHandler func(message *Message)
type Looper ¶
type Looper interface { Loop() GetMessageDispatcher() MessageDispatcher }
type LooperImpl ¶
type LooperImpl struct {
// contains filtered or unexported fields
}
func NewLooperImpl ¶
func NewLooperImpl(messageQueueBuffer uint) *LooperImpl
func NewLooperImplCompat ¶
func NewLooperImplCompat(messageQueueBuffer int) *LooperImpl
func (*LooperImpl) Execute ¶
func (l *LooperImpl) Execute(item WorkItem)
func (*LooperImpl) GetMessageDispatcher ¶
func (l *LooperImpl) GetMessageDispatcher() MessageDispatcher
func (*LooperImpl) GetWorkDispatcher ¶
func (l *LooperImpl) GetWorkDispatcher() WorkDispatcher
func (*LooperImpl) Loop ¶
func (l *LooperImpl) Loop()
func (*LooperImpl) RemoveMessageHandler ¶
func (l *LooperImpl) RemoveMessageHandler(what int)
func (*LooperImpl) SendMessage ¶
func (l *LooperImpl) SendMessage(message *Message)
func (*LooperImpl) SetMessageHandler ¶
func (l *LooperImpl) SetMessageHandler(what int, handler MessageHandlerFunc)
type Message ¶
type Message struct { Id int What int IntData int BytesData []byte AnyData interface{} Sender interface{} }
Message holds an int message code and some data.
func NewMessage ¶
func NewMessageFrom ¶
func NewMessageWithAnyData ¶
func (*Message) StringData ¶
type MessageDispatcher ¶
type MessageDispatcher interface {
SendMessage(message *Message)
}
type MessageHandler ¶
type MessageHandler interface {
OnMessage(msg *Message)
}
type MessageHandlerFunc ¶
type MessageHandlerFunc func(message *Message)
type MessageQueue ¶
type MessageQueue struct {
// contains filtered or unexported fields
}
MessageQueue is a thread-safe message buffer. In general, it can be used to communicate between different threads. It is mostly used to communicate between the engine and the frontend. There are two channels under the hood - main and secondary. The size of main channel is specified in the constructor function, whereas the size of the secondary channel equals half the size of the main channel. You can lock the main channel, so new messages won't be sent to it. Then all messages in it can be processed. While the main channel is locked, messages are sent to the secondary channel. As soon as the main channel is unlocked, all messages from the secondary channel are sent to main channel.
func NewMessageQueue ¶
func NewMessageQueue(bufferSize uint) *MessageQueue
NewMessageQueue creates a new instance of MessageQueue with the specified buffer size. Returns pointer to the MessageQueue instance.
func (*MessageQueue) Dequeue ¶
func (q *MessageQueue) Dequeue() *Message
Dequeue Removes the first message from the queue. If queue is empty returns an empty Message.
func (*MessageQueue) DequeueBlocking ¶
func (q *MessageQueue) DequeueBlocking() *Message
DequeueBlocking Removes the first message from the queue. If the queue is empty waits for a message to come.
func (*MessageQueue) Enqueue ¶
func (q *MessageQueue) Enqueue(message *Message)
Enqueue sends the specified message to the end of the queue. Depending on the current state of the MessageQueue it sent to either main or secondary channel.
func (*MessageQueue) GetSize ¶
func (q *MessageQueue) GetSize() uint
GetSize returns the current size of the queue.
func (*MessageQueue) IsEmpty ¶
func (q *MessageQueue) IsEmpty() bool
IsEmpty indicate if the queue is empty, i.e. size == 0.
func (*MessageQueue) LockMainChannel ¶
func (q *MessageQueue) LockMainChannel()
LockMainChannel locks the main channel. While it is locked new messages will be sent to the secondary channel. The locked state remains until UnlockMainChannel is called.
func (*MessageQueue) UnlockMainChannel ¶
func (q *MessageQueue) UnlockMainChannel()
UnlockMainChannel unlocks the main channel. All messages from the secondary channel are sent to the main one. After that new messages are sent to the main channel again.
func (*MessageQueue) WaitForMessage ¶ added in v0.3.4
func (q *MessageQueue) WaitForMessage(what int)
WaitForMessage blocks the calling thread until a message with the given code is read from the queue.
type WorkDispatcher ¶
type WorkDispatcher interface {
Execute(item WorkItem)
}
type WorkItemFunc ¶
type WorkItemFunc struct {
// contains filtered or unexported fields
}
func NewWorkItemFunc ¶
func NewWorkItemFunc(work func()) *WorkItemFunc
func (*WorkItemFunc) Execute ¶
func (w *WorkItemFunc) Execute()