1998 2009 Ericsson AB. All Rights Reserved.
The contents of this file are subject to the Erlang Public License, Version 1.1, (the "License"); you may not use this file except in compliance with the License. You should have received a copy of the Erlang Public License along with this software. If not, it can be retrieved online at http://www.erlang.org/. Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License.

CORBA_Environment C Structure

2003-12-15
PC1 ch_c_corba_env.xml

This chapter describes the CORBA_Environment C structure.

## C Structure

Here is the complete definition of the CORBA_Environment C structure, defined in file "ic.h" :

`/* Environment definition */ typedef struct { /*----- CORBA compatibility part ------------------------*/ /* Exception tag, initially set to CORBA_NO_EXCEPTION ---*/ CORBA_exception_type _major; /*----- External Implementation part - initiated by the user ---*/ /* File descriptor */ int _fd; /* Size of input buffer */ int _inbufsz; /* Pointer to always dynamically allocated buffer for input */ char *_inbuf; /* Size of output buffer */ int _outbufsz; /* Pointer to always dynamically allocated buffer for output */ char *_outbuf; /* Size of memory chunks in bytes, used for increasing the output buffer, set to >= 32, should be around >= 1024 for performance reasons */ int _memchunk; /* Pointer for registered name */ char _regname[256]; /* Process identity for caller */ erlang_pid *_to_pid; /* Process identity for callee */ erlang_pid *_from_pid; /*- Internal Implementation part - used by the server/client ---*/ /* Index for input buffer */ int _iin; /* Index for output buffer */ int _iout; /* Pointer for operation name */ char _operation[256]; /* Used to count parameters */ int _received; /* Used to identify the caller */ erlang_pid _caller; /* Used to identify the call */ erlang_ref _unique; /* Exception id field */ CORBA_char *_exc_id; /* Exception value field */ void *_exc_value; } CORBA_Environment;`

The structure is divided into three parts:

The CORBA Compatibility part, demanded by the standard OMG IDL mapping v2.0.

The external implementation part used for generated client/server code.

The internal part useful for those who wish to define their own functions.

## The CORBA Compatibility Part

Contains only one field _major defined as a CORBA_Exception_type. The CORBA_Exception type is an integer which can be one of:

CORBA_NO_EXCEPTION, by default equal to 0, can be set by the application programmer to another value.

CORBA_SYSTEM_EXCEPTION, by default equal to -1, can be set by the application programmer to another value.

The current definition of these values are:

`#define CORBA_NO_EXCEPTION 0 #define CORBA_SYSTEM_EXCEPTION -1`

## The External Part

This part contains the following fields:

int _fd - a file descriptor returned from erl_connect. Used for connection setting.

char* _inbuf - pointer to a buffer used for input. Buffer size checks are done under runtime that prevent buffer overflows. This is done by expanding the buffer to fit the input message. In order to allow buffer reallocation, the output buffer must always be dynamically allocated. The pointer value can change under runtime in case of buffer reallocation.

int _inbufsz - start size of input buffer. Used for setting the input buffer size under initialization of the Erl_Interface function ei_receive_encoded/5. The value of this field can change under runtime in case of input buffer expansion to fit larger messages

int _outbufsz - start size of output buffer. The value of this field can change under runtime in case of input buffer expansion to fit larger messages

char* _outbuf - pointer to a buffer used for output. Buffer size checks prevent buffer overflows under runtime, by expanding the buffer to fit the output message in cases of lack of space in buffer. In order to allow buffer reallocation, the output buffer must always be dynamically allocated. The pointer value can change under runtime in case of buffer reallocation.

int _memchunk - expansion unit size for the output buffer. This is the size of memory chunks in bytes used for increasing the output in case of buffer expansion. The value of this field must be always set to >= 32, should be at least 1024 for performance reasons.

char regname[256] - a registered name for a process.

erlang_pid* _to_pid - an Erlang process identifier, is only used if the registered_name parameter is the empty string.

erlang_pid* _from_pid - your own process id so the answer can be returned.

## The Internal Part

This part contains the following fields:

int _iin - Index for input buffer. Initially set to zero. Updated to agree with the length of the received encoded message.

int _iout - Index for output buffer Initially set to zero. Updated to agree with the length of the message encoded to the communication counterpart.

char _operation[256] - Pointer for operation name. Set to the operation to be called.

int _received - Used to count parameters. Initially set to zero.

erlang_pid _caller - Used to identify the caller. Initiated to a value that identifies the caller.

erlang_ref _unique - Used to identify the call. Set to a default value in the case of generated functions.

CORBA_char* _exc_id - Exception id field. Initially set to NULL to agree with the initial value of _major (CORBA_NO_EXCEPTION).

void* _exc_value - Exception value field Initially set to NULL to agree with the initial value of _major (CORBA_NO_EXCEPTION).

The advanced user who defines his own functions has to update/support these values in a way similar to how they are updated in the generated code.

## Creating and Initiating the CORBA_Environment Structure

There are two ways to set the CORBA_Environment structure:

Manually

The following default values must be set to the CORBA_Environment *ev fields, when buffers for input/output should have the size inbufsz/ outbufsz:

ev->_inbufsz = inbufsz;

The value for this field can be between 0 and maximum size of a signed integer.

ev->_inbuf = malloc(inbufsz);

The size of the allocated buffer must be equal to the value of its corresponding index, _inbufsz.

ev->_outbufsz = outbufsz;

The value for this field can be between 0 and maximum size of a signed integer.

ev->_outbuf = malloc(outbufsz);

The size of the allocated buffer must be equal to the value of its corresponding index, _outbufsz.

ev->_memchunk = __OE_MEMCHUNK__;

Please note that __OE_MEMCHUNK__ is equal to 1024, you can set this value to a value bigger than 32 yourself.

ev->_to_pid = NULL;

ev->_from_pid = NULL;

By using the CORBA_Environment_alloc/2 function.

The CORBA_Environment_alloc function is defined as:

`CORBA_Environment *CORBA_Environment_alloc(int inbufsz, int outbufsz);`

where:

inbufsz is the desired size of input buffer

outbufsz is the desired size of output buffer

return value is a pointer to an allocated and initialized CORBA_Environment structure.

This function will set all needed default values and allocate buffers equal to the values passed, but will not allocate space for the _to_pid and _from_pid fields.

To free the space allocated by CORBA_Environment_alloc/2:

First call CORBA_free for the input and output buffers.

After freeing the buffer space, call CORBA_free for the CORBA_Environment space.

Remember to set the fields _fd, _regname, *_to_pid and/or *_from_pid to the appropriate application values. These are not automatically set by the stubs.

Never assign static buffers to the buffer pointers. Never set the _memchunk field to a value less than 32.

## Setting System Exceptions

If the user wishes to set own system exceptions at critical positions on the code, it is strongly recommended to use one of the current values:

CORBA_NO_EXCEPTION upon success. The value of the _exc_id field should be then set to NULL. The value of the _exc_value field should be then set to NULL.

CORBA_SYSTEM_EXCEPTION upon system failure. The value of the _exc_id field should be then set to one of the values defined in "ic.h" :

`#define UNKNOWN "UNKNOWN" #define BAD_PARAM "BAD_PARAM" #define NO_MEMORY "NO_MEMORY" #define IMPL_LIMIT "IMP_LIMIT" #define COMM_FAILURE "COMM_FAILURE" #define INV_OBJREF "INV_OBJREF" #define NO_PERMISSION "NO_PERMISSION" #define INTERNAL "INTERNAL" #define MARSHAL "MARSHAL" #define INITIALIZE "INITIALIZE" #define NO_IMPLEMENT "NO_IMPLEMENT" #define BAD_TYPECODE "BAD_TYPECODE" #define BAD_OPERATION "BAD_OPERATION" #define NO_RESOURCES "NO_RESOURCES" #define NO_RESPONSE "NO_RESPONSE" #define PERSIST_STORE "PERSIST_STORE" #define BAD_INV_ORDER "BAD_INV_ORDER" #define TRANSIENT "TRANSIENT" #define FREE_MEM "FREE_MEM" #define INV_IDENT "INV_IDENT" #define INV_FLAG "INV_FLAG" #define INTF_REPOS "INTF_REPOS" #define BAD_CONTEXT "BAD_CONTEXT" #define OBJ_ADAPTER "OBJ_ADAPTER" #define DATA_CONVERSION "DATA_CONVERSION" #define OBJ_NOT_EXIST "OBJECT_NOT_EXIST"`

The value of the _exc_value field should be then set to a string that explains the problem in an informative way. The user should use the functions CORBA_exc_set/4 and CORBA_exception_free/1 to free the exception. The user has to use CORBA_exception_id/1 and CORBA_exception_value/1 to access exception information. Prototypes for these functions are declared in "ic.h"