Re: [lazarus] Simple IPC implementation.

2005-08-30 Thread Vincent Snijders

Marc Weustink wrote:

Vincent Snijders wrote:


Micha Nelissen wrote:


On Tue, 30 Aug 2005 11:09:30 +0200 (Romance Daylight Time)
Michael Van Canneyt [EMAIL PROTECTED] wrote:


In windows, it's slightly more difficult (surprise, surprise :-) )

You can use MsgWaitForMultipleObjects to do this for a windows
handle, or use WaitForSingleObject on a simple file handle.





You can't wait for file handles in windows.


AFAIK you can wait for file handles, but not for pipe handles.



AFAIK we wait here for pipehandles all the time



And how are you doing that?

In lazarus the following code is used (more or less):
repeat
  Windows.PeekNamedPipe(PipeHnd, nil, 0, nil, @TotalBytesAvailable, nil);
  if TotalBytesAvailable0 then break;
  Application.ProcessMessages
  Sleep(10);
until false;

IMHO, that is not really waiting.

Vincent.

_
To unsubscribe: mail [EMAIL PROTECTED] with
   unsubscribe as the Subject
  archives at http://www.lazarus.freepascal.org/mailarchives


Re: [lazarus] Simple IPC implementation.

2005-08-30 Thread Michael Van Canneyt


On Tue, 30 Aug 2005, Micha Nelissen wrote:

 On Tue, 30 Aug 2005 11:09:30 +0200 (Romance Daylight Time)
 Michael Van Canneyt [EMAIL PROTECTED] wrote:

  On Tue, 30 Aug 2005, Micha Nelissen wrote:
 
   What do you mean with Idle handler? TApplication.OnIdle ? In any sane app,
   that is only called once when the user goes idle; after that, the app is
   blocked waiting for user input.
 
  Hm.
 
  So that is not usable.
 
  On Unix, it's easy:
 
  I once wrote a GTK version of a debug server.
  You can tell GTK to watch for input on a file descriptor in it's main loop.
  If there is input on your descriptor, a callback you specify is called.

 Yes, this is used in the gtk widgetset interface in the LCL too, now;
 that's what WakeMainThread does there (send a byte down that pipe).

  In windows, it's slightly more difficult (surprise, surprise :-) )
 
  You can use MsgWaitForMultipleObjects to do this for a windows
  handle, or use WaitForSingleObject on a simple file handle.

 You can't wait for file handles in windows.

 OTOH, the backend of the IPC server will use messages to copy things
 around ? So the message queue would be woken up that way.

Yes.


  (Why they made this two separate functions is a mystery to me,
  it makes things horribly complicated...)

 Some threads have no message queue, so MsgWait... is more expensive, would
 require a message queue to be created.

  So in fact, what we'd need is a pair of calls in TApplication :
 
  TApplication.AddWatch(AHandle: THandle; Handler : TNotifyEvent);
  TApplication.RemoveWatch(AHandle: THandle);
 
  Which would handle both things transparantly in the message loop.
  (the standard IsWindow function of Windows could be used to determine what
  kind of handle it is in a windows app)
 
  This would come in handy in many applications.

 Hmm, I'm not sure. Can you give examples? A THandle is a widget-specific
 thingy, it doesn't have the same semantics on various platforms, so it's not
 directly usable by users of LCL (if you want one source code base).

 Things you could use this function for: file change notifications,
 events, ... all have to be seperately implemented in the FCL/LCL to make
 them work universally and well.

Seems so, yes.

Sebastian Guenther once started such a thing (fpAsync) but never managed
to get the windows side of things working, exactly because of the
different types of handle.

Michael.

_
 To unsubscribe: mail [EMAIL PROTECTED] with
unsubscribe as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives


Re: [lazarus] Simple IPC implementation.

2005-08-30 Thread Vincent Snijders

Micha Nelissen wrote:

On Tue, 30 Aug 2005 11:09:30 +0200 (Romance Daylight Time)
Michael Van Canneyt [EMAIL PROTECTED] wrote:


In windows, it's slightly more difficult (surprise, surprise :-) )

You can use MsgWaitForMultipleObjects to do this for a windows
handle, or use WaitForSingleObject on a simple file handle.



You can't wait for file handles in windows.


AFAIK you can wait for file handles, but not for pipe handles.

Vincent.

_
To unsubscribe: mail [EMAIL PROTECTED] with
   unsubscribe as the Subject
  archives at http://www.lazarus.freepascal.org/mailarchives


Re: [lazarus] Simple IPC implementation.

2005-08-30 Thread Michael Van Canneyt


On Tue, 30 Aug 2005, Vincent Snijders wrote:

 Micha Nelissen wrote:
  On Tue, 30 Aug 2005 11:09:30 +0200 (Romance Daylight Time)
  Michael Van Canneyt [EMAIL PROTECTED] wrote:
 
 In windows, it's slightly more difficult (surprise, surprise :-) )
 
 You can use MsgWaitForMultipleObjects to do this for a windows
 handle, or use WaitForSingleObject on a simple file handle.
 
 
  You can't wait for file handles in windows.
 
 AFAIK you can wait for file handles, but not for pipe handles.

You can wait on any handle: file, pipe, socket.

From msdn:

The WaitForSingleObject function can wait for the following objects:

* Change notification
* Console input
* Event
* Job
* Memory resource notification
* Mutex
* Process
* Semaphore
* Thread
* Waitable timer

The MsgWaitForMultipleObjects function can specify handles of any of the 
following object types in the pHandles array:

* Change notification
* Console input
* Event
* Job
* Memory resource notification
* Mutex
* Process
* Semaphore
* Thread
* Waitable timer

Michael.

_
 To unsubscribe: mail [EMAIL PROTECTED] with
unsubscribe as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives


Re: [lazarus] Simple IPC implementation.

2005-08-30 Thread Micha Nelissen
On Tue, 30 Aug 2005 12:20:40 +0200 (Romance Daylight Time)
Michael Van Canneyt [EMAIL PROTECTED] wrote:

 You can wait on any handle: file, pipe, socket.
 
 From msdn:
 
 The WaitForSingleObject function can wait for the following objects:
 
 * Change notification
 * Console input
 * Event
 * Job
 * Memory resource notification
 * Mutex
 * Process
 * Semaphore
 * Thread
 * Waitable timer

Well, I don't see neither file, nor pipe, nor socket in this list.
(That's why I made that specific comment, earlier). The documentation says
waiting for file and pipe handles works in some circumstances but it
should not be used.

Micha

_
 To unsubscribe: mail [EMAIL PROTECTED] with
unsubscribe as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives


Re: [lazarus] Simple IPC implementation.

2005-08-30 Thread Micha Nelissen
On Tue, 30 Aug 2005 11:09:30 +0200 (Romance Daylight Time)
Michael Van Canneyt [EMAIL PROTECTED] wrote:

 On Tue, 30 Aug 2005, Micha Nelissen wrote:
 
  What do you mean with Idle handler? TApplication.OnIdle ? In any sane app,
  that is only called once when the user goes idle; after that, the app is
  blocked waiting for user input.
 
 Hm.
 
 So that is not usable.
 
 On Unix, it's easy:
 
 I once wrote a GTK version of a debug server.
 You can tell GTK to watch for input on a file descriptor in it's main loop.
 If there is input on your descriptor, a callback you specify is called.

Yes, this is used in the gtk widgetset interface in the LCL too, now;
that's what WakeMainThread does there (send a byte down that pipe).

 In windows, it's slightly more difficult (surprise, surprise :-) )
 
 You can use MsgWaitForMultipleObjects to do this for a windows
 handle, or use WaitForSingleObject on a simple file handle.

You can't wait for file handles in windows.

OTOH, the backend of the IPC server will use messages to copy things
around ? So the message queue would be woken up that way.

 (Why they made this two separate functions is a mystery to me,
 it makes things horribly complicated...)

Some threads have no message queue, so MsgWait... is more expensive, would
require a message queue to be created.
 
 So in fact, what we'd need is a pair of calls in TApplication :
 
 TApplication.AddWatch(AHandle: THandle; Handler : TNotifyEvent);
 TApplication.RemoveWatch(AHandle: THandle);
 
 Which would handle both things transparantly in the message loop.
 (the standard IsWindow function of Windows could be used to determine what
 kind of handle it is in a windows app)
 
 This would come in handy in many applications.

Hmm, I'm not sure. Can you give examples? A THandle is a widget-specific
thingy, it doesn't have the same semantics on various platforms, so it's not
directly usable by users of LCL (if you want one source code base). 

Things you could use this function for: file change notifications,
events, ... all have to be seperately implemented in the FCL/LCL to make
them work universally and well.

Micha

_
 To unsubscribe: mail [EMAIL PROTECTED] with
unsubscribe as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives


Re: [lazarus] Simple IPC implementation.

2005-08-30 Thread Florian Klaempfl
Vincent Snijders wrote:

 
 And how are you doing that?
 
 In lazarus the following code is used (more or less):
 repeat
   Windows.PeekNamedPipe(PipeHnd, nil, 0, nil, @TotalBytesAvailable, nil);
   if TotalBytesAvailable0 then break;
   Application.ProcessMessages
   Sleep(10);
 until false;

Shouldn't a sleep(1) or sleep(0) make things more responsive? Giving up the
current time slice is usually enough to make a thread consuming virtually zero
cpu load.

_
 To unsubscribe: mail [EMAIL PROTECTED] with
unsubscribe as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives


Re: [lazarus] Simple IPC implementation.

2005-08-30 Thread Florian Klaempfl
Micha Nelissen wrote:

 On Tue, 30 Aug 2005 12:23:06 +0200 (Romance Daylight Time)
 Michael Van Canneyt [EMAIL PROTECTED] wrote:
 
 
Things you could use this function for: file change notifications,
events, ... all have to be seperately implemented in the FCL/LCL to make
them work universally and well.

Seems so, yes.

Sebastian Guenther once started such a thing (fpAsync) but never managed
to get the windows side of things working, exactly because of the
different types of handle.
 
 
 Hmm, is this work published somewhere?

http://svn.freepascal.org/svn/fpc/trunk/fcl/inc/fpasync.pp

_
 To unsubscribe: mail [EMAIL PROTECTED] with
unsubscribe as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives


Re: [lazarus] Simple IPC implementation.

2005-08-30 Thread Marc Weustink

Vincent Snijders wrote:

Marc Weustink wrote:


Vincent Snijders wrote:


Micha Nelissen wrote:


On Tue, 30 Aug 2005 11:09:30 +0200 (Romance Daylight Time)
Michael Van Canneyt [EMAIL PROTECTED] wrote:


In windows, it's slightly more difficult (surprise, surprise :-) )

You can use MsgWaitForMultipleObjects to do this for a windows
handle, or use WaitForSingleObject on a simple file handle.






You can't wait for file handles in windows.


AFAIK you can wait for file handles, but not for pipe handles.




AFAIK we wait here for pipehandles all the time



And how are you doing that?

In lazarus the following code is used (more or less):
repeat
  Windows.PeekNamedPipe(PipeHnd, nil, 0, nil, @TotalBytesAvailable, nil);
  if TotalBytesAvailable0 then break;
  Application.ProcessMessages
  Sleep(10);
until false;

IMHO, that is not really waiting.


Nope.

I dived a bit into it, it is not really my code. I think you create a 
wait with the following concept, using overlapped IO


Use ReadFileEx(hPipe, buf, nNumberOfBytesToRead, lpOverlapped, 
lpCompletionRoutine) to read from the pipe.

Then call MsgWaitForMultipleObjectEx to enter an alertable wait state
In the lpCompletionRoutine you can set an event to signal that the pipe 
read is finished.


Hmm... now I notice that this is NT+ only :(

Marc

_
To unsubscribe: mail [EMAIL PROTECTED] with
   unsubscribe as the Subject
  archives at http://www.lazarus.freepascal.org/mailarchives


Re: [lazarus] Simple IPC implementation.

2005-08-30 Thread Michael Van Canneyt


On Tue, 30 Aug 2005, Micha Nelissen wrote:

 On Tue, 30 Aug 2005 12:20:40 +0200 (Romance Daylight Time)
 Michael Van Canneyt [EMAIL PROTECTED] wrote:

  You can wait on any handle: file, pipe, socket.
 
  From msdn:
 
  The WaitForSingleObject function can wait for the following objects:
 
  * Change notification
  * Console input
  * Event
  * Job
  * Memory resource notification
  * Mutex
  * Process
  * Semaphore
  * Thread
  * Waitable timer

 Well, I don't see neither file, nor pipe, nor socket in this list.
 (That's why I made that specific comment, earlier). The documentation says
 waiting for file and pipe handles works in some circumstances but it
 should not be used.

It works. They even forgot to mention sockets.
The point is that it interferes with the message queue. in other words,
while using WaitForSingleObject(), your app will not respond to messages.

That is why they recommend MsgWaitForMultipleObjects(), this should work on 
both,
and will warn about files and about arrived messages.

In each case, I have everything working now both on Windows and Linux,
both command-line and GUI apps. I'll commit the SimpleIPC implementation
in the FCL.

I can send the rest (GUI demo apps  debugserver stuff) to the lazarus team,
if they want.

Michael.

_
 To unsubscribe: mail [EMAIL PROTECTED] with
unsubscribe as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives


Re: [lazarus] Simple IPC implementation.

2005-08-30 Thread Michael Van Canneyt


On Tue, 30 Aug 2005, Vincent Snijders wrote:

 Michael Van Canneyt wrote:
 
  On Tue, 30 Aug 2005, Vincent Snijders wrote:
 
 
 Micha Nelissen wrote:
 
 On Tue, 30 Aug 2005 11:09:30 +0200 (Romance Daylight Time)
 Michael Van Canneyt [EMAIL PROTECTED] wrote:
 
 
 In windows, it's slightly more difficult (surprise, surprise :-) )
 
 You can use MsgWaitForMultipleObjects to do this for a windows
 handle, or use WaitForSingleObject on a simple file handle.
 
 
 You can't wait for file handles in windows.
 
 
 AFAIK you can wait for file handles, but not for pipe handles.
 
 
  You can wait on any handle: file, pipe, socket.
 
  From msdn:
 
  The WaitForSingleObject function can wait for the following objects:
 
  * Change notification
  * Console input
  * Event
  * Job
  * Memory resource notification
  * Mutex
  * Process
  * Semaphore
  * Thread
  * Waitable timer
 
  The MsgWaitForMultipleObjects function can specify handles of any of the 
  following object types in the pHandles array:
 
  * Change notification
  * Console input
  * Event
  * Job
  * Memory resource notification
  * Mutex
  * Process
  * Semaphore
  * Thread
  * Waitable timer
 

 Didn't you notice pipe is missing in both lists? Or is pipe called
 differently?

Both pipes and TCP/IP sockets are missing in this list.

Michael.

_
 To unsubscribe: mail [EMAIL PROTECTED] with
unsubscribe as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives


Re: [lazarus] Simple IPC implementation.

2005-08-30 Thread Michael Van Canneyt


On Tue, 30 Aug 2005, Micha Nelissen wrote:

 On Tue, 30 Aug 2005 12:23:06 +0200 (Romance Daylight Time)
 Michael Van Canneyt [EMAIL PROTECTED] wrote:

   Things you could use this function for: file change notifications,
   events, ... all have to be seperately implemented in the FCL/LCL to make
   them work universally and well.
 
  Seems so, yes.
 
  Sebastian Guenther once started such a thing (fpAsync) but never managed
  to get the windows side of things working, exactly because of the
  different types of handle.

 Hmm, is this work published somewhere?

Yes.

fcl/inc/fpasync.pp
packages/base/libasync

Michael.

_
 To unsubscribe: mail [EMAIL PROTECTED] with
unsubscribe as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives


Re: [lazarus] Simple IPC implementation.

2005-08-29 Thread Michael Van Canneyt


On Mon, 29 Aug 2005, Micha Nelissen wrote:

 On Mon, 29 Aug 2005 16:32:42 +0200 (CEST)
 Michael Van Canneyt [EMAIL PROTECTED] wrote:
 
  3. Currently a loop must be implemented with a timer. 
 (the server app shows how)
 Any suggestions on how to integrate it in a message loop
 would be appreciated. Then a simple 'Loop' property would 
 be enough.
 
 Do you know the TThread.Synchronize eventloop hooks ? (WakeMainThread and
 friends). I think this can be done similarly ?

I was thinking along the following lines: the server object
hooks in the Idle handler of the application, and when the 
application goes idle, it checks if a message is available.
if so, it reads it and calls OnMessage. 

This could be done in a Lazarus-specific descendent of the 
server component.

If possible, I'd like to avoid mutexes and the like, they require the 
thread manager, and hence cthreads...

Michael.

_
 To unsubscribe: mail [EMAIL PROTECTED] with
unsubscribe as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives


Re: [lazarus] Simple IPC implementation.

2005-08-29 Thread Michael Van Canneyt


On Mon, 29 Aug 2005, Micha Nelissen wrote:

 On Mon, 29 Aug 2005 23:22:53 +0200 (CEST)
 Michael Van Canneyt [EMAIL PROTECTED] wrote:
 
  On Mon, 29 Aug 2005, Micha Nelissen wrote:
  
   Do you know the TThread.Synchronize eventloop hooks ? (WakeMainThread and
   friends). I think this can be done similarly ?
  
  I was thinking along the following lines: the server object
  hooks in the Idle handler of the application, and when the 
  application goes idle, it checks if a message is available.
  if so, it reads it and calls OnMessage. 
 
 This is not enough. The hook you describe is a CheckMessage procedure.
 You also need a procedural var (like WakeMainThread), which the gtk and
 win32 widgetsets can handle when a new message arrives, and thus to wake
 the main thread loop waiting for a user action.
 
 Otherwise all ipc message would be ignored until the user does something
 (not what you want most of the cases).

But isn't that is what the Idle handler is for ? 
It's called when the user does nothing ?
That is how all actions work, after all...

Michael.

_
 To unsubscribe: mail [EMAIL PROTECTED] with
unsubscribe as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives