Guidance
指路人
g.yi.org
Software / Reginald / Examples / mailslot / serverslot.rex

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
/* An example of using Reginald REXX to FUNCDEF
 * and use Windows Mail Slots.
 *
 * This is the script that runs on the server
 * computer. It creates a mailslot and waits for
 * records sent from the respective script that
 * runs on the client (ie, clientslot.rex).
 */

/* So that we don't need the CALL keyword */
ADDRESS null

/* Some OS functions deal with 32-bit addresses so we need
 * 10 digits to prevent dealing with exponential form
 */
NUMERIC DIGITS 10

/* ============== FUNCDEF some needed OS functions ============ */

/* Make things easy for us with WINFUNC option */
OPTIONS "WINFUNC NOSOURCE"

/* Trap FAILURE/SYNTAX so we don't have to check FUNCDEF for errors */
/* Trap HALT for user abort and cleanup */
SIGNAL ON FAILURE
SIGNAL ON SYNTAX
SIGNAL ON HALT

/* Register the Windows OS function CreateMailslot(). We tell it to check
 * for a return of -1, and if so, raise USER 1 condition.
 */
FUNCDEF("CreateMailslot", "void, str, 32u, 32, void", "kernel32",,"U1 -1")

/* Register the Windows OS function GetMailslotInfo(). We tell it to check
 * for a return of 0, and if so, raise USER 1 condition.
 */
FUNCDEF("GetMailslotInfo", "32, void, 32u *, 32 * stor, 32u * stor, 32u * dual", "kernel32",,"U1 0")

/* Register the Windows OS function ReadFile(). We tell it to check
 * for a return of 0, and if so, raise USER 1 condition.
 */
FUNCDEF("ReadFile", "32, void, void, 32u, 32u * stor, void", "kernel32",,"U1 0")

/* Register the Windows OS function CloseHandle() */
FUNCDEF("CloseHandle", "32, void", "kernel32")

/* First let's trap USER 1 so we don't have to error check
 * any of these calls to the above FUNCDEF'ed functions. Yay!
 */
SIGNAL ON user 1 NAME mailsloterror

/* Create a mail slot named testslot in the current directory,
 * allowing any size records to be received (up to 64K).
 * Specify that we want ReadFile() to wait forever when reading
 * a record.
 */
handle = createmailslot("\\.\mailslot\testslot", , -1)

/* Trap USER 1 so we don't have to error check with the
 * MailSlot functions
 */
SIGNAL ON user 1 NAME mailsloterror2

/* Trap SYNTAX in case CONVERTDATA has a memory allocation failure */
SIGNAL ON SYNTAX NAME mailsloterror2

again:
/* Wait for the next record to be received at the mailslot. Wait up
 * up to 10000 milliseconds before returning.
 */
err = 10000
CALL getmailslotinfo(handle, , msgsize, , err)

/* Here you may want to check for user aborting if using some sort
 * of graphical user interface, and SIGNAL to HALT. You may also want
 * to clear out messages at the window. For example, with REXX Dialog,
 * you'd do a RXMSG 'CLEAR' operation.
 */

/* Check if there was a record. If so, the size is not -1 */
DO WHILE msgsize \= -1

	/* Allocate memory to read in the record. If an error,
	 * REXX will raise SYNTAX
	 */
	buffer = CONVERTDATA(0, msgsize, , "A")

	/* Read the record */
	CALL readfile(handle, buffer, msgsize, numread)

	/* Make sure all the chars were read */
	IF msgsize \= numread THEN SAY "ERROR: The correct number of characters weren't received!"

	ELSE DO
		/* Assign the record chars to the REXX variable named "buffer" */
		CALL CONVERTDATA(buffer, "buffer", , "=")

		/* Display the received record. Here you'd normally do something useful with it */
		SAY buffer
	END

	/* Read the next message's size */
	CALL getmailslotinfo(handle, , msgsize)

END /* msgSize \= -1 */

/* Delay for 60 seconds so we don't eat up too much CPU time */
SLEEP(60)

/* Wait for next record */
SIGNAL again

mailsloterror2:
/* If we're here, then there was a problem reading a record. We'll
 * just report that and skip to the next record.
 */
SAY CONDITION('D') "failed:" UNIXERROR(RXFUNCERRMSG())
SIGNAL again

/* ======================= Error Handling ======================= */

mailsloterror:
	/* NOTE: For a USER condition raised by some FUNCDEF'ed function,
	 * CONDITION('D') fetches the name of function that was called.
	 * CONDITION('E') fetches the return value of the function. SIGL
	 * is the line number where the error occurred. RXFUNCERRMSG()
	 * can return any real error number from the operating system, and
	 * UNIXERROR() can display an appropriate message for that error.
	 */
	SAY CONDITION('D') "failed:" UNIXERROR(RXFUNCERRMSG())
	SIGNAL HALT

syntax:
failure:
    /* NOTE: CONDITION('D') fetches error message. CONDITION('E') fetches the
     * error number. SIGL is the line number where the error occurred.
     */
	CONDITION('M')

halt:
	/* Close the mailslot. Actually, Windows will do this for us if the
	 * program that also launched this script is ending now.
	 */
	IF SYMBOL(handle) == 'VAR' THEN closehandle(handle)
	RETURN
掌柜推荐
 
 
¥860.00 ·
 
 
¥900.00 ·
 
 
¥810.00 ·
 
 
¥317.00 ·
 
 
¥1,370.00 ·
 
 
¥660.00 ·
© Sun 2024-11-24  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2013-06-18 23:40:59