wxFile Read doesn't block [solved] Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Tony0945
Experienced Solver
Experienced Solver
Posts: 96
Joined: Wed Oct 21, 2009 4:02 pm

wxFile Read doesn't block [solved]

Post by Tony0945 » Tue Mar 08, 2016 5:11 pm

On windows, I'm trying to simulate a Linux named pipe. But instead of blocking on the read from an empty file, the application is saturating one core of the CPU furiously reading NULL from the empty file (drive light lights up brightly too). I determined this with gdb. My research has not revealed how to set the file read mode to blocking. I'd rather not use sockets because of the need to determine a safe port number.

My code is here.

Code: Select all

#include <cstdlib>
#include <iostream>

#include <wx/stdpaths.h>
#include <wx/datetime.h>
#include <wx/string.h>
#include <wx/init.h>
#include <wx/utils.h>
#include <wx/file.h>

#include "Database.h"

#ifdef __WXMSW__
#define PIPENAME wxT("c:\\fakepipe.txt")
#else
#define PIPENAME "/opt/oregano/daemonpipe"
#endif

int main(int argc, char *argv[])
{
    wxInitializer ini;
    
    //create the pipe
    wxFile file(PIPENAME);
#ifndef __WXMSW__
    if (wxExecute("mkfifo "PIPENAME,wxEXEC_SYNC))
#else
    if (file.Create(PIPENAME))
#endif
    {
        exit(wxSysErrorCode());
    }
    if  (!file.Open(PIPENAME))
    {
        exit(wxSysErrorCode());
    }
    // initializedb
    theDB = new  Database();
#ifdef __WXMSW__
    wxString DBPath=wxStandardPaths::Get().GetUserDataDir()+"\\tvguide.db";
#else
    wxString DBPath=wxStandardPaths::Get().GetUserDataDir()+"/tvguide.db";
#endif
    if (!(theDB->OpenDataBase(DBPath)))
    {
        wxMessageBox("FATAL ERROR! Cannot Open or Create "+DBPath); 
        exit(wxSysErrorCode());
    }
    theDB->checkdatabase();
    
    for(;;)  // Main loop
    {
        // check databse and adjust next recording accordingly
            //TODO
        
        //wait for a message from the pipe
        for(;;)
        {
            char c;
            file.Read(&c,1);  // should block here but doesn't
            if (c=='u') break; // 
            else if (c=='s')
            {
                //close db and exit
                theDB->Close();
                exit(0);
            }
        }
    }
}
Last edited by Tony0945 on Tue Mar 15, 2016 3:04 pm, edited 1 time in total.

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2291
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: wxFile Read doesn't block

Post by evstevemd » Wed Mar 09, 2016 4:02 pm

Saying what you want to do can help much. Your explanations aren't clear as to what you are trying to accomplish!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 19.04/Windows 10 Pro/MacOS 10.13 - GCC/MinGW/Clang, CodeLite IDE]

Nunki
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 202
Joined: Fri Sep 14, 2012 8:26 am
Location: Kontich, Belgium
Contact:

Re: wxFile Read doesn't block

Post by Nunki » Tue Mar 15, 2016 1:19 pm

As from my former experience with a big software project we too chose Named pipes at first. As it turned out to be present on multiple platforms (Windows, Unix, linux). However later on we discovered that what MS understands as Named Pipes is not what unix Named Pipes are. And as such you end up with code that is loaded with #ifdef... So we had to turn to sockets for communication.
I don't quite get what you mean with not using sockets because of the need of a safe port number. It's not the port that makes you application/communication safe. It's the protocol you design/use to do your communication that makes up for your safety tmho.

regards,
Nunki

Nunki
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 202
Joined: Fri Sep 14, 2012 8:26 am
Location: Kontich, Belgium
Contact:

Re: wxFile Read doesn't block

Post by Nunki » Tue Mar 15, 2016 1:23 pm

Maybe this link is an interesting read for your case:

http://docs.wxwidgets.org/trunk/overview_ipc.html

Regards,
Nunki

Tony0945
Experienced Solver
Experienced Solver
Posts: 96
Joined: Wed Oct 21, 2009 4:02 pm

Re: wxFile Read doesn't block

Post by Tony0945 » Tue Mar 15, 2016 3:03 pm

Thanks to all. What I meant by "safe port number" was a port number not used by another program. I implemented the program with sockets, the daemon listening for connections on localhost (and blocking! Whew!), opening the connection, reading a single character, then closing the connection. There are, at present, only two messages, "(u)update", and "(s)shutdown", actual data transmission being via a shared database. The user program notifys the daemon, via "update' that it has made a change to the database. "Shutdown" is used by OpenRC to stop the daemon on shutdown. The same code works on Windows and Linux, but I do have the problem of finding a unique port number. I would have preferred reading a string, but that entails allocating a buffer and dealing with possible buffer overflow.

It appears that Windows continually polls, there is no blocking read.

Nunki
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 202
Joined: Fri Sep 14, 2012 8:26 am
Location: Kontich, Belgium
Contact:

Re: wxFile Read doesn't block [solved]

Post by Nunki » Mon Mar 21, 2016 9:34 am

This link

https://en.wikipedia.org/wiki/List_of_T ... rt_numbers

May be handy when you would consider using sockets. This is a list with all TCP ports currently known to be used. Anything below 1024 is reserved and may not be used by you as an application programmer. Anything above 1024 up to 65535 is at your disposal. However you could easily use the same port of another application, providing that application is not on your pc. Nor will ever be.

regards,
Nunki

Post Reply