emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: GNU Emacs: Client/Server


From: Kim F. Storm
Subject: Re: GNU Emacs: Client/Server
Date: 30 Jan 2004 11:38:14 +0100
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

"Dhruva Krishnamurthy" <address@hidden> writes:

> Hello,
> On 29 Jan 2004 17:18:44 +0100, "Kim F. Storm" <address@hidden> said:
> > That's ok, but if you think in terms of the current mechanisms used by
> > process.c for network services, i.e. listen, accept, and select, how
> > hard/easy would it be to add mail slot handling to that, i.e. can you
> > wait on connections and/or input to a mail slot at the same time
> > as you wait for TCP connections using select?
> 
>  Yes, you can wait for connections in mail slots
> 
> > Maybe you can show me some W32 code which examplifies how a
> > multi-connection client and server application mixing normal
> > processes, network socket connections, and mail slot connections can
> > be written.
> 
> PS: I am not sending this mail to the list, not to annoy people who are
> not interested.

I don't have all the answers (and questions) on this subject, so it
is ok to CC: emacs-devel.

> 
> The code I have written to start a MAIL SLOT Server: It just processes 3
> messages, START, END and FLUSH (I use it to control profiling)

Thanks, but....

The problem with your code is how to mix it with code that must ALSO wait
on normal network receive and process output.  In your code, you have
two waiting-points (as I understand it:)

>     fResult=GetMailslotInfo(g_CRAMP_Profiler.g_h_mailslot,

which I assume waits for connections, and

>       fResult=ReadFile(g_CRAMP_Profiler.g_h_mailslot,

which I assume waits to receive data.


Both of these must --somehow-- be wrapped in such a way that they
become non-blocking operations and/or will function through the normal
"select" call which more or less combines all waiting points for async
processing in emacs into one.  Actually, I'm not sure select is
"normal" on W32, but that's the interface you need to emulate to add
mail-slot support to emacs'.


> 
> //-----------------------------------------------------------------------------
> // ProfilerMailSlotServerTH
> //-----------------------------------------------------------------------------
> DWORD WINAPI
> ProfilerMailSlotServerTH(LPVOID idata){
>   char filename[MAX_PATH];
>   sprintf(filename,"\\\\.\\mailslot\\cramp_mailslot#%ld",
>           GetCurrentProcessId());
> 
>   g_CRAMP_Profiler.g_h_mailslot=CreateMailslot(filename,
>                                                0,
>                                                MAILSLOT_WAIT_FOREVER,
>                                                NULL);
>   if(INVALID_HANDLE_VALUE==g_CRAMP_Profiler.g_h_mailslot)
>     return(-1);
> 
>   // Mail slot server loop
>   DWORD wstate=-1;
>   HANDLE h_event;
>   char msevtname[256];
> 
>   sprintf(msevtname,"CRAMP_MAILSLOT#%ld",GetCurrentProcessId());
>   h_event=CreateEvent(NULL,FALSE,FALSE,msevtname);
>   DEBUGCHK(h_event);
> 
>   while(1){
>     DWORD cbMessage=0,cMessage=0,cbRead=0;
>     BOOL fResult;
>     LPSTR lpszBuffer;
>     DWORD cAllMessages;
>     OVERLAPPED ov;
> 
>     ov.Offset=0;
>     ov.OffsetHigh=0;
>     ov.hEvent=h_event;
>     fResult=GetMailslotInfo(g_CRAMP_Profiler.g_h_mailslot,
>                             (LPDWORD)NULL,
>                             &cbMessage,
>                             &cMessage,
>                             (LPDWORD)NULL);
>     DEBUGCHK(fResult);
>     if(cbMessage==MAILSLOT_NO_MESSAGE)
>       continue;
>     cAllMessages=cMessage;
> 
>     // Mail slot loop
>     while(cMessage){
>       lpszBuffer=(LPSTR)GlobalAlloc(GPTR,cbMessage);
>       DEBUGCHK(lpszBuffer);
>       lpszBuffer[0]='\0';
>       fResult=ReadFile(g_CRAMP_Profiler.g_h_mailslot,
>                        lpszBuffer,
>                        cbMessage,
>                        &cbRead,
>                        &ov);
>       if(!fResult||!strlen(lpszBuffer))
>         break;
> 
>       // Process the message HERE
>       if(!stricmp(lpszBuffer,"STOP"))
>         CRAMP_DisableProfile();
>       else if(!stricmp(lpszBuffer,"START"))
>         CRAMP_EnableProfile();
>       else if(!stricmp(lpszBuffer,"FLUSH"))
>         CRAMP_FlushProfileLogs();
> 
>       GlobalFree((HGLOBAL)lpszBuffer);
>       fResult=GetMailslotInfo(g_CRAMP_Profiler.g_h_mailslot,
>                               (LPDWORD)NULL,
>                               &cbMessage,
>                               &cMessage,
>                               (LPDWORD)NULL);
>       if(!fResult)
>         break;
>     }
>   }
> 
>   return(0);
> }
> 
> The mail slot client: This communicates with the process on the computer
> specified
> //-----------------------------------------------------------------------------
> // WinMain
> //-----------------------------------------------------------------------------
> int
> WINAPI WinMain(HINSTANCE hinstExe,
>                HINSTANCE,
>                PSTR pszCmdLine,
>                int nCmdShow){
>   // Get the command line stuff
>   int argcW=0;
>   LPWSTR *argvW=0;
>   argvW=CommandLineToArgvW(GetCommandLineW(),&argcW);
>   if(!argvW)
>     return(-1);
>   if(argcW<4){
>     GlobalFree(argvW);
>     return(-1);
>   }
> 
>   // Currently supports only 1 arg
>   char *buff=0;
>   char pid[256];
>   char msg[256];
>   char comp[256];
>   char mspath[MAX_PATH];
>   WideCharToMultiByte(CP_ACP,0,argvW[1],-1,
>                       comp,256,0,0);
>   WideCharToMultiByte(CP_ACP,0,argvW[2],-1,
>                       pid,256,0,0);
>   WideCharToMultiByte(CP_ACP,0,argvW[3],-1,
>                       msg,256,0,0);
>   sprintf(mspath,"\\\\%s\\mailslot\\cramp_mailslot#%s",comp,pid);
> 
>   HANDLE h_file=0;
>   h_file=CreateFile(mspath,
>                     GENERIC_WRITE,
>                     FILE_SHARE_READ,
>                     NULL,
>                     OPEN_EXISTING,
>                     FILE_ATTRIBUTE_NORMAL,
>                     NULL);
>   if(h_file==INVALID_HANDLE_VALUE)
>     return(-1);
> 
>   DWORD cbWritten=0;
>   int ret=0;
>   ret=WriteFile(h_file,msg,strlen(msg)+1,&cbWritten,NULL);
>   CloseHandle(h_file);
>   h_file=0;
> 
>   return(!ret);
> }
> ________________________________________
> Dhruva Krishnamurthy
> Proud FSF member: #1935
> http://schemer.fateback.com/
> 
> 

-- 
Kim F. Storm <address@hidden> http://www.cua.dk





reply via email to

[Prev in Thread] Current Thread [Next in Thread]