mbsget_dyna.c
来自「Hulft-Message是用来在Internet上传输文件数据的商用Middl」· C语言 代码 · 共 342 行
C
342 行
/* ---------------------------------------------------------------------------------
Program name : mbsget_dyna.c
Description : Sample C program that load the library dynamically.
(C) Copyright SAISON INFORMATION SYSTEMS CO.,LTD. 2004 All Rights Reserved.
--------------------------------------------------------------------------------- */
/*----------------------------------------------------------------------------------
Function :
"mbsget_dyna" is a sample C program to get a message from a queue. This
program gets a message with "TEST" as UserDefineID from specified queue,
and writes this to StdOut.
This program has the following parameters. If the optional parameters
are omitted, the parameters in System-Environment file( mbcenv.conf or MBCENV )
will be used.
required :
optional :
(1) -s Server : The name of HULFT-Message Server
(2) -p Port : The port of HULFT-Message Server
(3) -q Queue : The name of the queue
--------------------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(WIN)
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#include "mbapi.h"
#define usage "Usage : mbsget_dyna [-s Server] [-p Port] [-q Queue]"
#define BUFFER_SHORTAGE 10003 /* API error code */
typedef MBHD* (CALLING API_MBCONN)( MBCON *, size_t, MBERR * );
typedef int (CALLING API_MBDISCON)( MBHD *, MBERR * );
typedef MBQHD* (CALLING API_MBOPEN)( MBHD *, MBQ *, size_t, MBERR * );
typedef int (CALLING API_MBCLOSE)( MBHD *, MBQHD *, MBERR * );
typedef int (CALLING API_MBGET)( MBHD *, MBQHD *, MBMSG *, size_t, MBERR * );
/* Global valiables */
API_MBCONN *MBconnect;
API_MBDISCON *MBdisconnect;
API_MBOPEN *MBopen;
API_MBCLOSE *MBclose;
API_MBGET *MBget;
#if defined(WIN)
HMODULE lib;
#else
void *lib;
#endif
/* Local function prottypes */
int init_library();
void free_library();
int main( int argc, char **argv )
{
/* ---------------------------------------------------------------------------------
Declare structures needed
--------------------------------------------------------------------------------- */
MBHD *Hconn; /* connection handle */
MBQHD *Hque; /* queue handle */
MBCON Conn; /* MBCON structure */
MBQ Que; /* MBQ structure */
MBMSG Msg; /* MBMSG structure */
MBERR err; /* MBERR structure */
int rc; /* return code */
unsigned short port; /* server port */
char server[MBA_HOST_NAME_LEN+1]; /* server name */
char queue[MBA_QUEUE_NAME_LEN+1]; /* queue name */
char user_define_id[] = "TEST"; /* user definition ID */
/* ---------------------------------------------------------------------------------
Initialize
--------------------------------------------------------------------------------- */
memset( server, 0x00, sizeof(server) );
memset( queue, 0x00, sizeof(queue) );
port = 0;
/* ---------------------------------------------------------------------------------
Check the arguments
--------------------------------------------------------------------------------- */
if( argc > 7 ){
printf( "%s\n", usage );
exit( -1 );
}
argv++;
argc--;
while( argc-- ){
if( !strcmp( *argv, "-s" )){ /* server name */
if( argc <= 0 ){
printf( "%s\n", usage );
exit( -1 );
}
argv++;
argc--;
strncpy( server, *argv, MBA_HOST_NAME_LEN );
}
else if( !strcmp( *argv, "-p" )){ /* server port */
if( argc <= 0 ){
printf( "%s\n", usage );
exit( -1 );
}
argv++;
argc--;
port = atoi( *argv );
}
else if( !strcmp( *argv, "-q" )){ /* queue name */
if( argc <= 0 ){
printf( "%s\n", usage );
exit( -1 );
}
argv++;
argc--;
strncpy( queue, *argv, MBA_QUEUE_NAME_LEN );
}
else{
printf( "%s\n", usage );
exit( -1 );
}
argv++;
}
/* ---------------------------------------------------------------------------------
Initialize library
--------------------------------------------------------------------------------- */
if( init_library() < 0 ) {
exit( -1 );
}
/* ---------------------------------------------------------------------------------
Connect to HULFT-Message Server
--------------------------------------------------------------------------------- */
memset( &Conn, 0x00, sizeof( Conn ));
strcpy( Conn.Server, server ); /* server name */
Conn.Port = port; /* server port */
Hconn = MBconnect( &Conn, sizeof( Conn ), &err );
if( !Hconn ){ /* if API returns unsuccessfully */
printf( "mbconnect : err=%d dtl=%d sys=%d msg=%s\n",
err.ErrCode, err.DtlCode, err.SysCode, err.ErrMsg );
free_library();
exit( -1 );
}
/* ---------------------------------------------------------------------------------
Open the queue
--------------------------------------------------------------------------------- */
memset( &Que, 0x00, sizeof( Que ));
strcpy( Que.Queue, queue ); /* queue name */
Hque = MBopen( Hconn, &Que, sizeof( Que ), &err );
if( !Hque ){ /* if API returns unsuccessfully */
printf( "mbopen : err=%d dtl=%d sys=%d msg=%s\n",
err.ErrCode, err.DtlCode, err.SysCode, err.ErrMsg );
MBdisconnect( Hconn, &err );
free_library();
exit( -1 );
}
/* ---------------------------------------------------------------------------------
Get the message from the queue
--------------------------------------------------------------------------------- */
memset( &Msg, 0x00, sizeof( Msg ));
strcpy( Msg.UserDefineID, user_define_id ); /* user definition ID */
Msg.Size = 256; /* message buffer size */
Msg.Buf = malloc( Msg.Size+1 ); /* message buffer */
if( !Msg.Buf ){
printf( "There is no available memory.\n" );
MBclose( Hconn, Hque, &err );
MBdisconnect( Hconn, &err );
free_library();
exit( -1 );
}
memset( Msg.Buf, 0x00, Msg.Size+1 );
while( 1 ){
rc = MBget( Hconn, Hque, &Msg, sizeof( Msg ), &err );
if( err.ErrCode == BUFFER_SHORTAGE ){ /* if Msg.Size is too short */
Msg.Size = Msg.MsgSize;
Msg.Buf = realloc( Msg.Buf, Msg.Size+1 );
if( !Msg.Buf ){
printf( "There is no available memory.\n" );
MBclose( Hconn, Hque, &err );
MBdisconnect( Hconn, &err );
free_library();
exit( -1 );
}
memset( Msg.Buf, 0x00, Msg.Size+1 );
}
else{
break;
}
}
if( rc ){ /* if API returns unsuccessfully */
printf( "mbget : err=%d dtl=%d sys=%d msg=%s\n",
err.ErrCode, err.DtlCode, err.SysCode, err.ErrMsg );
free( Msg.Buf );
MBclose( Hconn, Hque, &err );
MBdisconnect( Hconn, &err );
free_library();
exit( -1 );
}
printf( ">> " );
puts( Msg.Buf );
free( Msg.Buf );
/* --------------------------------------------------------------------------------
Close the queue
--------------------------------------------------------------------------------- */
rc = MBclose( Hconn, Hque, &err );
if( rc ){ /* if API returns unsuccessfully */
printf( "mbclose : err=%d dtl=%d sys=%d msg=%s\n",
err.ErrCode, err.DtlCode, err.SysCode, err.ErrMsg );
MBdisconnect( Hconn, &err );
free_library();
exit( -1 );
}
/* ---------------------------------------------------------------------------------
Disconnect from HULFT-Message Server
--------------------------------------------------------------------------------- */
rc = MBdisconnect( Hconn, &err );
if( rc ){ /* if API returns unsuccessfully */
printf( "mbdisconnect : err=%d dtl=%d sys=%d msg=%s\n",
err.ErrCode, err.DtlCode, err.SysCode, err.ErrMsg );
free_library();
exit( -1 );
}
printf( "Normal termination\n" );
free_library();
return( 0 );
}
int init_library() {
/* ---------------------------------------------------------------------------------
Load library
--------------------------------------------------------------------------------- */
#if defined(WIN)
lib = LoadLibrary( "libmsg.dll" );
#else
lib = dlopen( "libmsg.so", RTLD_LAZY );
#endif
if( lib == NULL ) {
printf( "load library error\n" );
return( -1 );
}
/* ---------------------------------------------------------------------------------
Get functions address
--------------------------------------------------------------------------------- */
/* MBCONN */
#if defined(WIN)
MBconnect = (API_MBCONN *)GetProcAddress( lib, "MBCONN" );
#else
MBconnect = (API_MBCONN *)dlsym( lib, "MBCONN" );
#endif
if( MBconnect == NULL ) {
printf( "get proc address error: MBCONN\n" );
free_library();
return( -1 );
}
/* MBDISCON */
#if defined(WIN)
MBdisconnect = (API_MBDISCON *)GetProcAddress( lib, "MBDISCON" );
#else
MBdisconnect = (API_MBDISCON *)dlsym( lib, "MBDISCON" );
#endif
if( MBdisconnect == NULL ) {
printf( "get proc address error: MBDISCON\n" );
free_library();
return( -1 );
}
/* MBOPEN */
#if defined(WIN)
MBopen = (API_MBOPEN *)GetProcAddress( lib, "MBOPEN" );
#else
MBopen = (API_MBOPEN *)dlsym( lib, "MBOPEN" );
#endif
if( MBopen == NULL ) {
printf( "get proc address error: MBOPEN\n" );
free_library();
return( -1 );
}
/* MBCLOSE */
#if defined(WIN)
MBclose = (API_MBCLOSE *)GetProcAddress( lib, "MBCLOSE" );
#else
MBclose = (API_MBCLOSE *)dlsym( lib, "MBCLOSE" );
#endif
if( MBclose == NULL ) {
printf( "get proc address error: MBCLOSE\n" );
free_library();
return( -1 );
}
/* MBGET */
#if defined(WIN)
MBget = (API_MBGET *)GetProcAddress( lib, "MBGET" );
#else
MBget = (API_MBGET *)dlsym( lib, "MBGET" );
#endif
if( MBget == NULL ) {
printf( "get proc address error: MBGET\n" );
free_library();
return( -1 );
}
return 0;
}
void free_library() {
/* ---------------------------------------------------------------------------------
Free library
--------------------------------------------------------------------------------- */
#if defined(WIN)
FreeLibrary( lib );
#else
dlclose( lib );
#endif
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?