Classic Procedural Applications
In this development model you have full control over (and responsibility for) how the application behaves. This model is most suited when you are porting existing C applications, or you want full control over program flow and events .
It is important to understand how to correctly implement a MoSync event loop. There three most important points to consider are:
- Checking for events often enough. The MoSync event queue is not infinite, so if you don't check often enough you might miss events.
- Using maWait() rather than busy-waiting, to conserve CPU usage and battery power.
- Responding to and handling the close event. After a close event is posted, your application will be forcibly terminated within a short period of time. However, your application should voluntarily exit as soon as possible, having saved any important data. (Note that after the close event has been posted, no further events will be posted and most syscalls will have no effect. See syscalls in the API Reference Guide for further information on this topic.
Example: A Simple, Well-Behaved Application
#include <ma.h>
int MAMain() {
// Application initialization goes here
for(;;) {
MAEvent e;
// Wait until we have an event
maWait(0);
// Process all events that have occured
while(maGetEvent(&e)) {
if(e.type == EVENT_TYPE_CLOSE) {
// do cleanup
maExit(0);
}
else if(e.type == EVENT_TYPE_KEY_PRESSED) {
// It's good practise to always provide one key
// to exit the application.
if(e.key == MAK_0) {
// do cleanup
maExit(0);
}
// handle other key presses
}
}
}
return 0;
}
This example only checks for key presses, but note that results of asynchronous operations are also passed as events and should be handled similarily. See the syscall reference for more information.
Working with connections in classic applications involves responding to events whose type is EVENT_TYPE_CONN. Connection operations are executed asynchronously, and these events are the way in which your application is notified of their progress, results and termination.
Example: A Classic application Using Connections
#include <maapi.h>
#include <conprint.h>
int MAMain() {
char buffer[160];
// Application initialization goes here.
InitConsole();
// Create a connection.
printf("Connecting...\n");
MAHandle conn = maConnect("http://www.example.com/"); // Will cause a CONNECT event.
// The event loop.
for(;;) {
MAEvent e;
// Wait until we have an event.
maWait(0);
// Process all events that have occured.
while(maGetEvent(&e)) {
if(e.type == EVENT_TYPE_CLOSE) {
maExit(0);
}
else if(e.type == EVENT_TYPE_KEY_PRESSED) {
if(e.key == MAK_0)
maExit(0);
}
else if(e.type == EVENT_TYPE_CONN) {
if(e.conn.opType == CONNOP_CONNECT) {
// The Connect operation is complete.
if(e.conn.result < 0) {
printf("Connect error %i\n", e.conn.result);
// Close the connection, freeing resources.
maConnClose(conn);
} else {
printf("HTTP result %i\n", e.conn.result);
// Start reading data.
maConnRead(conn, buffer, sizeof(buffer) - 1); // Will cause a CONN READ event.
}
} else if(e.conn.opType == CONNOP_READ) {
// The Read operation is complete.
if(e.conn.result == CONNERR_CLOSED) {
printf("Connection closed.\n");
maConnClose(conn);
} else if(e.conn.result < 0) {
printf("Read error %i\n", e.conn.result);
maConnClose(conn);
} else {
printf("Read %i bytes:\n", e.conn.result);
// Zero-terminate buffer.
buffer[e.conn.result] = 0;
PrintConsole(buffer);
// Read more data.
maConnRead(conn, buffer, sizeof(buffer) - 1);
}
}
}
}
}
return 0;
}
- Printer-friendly version
- Login or register to post comments
Share on Facebook