home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************/
- /* */
- /* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
- /* ------------------------------- ------------------ */
- /* */
- /* Book: ACM System Amiga C Club */
- /* Chapter: Messages Tulevagen 22 */
- /* File: Example1A.c 181 41 LIDINGO */
- /* Author: Anders Bjerin SWEDEN */
- /* Date: 92-05-02 */
- /* Version: 1.00 */
- /* */
- /* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
- /* */
- /* Registered members may use this program freely in their */
- /* own commercial/noncommercial programs/articles. */
- /* */
- /***********************************************************/
-
- /* This program will create a message port called "NrPort". */
- /* It will then go to sleep and will first wake up when it */
- /* has received a message. It will collect the message, read */
- /* and alter it before it replies and the program terminates. */
-
-
-
- #include <exec/types.h> /* STRPTR */
- #include <exec/ports.h> /* struct Message */
- #include <exec/memory.h> /* MEMF_PUBLIC */
- #include <exec/nodes.h> /* NT_MESSAGE */
-
-
-
- /* Declare a pointer to our message port: */
- struct MsgPort *msgp;
-
- /* The message will come in this form: */
- struct NrMessage
- {
- struct Message SystemMsg;
- int number;
- };
-
- /* Declare a pointer to the message: */
- struct NrMessage *nrmsg;
-
-
-
- /* Declare the functions: */
- void clean_up();
- void main();
-
-
-
- void main()
- {
- /* Create a message port: (Name "NrPort", priority 0) */
- msgp = (struct MsgPort *) CreatePort( "NrPort", 0 );
-
- /* Check if we have successfully created a port: */
- if( !msgp )
- clean_up( "Could not create the message port!" );
-
-
-
- /* Wait for a message to arrive: */
- printf( "A: Waiting for a message to arrive...\n" );
- WaitPort( msgp );
-
-
-
- /* Now one or more messages have arrived, try to collect */
- /* as many messages as possible: */
- while( nrmsg = (struct NrMessage *) GetMsg( msgp ))
- {
- /* Read the message: */
- printf( "A: The value we received was: %d\n", nrmsg->number );
- printf( "A: Lets change it!\n" );
-
- /* Alter it: */
- nrmsg->number = 67890;
-
- /* Reply: */
- ReplyMsg( nrmsg );
-
- /* Note that after we have replied, we may not */
- /* read or alter the message any more! */
- }
-
-
-
- clean_up( "The End!" );
- }
-
-
-
- void clean_up( text )
- STRPTR text;
- {
- /* If we have successfully created a port, close it: */
- if( msgp )
- DeletePort( msgp);
-
- /* Print any message: */
- printf( "A: %s\n", text );
-
- /* Quit: */
- exit( 0 );
- }
-
-