diff options
Diffstat (limited to 'packages/fcl-process/src/winall/simpleipc.inc')
-rw-r--r-- | packages/fcl-process/src/winall/simpleipc.inc | 158 |
1 files changed, 11 insertions, 147 deletions
diff --git a/packages/fcl-process/src/winall/simpleipc.inc b/packages/fcl-process/src/winall/simpleipc.inc index 99eeac8634..1c70dcce09 100644 --- a/packages/fcl-process/src/winall/simpleipc.inc +++ b/packages/fcl-process/src/winall/simpleipc.inc @@ -14,7 +14,7 @@ **********************************************************************} -uses Windows,messages,contnrs; +uses Windows,messages; const MsgWndClassName: WideString = 'FPCMsgWindowCls'; @@ -22,7 +22,6 @@ const resourcestring SErrFailedToRegisterWindowClass = 'Failed to register message window class'; SErrFailedToCreateWindow = 'Failed to create message window %s'; - SErrMessageQueueOverflow = 'Message queue overflow (limit %s)'; var MsgWindowClass: TWndClassW = ( @@ -38,43 +37,12 @@ var lpszClassName: nil); type - TWinMsgServerMsg = class - strict private - FStream: TStream; - FMsgType: TMessageType; - public - constructor Create; - destructor Destroy; override; - property Stream: TStream read FStream; - property MsgType: TMessageType read FMsgType write FMsgType; - end; - - TWinMsgServerMsgQueue = class - strict private - FList: TFPObjectList; - FMaxCount: Integer; - FMaxAction: TIPCMessageOverflowAction; - function GetCount: Integer; - procedure DeleteAndFree(Index: Integer); - function PrepareToPush: Boolean; - public - constructor Create; - destructor Destroy; override; - procedure Clear; - procedure Push(AItem: TWinMsgServerMsg); - function Pop: TWinMsgServerMsg; - property Count: Integer read GetCount; - property MaxCount: Integer read FMaxCount write FMaxCount; - property MaxAction: TIPCMessageOverflowAction read FMaxAction write FMaxAction; - end; - TWinMsgServerComm = Class(TIPCServerComm) strict private FHWND : HWND; FWindowName : String; FWndProcException: Boolean; FWndProcExceptionMsg: String; - FMsgQueue: TWinMsgServerMsgQueue; function AllocateHWnd(const aWindowName: WideString) : HWND; procedure ProcessMessages; procedure ProcessMessagesWait(TimeOut: Integer); @@ -97,95 +65,6 @@ type Property WindowName : String Read FWindowName; end; - { --------------------------------------------------------------------- - TWinMsgServerMsg / TWinMsgServerMsgQueue - ---------------------------------------------------------------------} - -constructor TWinMsgServerMsg.Create; -begin - FMsgType := 0; - FStream := TMemoryStream.Create; -end; - -destructor TWinMsgServerMsg.Destroy; -begin - FStream.Free; -end; - - -constructor TWinMsgServerMsgQueue.Create; -begin - FMaxCount := DefaultIPCMessageQueueLimit; - FMaxAction := DefaultIPCMessageOverflowAction; - FList := TFPObjectList.Create(False); // FreeObjects = False! -end; - -destructor TWinMsgServerMsgQueue.Destroy; -begin - Clear; - FList.Free; -end; - -procedure TWinMsgServerMsgQueue.Clear; -begin - while FList.Count > 0 do - DeleteAndFree(FList.Count - 1); -end; - -procedure TWinMsgServerMsgQueue.DeleteAndFree(Index: Integer); -begin - FList[Index].Free; // Free objects manually! - FList.Delete(Index); -end; - -function TWinMsgServerMsgQueue.GetCount: Integer; -begin - Result := FList.Count; -end; - -function TWinMsgServerMsgQueue.PrepareToPush: Boolean; -begin - Result := True; - case FMaxAction of - ipcmoaDiscardOld: - begin - while (FList.Count >= FMaxCount) do - DeleteAndFree(FList.Count - 1); - end; - ipcmoaDiscardNew: - begin - Result := (FList.Count < FMaxCount); - end; - ipcmoaError: - begin - if (FList.Count >= FMaxCount) then - // Caller is expected to catch this exception, so not using Owner.DoError() - raise EIPCError.CreateFmt(SErrMessageQueueOverflow, [IntToStr(FMaxCount)]); - end; - end; -end; - -procedure TWinMsgServerMsgQueue.Push(AItem: TWinMsgServerMsg); -begin - if PrepareToPush then - FList.Insert(0, AItem); -end; - -function TWinMsgServerMsgQueue.Pop: TWinMsgServerMsg; -var - Index: Integer; -begin - Index := FList.Count - 1; - if Index >= 0 then - begin - // Caller is responsible for freeing the object. - Result := TWinMsgServerMsg(FList[Index]); - FList.Delete(Index); - end - else - Result := nil; -end; - { --------------------------------------------------------------------- MsgWndProc ---------------------------------------------------------------------} @@ -257,13 +136,11 @@ begin FWindowName := FWindowName+'_'+InstanceID; FWndProcException := False; FWndProcExceptionMsg := ''; - FMsgQueue := TWinMsgServerMsgQueue.Create; end; destructor TWinMsgServerComm.Destroy; begin StopServer; - FMsgQueue.Free; inherited; end; @@ -275,7 +152,6 @@ end; procedure TWinMsgServerComm.StopServer; begin - FMsgQueue.Clear; if FHWND <> 0 then begin DestroyWindow(FHWND); @@ -304,12 +180,12 @@ end; function TWinMsgServerComm.HaveQueuedMessages: Boolean; inline; begin - Result := (FMsgQueue.Count > 0); + Result := (Owner.Queue.Count > 0); end; function TWinMsgServerComm.CountQueuedMessages: Integer; inline; begin - Result := FMsgQueue.Count; + Result := Owner.Queue.Count; end; procedure TWinMsgServerComm.HandlePostedMessage(const Msg: TMsg); inline; @@ -397,10 +273,11 @@ end; procedure TWinMsgServerComm.ReadMsgData(var Msg: TMsg); var CDS: PCopyDataStruct; - MsgItem: TWinMsgServerMsg; + MsgItem: TIPCServerMsg; + begin CDS := PCopyDataStruct(Msg.lParam); - MsgItem := TWinMsgServerMsg.Create; + MsgItem := TIPCServerMsg.Create; try MsgItem.MsgType := CDS^.dwData; MsgItem.Stream.WriteBuffer(CDS^.lpData^,CDS^.cbData); @@ -409,7 +286,7 @@ begin // Caller is expected to catch this exception, so not using Owner.DoError() raise; end; - FMsgQueue.Push(MsgItem); + PushMessage(MsgItem); end; function TWinMsgServerComm.TryReadMsgData(var Msg: TMsg; out Error: String): Boolean; @@ -426,21 +303,8 @@ begin end; procedure TWinMsgServerComm.ReadMessage; -var - MsgItem: TWinMsgServerMsg; begin - MsgItem := FMsgQueue.Pop; - if Assigned(MsgItem) then - try - // Load message from the queue into the owner's message data. - MsgItem.Stream.Position := 0; - Owner.FMsgData.Size := 0; - Owner.FMsgType := MsgItem.MsgType; - Owner.FMsgData.CopyFrom(MsgItem.Stream, MsgItem.Stream.Size); - finally - // We are responsible for freeing the message from the queue. - MsgItem.Free; - end; + // Do nothing, PeekMessages has pushed messages to the queue. end; function TWinMsgServerComm.GetInstanceID: String; @@ -451,7 +315,7 @@ end; { --------------------------------------------------------------------- TWinMsgClientComm ---------------------------------------------------------------------} - + Type TWinMsgClientComm = Class(TIPCClientComm) Private @@ -544,7 +408,7 @@ Function TSimpleIPCServer.CommClass : TIPCServerCommClass; begin if (DefaultIPCServerClass<>Nil) then Result:=DefaultIPCServerClass - else + else Result:=TWinMsgServerComm; end; @@ -553,7 +417,7 @@ Function TSimpleIPCClient.CommClass : TIPCClientCommClass; begin if (DefaultIPCClientClass<>Nil) then Result:=DefaultIPCClientClass - else + else Result:=TWinMsgClientComm; end; |