Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] Event and socket messages ?

This thread is locked; no one can reply to it. rss feed Print
[A5] Event and socket messages ?
dt65536
Member #15,540
March 2014

Hi everybody,

I am beginning a new (game) project, and I'm using both Allegro and network communication for the first time. The documentation of Allegro is easy and complete so I have a good time using it, but I still have just notions of how a network works (I am still learning about what sockets are exactly).

I am now at the point where I need the game client to retrieve data such as position of players sent from a server.
So, as in my main loop I am waiting for an event, using :
al_wait_for_event(ALLEGRO_EVENT_QUEUE, ALLEGRO_EVENT*);
and as this function "freezes" the program, I can't call such things as 'recv' or 'recvfrom' to process the data I should receive,
I imagined I could create an user event which would be emitted whenever data was retrieved.

Sadly, I couldn't find a similar question or an example about events and network, so I am referring to your wisdom.

My questions are :
- Is it the "proper way" to handle data communication over a network ?
- if not, what method should I use ?
- if yes, how do I have to "emit" my event ? (do I have to use multithreading (which I have never used either) ? Is there a way to "overload" the al_wait_for_event function ?)

I apologize if the terms I used aren't exact (I hope it's still understandable).

Thanks in advance for your answers.

edit : I omitted to mention I wish to use only C language (so no objects).

Thomas Fjellstrom
Member #476
June 2000
avatar

If you don't feel comfortable making your networking code feed an allegro event queue, you can use al_wait_for_event_timed or al_get_next_event, and handle your own waiting.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

dt65536
Member #15,540
March 2014

Thanks for your reply,
The fact is I have no idea what method to use, as both creating my own waiting loop and using the threading seems it can work.
Anyway, I'll stick to using al_wait_for_event and create an ALLEGRO_THREAD, I'm not sure how to create a loop that wakes the program up only if an event is sent or a message is received.

Is there any project to one day support network communications and sockets as a part of Allegro (either as an addon or in the core) ?

Thomas Fjellstrom
Member #476
June 2000
avatar

If you go the thread route, you can send your own custom messages to your main loop's event queue to wake it up and handle whatever you need to handle. (see: al_init_user_event_source and al_emit_user_event)

dt65536 said:

I'm not sure how to create a loop that wakes the program up only if an event is sent or a message is received.

event queue's will wake al_wait_for_event up when they get an event, no matter who sends the event.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Go to: