All Projects → VSoftTechnologies → VSoft.Messaging

VSoftTechnologies / VSoft.Messaging

Licence: other
Simple internal application messaging for Delphi

Programming Languages

pascal
1382 projects

Projects that are alternatives of or similar to VSoft.Messaging

VSoft.HttpClient
WinHTTP Client for Delphi
Stars: ✭ 18 (-47.06%)
Mutual labels:  dpmpackage
FireMonkeyInstagramApp
Sample Instagram UI design project made with Embarcadero Delphi 10.1 Berlin.
Stars: ✭ 23 (-32.35%)
Mutual labels:  fmx
TeeGrid-VCL-FMX-Samples
Grid control for Delphi and C++ (VCL and FMX)
Stars: ✭ 132 (+288.24%)
Mutual labels:  fmx
OldCEF4Delphi
OldCEF4Delphi is an open source project to embed Chromium-based browsers in applications made with Delphi.
Stars: ✭ 55 (+61.76%)
Mutual labels:  fmx
QQWry
Delphi interface for QQWry IP database
Stars: ✭ 14 (-58.82%)
Mutual labels:  fmx
skia4delphi
Skia4Delphi is a cross-platform 2D graphics API for Delphi platforms based on Google's Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.
Stars: ✭ 486 (+1329.41%)
Mutual labels:  fmx
RADStudio-DemoKit
A collection of demos around 10.4 Sydney updated for 10.4.2.
Stars: ✭ 39 (+14.71%)
Mutual labels:  fmx
FMXPong
Pong 3D with Delphi and Firemonkey (Windows, Mac OS, Android, IOS)
Stars: ✭ 25 (-26.47%)
Mutual labels:  fmx
ipub-messaging
Messaging system for communication between classes / layers in delphi
Stars: ✭ 46 (+35.29%)
Mutual labels:  fmx
VSoft.CommandLineParser
Simple Command Line Options Parser - part of the DUnitX Project
Stars: ✭ 78 (+129.41%)
Mutual labels:  dpmpackage
WebView4Delphi
WebView4Delphi is an open source project created by Salvador Díaz Fau to embed Chromium-based browsers in applications made with Delphi or Lazarus/FPC for Windows.
Stars: ✭ 157 (+361.76%)
Mutual labels:  fmx
VSoft.WeakReferences
Weak References for delphi
Stars: ✭ 28 (-17.65%)
Mutual labels:  dpmpackage
FMX.MaterialIconsFont
Material Design Icons 3.0.1
Stars: ✭ 20 (-41.18%)
Mutual labels:  fmx
FMX.FontAwesome
[FireMonkey] FontAwesome
Stars: ✭ 21 (-38.24%)
Mutual labels:  fmx
OpenWeatherDemo
Demo app for the article as published on the Delphi Worlds blog
Stars: ✭ 17 (-50%)
Mutual labels:  fmx
GBE3D
Suite de composants 3D pour Delphi Firemonkey
Stars: ✭ 18 (-47.06%)
Mutual labels:  fmx
VK API
VK API Wrapper - in progress
Stars: ✭ 16 (-52.94%)
Mutual labels:  fmx
PUSHTestFCM
[FireMonkey] Push test project
Stars: ✭ 17 (-50%)
Mutual labels:  fmx
VKAudioPlayer
VK Audio Player on Delphi
Stars: ✭ 16 (-52.94%)
Mutual labels:  fmx

VSoft.Messaging

VSoft.Messaging is a libary that provides an internal synchronous/asynchronous publish/subscribe messaging system for Delphi applications.

This library is used by FinalBuilder and has been working well for many years.

How it works

This library takes advantage of TObject.Dispatch and dynamic methods which were introduced back in delphi 1.0. The Dispatch method is how windows messages are dispatched to message handler methods on TObject descendants.

Whilst TObject.Dispatch was originally added for the purpose of dispatching windows messages, it's not actually tied to windows. That means this library should work on any platform that Delphi supports. That said, I have only tested this on Win32/Win64 (vcl and fmx).

Messages

Messages are defined as records, with a MsgId field.

//create a unique message id.
//on windows this needs to be higher than WM_USER

const PROJECT_OPENED_MSG = WM_USER + $1000;
type
  TProjectOpenedMessage = record
    MsgID  : TMessageID;
    Filler : TMessageFiller;
    //payload starts here.
    ProjectName   : string;
  public
    constructor Create(const theProjectName : string);
  end;

implementation

constructor TProjectOpenedMessage.Create(const theProjectName : string);
begin
  MsgID := PROJECT_OPENED_MSG;  //assign our message id
  ProjectName := theProjectName; //store the payload.
end;

TMessageID is defined as TMessageID = Cardinal;

The Filler field is only needed if you are handling messages in VCL or FMX (on Windows) applications, and then only if you are handling them on Forms, Frames or wincontrols, ie anything with a Windproc. This is because the DefaultHandler in TControl, TWinControl and TCustomForm casts the Message parameter to TMessage. If you are using this on other platforms, you can use Word for the MsgId field, as that is all that is used by TObject.Dispatch

Channels

Messages are sent through a channel object. To create a channel, use the TMessageChannelFactory.CreateChannel method

var
  channel : IMessageChannel;
begin
  channel := TMessageChannelFactory.CreateChannel;
...

Sending Messages

Example :

procedure SendProjectOpenedMessage(const projectName : string)
var
  msg : TProjectOpenedMessage;
begin
  msg := TProjectOpened.Create(projectName);
  FChannel.Queue.PostMessage(msg); //async
  //or
  FChannel.Queue.SendMessage(msg); //sync
end;

Note that the reason the PostMessage and SendMessage are on the channel.Queue property is purely to work around the generics limitations in delphi, where non-generic interfaces cannot have generic methods (records can, so Queue is actually a record).

Queue.PostMessage sends the message Asynchronously, ie the method returns before the message has been received.

Queue.SendMessage returns after all dispatchers have finished dispatching the message. Note SendMessage should not be used to send messages to dispatchers that operate on the user interface, use PostMessage instead.

Receiving Messages

For an object to receive the messages, it needs to create a Dispatcher, and hook the dispatcher to itself and the channel

type
  TReceiver = class
  private
    FDispatcher : IMessageDispatcher;
  public
    constructor Create(const channel : IMessageChannel);
  end;

implementation

constructor TReceiver.Create(const channel : IMessageChannel);
begin
  FDispatcher := TMessageDispatcherFactory.CreateDispatcher;

  // tell the dispatcher where to dispatch the messages to.
  FDispatcher.Target := Self;

  //hook up the dispatcher to the channel.
  FDispatcher.Channel := channel;
end;

NOTE: If you are handling messages on forms/frames/controls where you will be making UI updates, then you should use TMessageDispatcherFactory.CreateUIDispatcher - this ensures that messages are only dispatched on the main thread (calls TThread.Queue ) . For performance reasons CreateUIDispatcher should not be used for non ui code, and you should avoid using too many ui dispatchers hooked to the same channel.

Handling Messages

Handling messages is just like handling windows messages in Delphi

type
  TReceiver = class
  private
    ...
    procedure ProjectOpened(var msg : TProjectOpenedMessage); message PROJECT_OPENED_MSG;
    ...
  end;
implementation

procedure TReceiver.ProjectOpened(var msg : TProjectOpenedMessage);
begin
    //use msg.ProjectName payload here.
end;
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].