1
// **********************************************************************
3
// Copyright (c) 2003-2010 ZeroC, Inc. All rights reserved.
5
// This copy of Ice is licensed to you under the terms described in the
6
// ICE_LICENSE file included in this distribution.
8
// **********************************************************************
10
#include <Connection.h>
15
using namespace IcePHP;
17
ZEND_EXTERN_MODULE_GLOBALS(ice)
20
// Class entries represent the PHP class implementations we have registered.
22
static zend_class_entry* connectionClassEntry = 0;
24
static zend_class_entry* connectionInfoClassEntry = 0;
25
static zend_class_entry* ipConnectionInfoClassEntry = 0;
26
static zend_class_entry* tcpConnectionInfoClassEntry = 0;
27
static zend_class_entry* udpConnectionInfoClassEntry = 0;
30
// Ice::Connection support.
32
static zend_object_handlers _connectionHandlers;
33
static zend_object_handlers _connectionInfoHandlers;
37
static zend_object_value handleConnectionAlloc(zend_class_entry* TSRMLS_DC);
38
static void handleConnectionFreeStorage(void* TSRMLS_DC);
39
static int handleConnectionCompare(zval*, zval* TSRMLS_DC);
41
static zend_object_value handleConnectionInfoAlloc(zend_class_entry* TSRMLS_DC);
42
static void handleConnectionInfoFreeStorage(void* TSRMLS_DC);
45
ZEND_METHOD(Ice_Connection, __construct)
47
runtimeError("Connection cannot be instantiated" TSRMLS_CC);
50
ZEND_METHOD(Ice_Connection, __toString)
52
if(ZEND_NUM_ARGS() > 0)
57
Ice::ConnectionPtr _this = Wrapper<Ice::ConnectionPtr>::value(getThis() TSRMLS_CC);
62
string str = _this->toString();
63
RETURN_STRINGL(STRCAST(str.c_str()), str.length(), 1);
65
catch(const IceUtil::Exception& ex)
67
throwException(ex TSRMLS_CC);
72
ZEND_METHOD(Ice_Connection, close)
74
Ice::ConnectionPtr _this = Wrapper<Ice::ConnectionPtr>::value(getThis() TSRMLS_CC);
78
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("b"), &b TSRMLS_CC) != SUCCESS)
85
_this->close(b ? true : false);
87
catch(const IceUtil::Exception& ex)
89
throwException(ex TSRMLS_CC);
94
ZEND_METHOD(Ice_Connection, getEndpoint)
96
if(ZEND_NUM_ARGS() > 0)
101
Ice::ConnectionPtr _this = Wrapper<Ice::ConnectionPtr>::value(getThis() TSRMLS_CC);
106
if(!createEndpoint(return_value, _this->getEndpoint() TSRMLS_CC))
111
catch(const IceUtil::Exception& ex)
113
throwException(ex TSRMLS_CC);
118
ZEND_METHOD(Ice_Connection, flushBatchRequests)
120
if(ZEND_NUM_ARGS() > 0)
125
Ice::ConnectionPtr _this = Wrapper<Ice::ConnectionPtr>::value(getThis() TSRMLS_CC);
130
_this->flushBatchRequests();
132
catch(const IceUtil::Exception& ex)
134
throwException(ex TSRMLS_CC);
139
ZEND_METHOD(Ice_Connection, type)
141
if(ZEND_NUM_ARGS() > 0)
146
Ice::ConnectionPtr _this = Wrapper<Ice::ConnectionPtr>::value(getThis() TSRMLS_CC);
151
string str = _this->type();
152
RETURN_STRINGL(STRCAST(str.c_str()), str.length(), 1);
154
catch(const IceUtil::Exception& ex)
156
throwException(ex TSRMLS_CC);
161
ZEND_METHOD(Ice_Connection, timeout)
163
if(ZEND_NUM_ARGS() != 0)
168
Ice::ConnectionPtr _this = Wrapper<Ice::ConnectionPtr>::value(getThis() TSRMLS_CC);
173
Ice::Int timeout = _this->timeout();
174
ZVAL_LONG(return_value, static_cast<long>(timeout));
176
catch(const IceUtil::Exception& ex)
178
throwException(ex TSRMLS_CC);
183
ZEND_METHOD(Ice_Connection, toString)
185
ZEND_MN(Ice_Connection___toString)(INTERNAL_FUNCTION_PARAM_PASSTHRU);
188
ZEND_METHOD(Ice_Connection, getInfo)
190
if(ZEND_NUM_ARGS() != 0)
195
Ice::ConnectionPtr _this = Wrapper<Ice::ConnectionPtr>::value(getThis() TSRMLS_CC);
200
Ice::ConnectionInfoPtr info = _this->getInfo();
201
if(!createConnectionInfo(return_value, _this->getInfo() TSRMLS_CC))
206
catch(const IceUtil::Exception& ex)
208
throwException(ex TSRMLS_CC);
216
static zend_object_value
217
handleConnectionAlloc(zend_class_entry* ce TSRMLS_DC)
219
zend_object_value result;
221
Wrapper<Ice::ConnectionPtr>* obj = Wrapper<Ice::ConnectionPtr>::create(ce TSRMLS_CC);
224
result.handle = zend_objects_store_put(obj, 0, (zend_objects_free_object_storage_t)handleConnectionFreeStorage,
226
result.handlers = &_connectionHandlers;
235
handleConnectionFreeStorage(void* p TSRMLS_DC)
237
Wrapper<Ice::ConnectionPtr>* obj = static_cast<Wrapper<Ice::ConnectionPtr>*>(p);
239
zend_objects_free_object_storage(static_cast<zend_object*>(p) TSRMLS_CC);
246
handleConnectionCompare(zval* zobj1, zval* zobj2 TSRMLS_DC)
249
// PHP guarantees that the objects have the same class.
252
Ice::ConnectionPtr con1 = Wrapper<Ice::ConnectionPtr>::value(zobj1 TSRMLS_CC);
254
Ice::ConnectionPtr con2 = Wrapper<Ice::ConnectionPtr>::value(zobj2 TSRMLS_CC);
272
// Predefined methods for Connection.
274
static function_entry _interfaceMethods[] =
278
static function_entry _connectionClassMethods[] =
280
ZEND_ME(Ice_Connection, __construct, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
281
ZEND_ME(Ice_Connection, __toString, NULL, ZEND_ACC_PUBLIC)
282
ZEND_ME(Ice_Connection, close, NULL, ZEND_ACC_PUBLIC)
283
ZEND_ME(Ice_Connection, getEndpoint, NULL, ZEND_ACC_PUBLIC)
284
ZEND_ME(Ice_Connection, flushBatchRequests, NULL, ZEND_ACC_PUBLIC)
285
ZEND_ME(Ice_Connection, type, NULL, ZEND_ACC_PUBLIC)
286
ZEND_ME(Ice_Connection, timeout, NULL, ZEND_ACC_PUBLIC)
287
ZEND_ME(Ice_Connection, toString, NULL, ZEND_ACC_PUBLIC)
288
ZEND_ME(Ice_Connection, getInfo, NULL, ZEND_ACC_PUBLIC)
292
ZEND_METHOD(Ice_ConnectionInfo, __construct)
294
runtimeError("ConnectionInfo cannot be instantiated" TSRMLS_CC);
298
// Predefined methods for ConnectionInfo.
300
static function_entry _connectionInfoClassMethods[] =
302
ZEND_ME(Ice_ConnectionInfo, __construct, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
309
static zend_object_value
310
handleConnectionInfoAlloc(zend_class_entry* ce TSRMLS_DC)
312
zend_object_value result;
314
Wrapper<Ice::ConnectionInfoPtr>* obj = Wrapper<Ice::ConnectionInfoPtr>::create(ce TSRMLS_CC);
317
result.handle = zend_objects_store_put(obj, 0, (zend_objects_free_object_storage_t)handleConnectionInfoFreeStorage,
319
result.handlers = &_connectionInfoHandlers;
328
handleConnectionInfoFreeStorage(void* p TSRMLS_DC)
330
Wrapper<Ice::ConnectionInfoPtr>* obj = static_cast<Wrapper<Ice::ConnectionInfoPtr>*>(p);
332
zend_objects_free_object_storage(static_cast<zend_object*>(p) TSRMLS_CC);
336
IcePHP::connectionInit(TSRMLS_D)
339
// We register an interface and a class that implements the interface. This allows
340
// applications to safely include the Slice-generated code for the type.
344
// Register the Connection interface.
347
#ifdef ICEPHP_USE_NAMESPACES
348
INIT_NS_CLASS_ENTRY(ce, STRCAST("Ice"), STRCAST("Connection"), _interfaceMethods);
350
INIT_CLASS_ENTRY(ce, "Ice_Connection", _interfaceMethods);
352
zend_class_entry* interface = zend_register_internal_interface(&ce TSRMLS_CC);
355
// Register the Connection class.
357
INIT_CLASS_ENTRY(ce, "IcePHP_Connection", _connectionClassMethods);
358
ce.create_object = handleConnectionAlloc;
359
connectionClassEntry = zend_register_internal_class(&ce TSRMLS_CC);
360
memcpy(&_connectionHandlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
361
_connectionHandlers.compare_objects = handleConnectionCompare;
362
zend_class_implements(connectionClassEntry TSRMLS_CC, 1, interface);
365
// Register the ConnectionInfo class.
367
#ifdef ICEPHP_USE_NAMESPACES
368
INIT_NS_CLASS_ENTRY(ce, STRCAST("Ice"), STRCAST("ConnectionInfo"), _connectionInfoClassMethods);
370
INIT_CLASS_ENTRY(ce, "Ice_ConnectionInfo", _connectionInfoClassMethods);
372
ce.create_object = handleConnectionInfoAlloc;
373
connectionInfoClassEntry = zend_register_internal_class(&ce TSRMLS_CC);
374
memcpy(&_connectionInfoHandlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
375
zend_declare_property_bool(connectionInfoClassEntry, STRCAST("incoming"), sizeof("incoming") - 1, 0,
376
ZEND_ACC_PUBLIC TSRMLS_CC);
377
zend_declare_property_string(connectionInfoClassEntry, STRCAST("adapterName"), sizeof("adapterName") - 1,
378
STRCAST(""), ZEND_ACC_PUBLIC TSRMLS_CC);
381
// Register the IPConnectionInfo class.
383
#ifdef ICEPHP_USE_NAMESPACES
384
INIT_NS_CLASS_ENTRY(ce, STRCAST("Ice"), STRCAST("IPConnectionInfo"), NULL);
386
INIT_CLASS_ENTRY(ce, "Ice_IPConnectionInfo", NULL);
388
ce.create_object = handleConnectionInfoAlloc;
389
ipConnectionInfoClassEntry = zend_register_internal_class_ex(&ce, connectionInfoClassEntry, NULL TSRMLS_CC);
390
zend_declare_property_string(ipConnectionInfoClassEntry, STRCAST("localAddress"), sizeof("localAddress") - 1,
391
STRCAST(""), ZEND_ACC_PUBLIC TSRMLS_CC);
392
zend_declare_property_long(ipConnectionInfoClassEntry, STRCAST("localPort"), sizeof("localPort") - 1, 0,
393
ZEND_ACC_PUBLIC TSRMLS_CC);
394
zend_declare_property_string(ipConnectionInfoClassEntry, STRCAST("remoteAddress"), sizeof("remoteAddress") - 1,
395
STRCAST(""), ZEND_ACC_PUBLIC TSRMLS_CC);
396
zend_declare_property_long(ipConnectionInfoClassEntry, STRCAST("remotePort"), sizeof("remotePort") - 1, 0,
397
ZEND_ACC_PUBLIC TSRMLS_CC);
400
// Register the TCPConnectionInfo class.
402
#ifdef ICEPHP_USE_NAMESPACES
403
INIT_NS_CLASS_ENTRY(ce, STRCAST("Ice"), STRCAST("TCPConnectionInfo"), NULL);
405
INIT_CLASS_ENTRY(ce, "Ice_TCPConnectionInfo", NULL);
407
ce.create_object = handleConnectionInfoAlloc;
408
tcpConnectionInfoClassEntry = zend_register_internal_class_ex(&ce, ipConnectionInfoClassEntry, NULL TSRMLS_CC);
411
// Register the UDPConnectionInfo class.
413
#ifdef ICEPHP_USE_NAMESPACES
414
INIT_NS_CLASS_ENTRY(ce, STRCAST("Ice"), STRCAST("UDPConnectionInfo"), NULL);
416
INIT_CLASS_ENTRY(ce, "Ice_UDPConnectionInfo", NULL);
418
ce.create_object = handleConnectionInfoAlloc;
419
udpConnectionInfoClassEntry = zend_register_internal_class_ex(&ce, ipConnectionInfoClassEntry, NULL TSRMLS_CC);
420
zend_declare_property_string(udpConnectionInfoClassEntry, STRCAST("mcastAddress"), sizeof("mcastAddress") - 1,
421
STRCAST(""), ZEND_ACC_PUBLIC TSRMLS_CC);
422
zend_declare_property_long(udpConnectionInfoClassEntry, STRCAST("mcastPort"), sizeof("mcastPort") - 1, 0,
423
ZEND_ACC_PUBLIC TSRMLS_CC);
429
IcePHP::createConnection(zval* zv, const Ice::ConnectionPtr& p TSRMLS_DC)
431
if(object_init_ex(zv, connectionClassEntry) != SUCCESS)
433
runtimeError("unable to initialize connection" TSRMLS_CC);
437
Wrapper<Ice::ConnectionPtr>* obj = Wrapper<Ice::ConnectionPtr>::extract(zv TSRMLS_CC);
440
obj->ptr = new Ice::ConnectionPtr(p);
446
IcePHP::fetchConnection(zval* zv, Ice::ConnectionPtr& connection TSRMLS_DC)
454
if(Z_TYPE_P(zv) != IS_OBJECT || Z_OBJCE_P(zv) != connectionClassEntry)
456
invalidArgument("value is not a connection" TSRMLS_CC);
459
Wrapper<Ice::ConnectionPtr>* obj = Wrapper<Ice::ConnectionPtr>::extract(zv TSRMLS_CC);
464
connection = *obj->ptr;
470
IcePHP::createConnectionInfo(zval* zv, const Ice::ConnectionInfoPtr& p TSRMLS_DC)
473
if(Ice::TCPConnectionInfoPtr::dynamicCast(p))
475
status = object_init_ex(zv, tcpConnectionInfoClassEntry);
477
else if(Ice::UDPConnectionInfoPtr::dynamicCast(p))
479
Ice::UDPConnectionInfoPtr info = Ice::UDPConnectionInfoPtr::dynamicCast(p);
480
if((status = object_init_ex(zv, udpConnectionInfoClassEntry)) == SUCCESS)
482
add_property_string(zv, STRCAST("mcastAddress"), const_cast<char*>(info->mcastAddress.c_str()), 1);
483
add_property_long(zv, STRCAST("mcastPort"), static_cast<long>(info->mcastPort));
486
else if(Ice::IPConnectionInfoPtr::dynamicCast(p))
488
status = object_init_ex(zv, ipConnectionInfoClassEntry);
492
status = object_init_ex(zv, connectionInfoClassEntry);
495
if(status != SUCCESS)
497
runtimeError("unable to initialize connection info" TSRMLS_CC);
501
if(Ice::IPConnectionInfoPtr::dynamicCast(p))
503
Ice::IPConnectionInfoPtr info = Ice::IPConnectionInfoPtr::dynamicCast(p);
504
add_property_string(zv, STRCAST("localAddress"), const_cast<char*>(info->localAddress.c_str()), 1);
505
add_property_long(zv, STRCAST("localPort"), static_cast<long>(info->localPort));
506
add_property_string(zv, STRCAST("remoteAddress"), const_cast<char*>(info->remoteAddress.c_str()), 1);
507
add_property_long(zv, STRCAST("remotePort"), static_cast<long>(info->remotePort));
510
add_property_bool(zv, STRCAST("incoming"), p->incoming ? 1 : 0);
511
add_property_string(zv, STRCAST("adapterName"), const_cast<char*>(p->adapterName.c_str()), 1);
513
Wrapper<Ice::ConnectionInfoPtr>* obj = Wrapper<Ice::ConnectionInfoPtr>::extract(zv TSRMLS_CC);
516
obj->ptr = new Ice::ConnectionInfoPtr(p);