Async / sync class design question

Discussion in 'C++' started by Angus, Nov 5, 2006.

  1. Angus

    Angus Guest

    Hello

    I have written my own C++ class to handle winsock. It uses an asynchronous
    programming model so basically you ask for something and then when network
    has info it gets sent to my class and I can deal with it. Basically, a user
    class derives from this networking class and overrides functions such as
    OnCompleteMessage.

    Sometimes, however, it is more convenient for the user class if it can call
    a function which then provides the response on returning. Should I provide
    synchronous functions in my class? Or is this bad design? Instead should
    user class just work in same asynchronous manner? Or is there a good way to
    deal with this sync function requirement some other way?

    I know all this sounds vague but I am really looking for general design
    guidance. I am happy to provide more details on the class or the class
    itself if that would be useful.

    Angus
     
    Angus, Nov 5, 2006
    #1
    1. Advertising

  2. Angus

    Ian Collins Guest

    Angus wrote:
    > Hello
    >
    > I have written my own C++ class to handle winsock. It uses an asynchronous
    > programming model so basically you ask for something and then when network
    > has info it gets sent to my class and I can deal with it. Basically, a user
    > class derives from this networking class and overrides functions such as
    > OnCompleteMessage.
    >
    > Sometimes, however, it is more convenient for the user class if it can call
    > a function which then provides the response on returning. Should I provide
    > synchronous functions in my class? Or is this bad design? Instead should
    > user class just work in same asynchronous manner? Or is there a good way to
    > deal with this sync function requirement some other way?
    >

    That question depends on too many external considerations to have a
    single answer. Can the application do useful work while it is waiting
    for an asynchronous request to be processed? If so, it might as well
    use one. If not, it should use a synchronous request.

    Is the application event driven, or does it use threads?

    Provide both if you are unsure of the usage pattern.

    --
    Ian Collins.
     
    Ian Collins, Nov 5, 2006
    #2
    1. Advertising

  3. Angus

    Angus Guest

    In answer to your question, sometimes the application can do useful work
    while we wait. And the application is event driven.

    But if, for example, I need a response from the server to start processing
    something - eg:

    1. Request a string from server

    2. When we have string, do some string processing on the string.

    I need to wait for 1. to finish before I can start on 2. I need a sort of
    WaitForResponse function? Is that the way to do it?

    I have a function in the network class called OnCompleteMessage. But how do
    I know that the completemessage I get from this function is the particular
    one I asked for here. Do I send a request asking for some sort of
    requestid? Then wait until receive event that requestid x has completed?

    I am still hazy about how to best do all this.

    Angus



    "Ian Collins" <> wrote in message
    news:...
    > Angus wrote:
    > > Hello
    > >
    > > I have written my own C++ class to handle winsock. It uses an

    asynchronous
    > > programming model so basically you ask for something and then when

    network
    > > has info it gets sent to my class and I can deal with it. Basically, a

    user
    > > class derives from this networking class and overrides functions such as
    > > OnCompleteMessage.
    > >
    > > Sometimes, however, it is more convenient for the user class if it can

    call
    > > a function which then provides the response on returning. Should I

    provide
    > > synchronous functions in my class? Or is this bad design? Instead

    should
    > > user class just work in same asynchronous manner? Or is there a good

    way to
    > > deal with this sync function requirement some other way?
    > >

    > That question depends on too many external considerations to have a
    > single answer. Can the application do useful work while it is waiting
    > for an asynchronous request to be processed? If so, it might as well
    > use one. If not, it should use a synchronous request.
    >
    > Is the application event driven, or does it use threads?
    >
    > Provide both if you are unsure of the usage pattern.
    >
    > --
    > Ian Collins.
     
    Angus, Nov 5, 2006
    #3
  4. Angus

    Ian Collins Guest

    Angus wrote:

    Please don't top post.

    > "Ian Collins" <> wrote in message
    >>
    >>That question depends on too many external considerations to have a
    >>single answer. Can the application do useful work while it is waiting
    >>for an asynchronous request to be processed? If so, it might as well
    >>use one. If not, it should use a synchronous request.
    >>
    >>Is the application event driven, or does it use threads?
    >>
    >>Provide both if you are unsure of the usage pattern.
    >>

    > In answer to your question, sometimes the application can do useful work
    > while we wait. And the application is event driven.
    >
    > But if, for example, I need a response from the server to start processing
    > something - eg:
    >
    > 1. Request a string from server
    >
    > 2. When we have string, do some string processing on the string.
    >
    > I need to wait for 1. to finish before I can start on 2. I need a sort of
    > WaitForResponse function? Is that the way to do it?
    >

    Fairly typical situation where you have to block. You can do this with
    an async only interface, but it's clearer and safer to use a sync call.

    > I have a function in the network class called OnCompleteMessage. But how do
    > I know that the completemessage I get from this function is the particular
    > one I asked for here. Do I send a request asking for some sort of
    > requestid? Then wait until receive event that requestid x has completed?
    >
    > I am still hazy about how to best do all this.
    >

    Sounds like you have a long way to go. Restrict yourself to one
    outstanding message, get that working then tackle the reply/response
    matching issues.

    --
    Ian Collins.
     
    Ian Collins, Nov 5, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Niv
    Replies:
    9
    Views:
    845
    Mike Treseler
    Jan 5, 2006
  2. Sundar

    sync/async serial comm..

    Sundar, Nov 21, 2006, in forum: C Programming
    Replies:
    3
    Views:
    547
    osmium
    Nov 21, 2006
  3. barbara guidi

    sync vs async webservices

    barbara guidi, Jun 11, 2010, in forum: Java
    Replies:
    12
    Views:
    2,355
    Arved Sandstrom
    Jun 16, 2010
  4. Trans
    Replies:
    2
    Views:
    506
    Trans
    Dec 12, 2005
  5. Rm Rm

    http post is async or sync?

    Rm Rm, Jan 4, 2008, in forum: Ruby
    Replies:
    4
    Views:
    152
    Paul Stickney
    Jan 7, 2008
Loading...

Share This Page