diff options
-rw-r--r-- | src/libcoopgamma.py | 11 | ||||
-rw-r--r-- | src/libcoopgamma_native.pyx.gpp | 37 |
2 files changed, 33 insertions, 15 deletions
diff --git a/src/libcoopgamma.py b/src/libcoopgamma.py index c75ed04..ee0ec63 100644 --- a/src/libcoopgamma.py +++ b/src/libcoopgamma.py @@ -720,7 +720,7 @@ class Context: params = (self.fd, data) return 'libcoopgamma.Context(%s)' % ', '.join(repr(p) for p in params) - def connect(self, method, site): + def connect(self, method = None, site = None): ''' Connect to a coopgamma server, and start it if necessary @@ -736,12 +736,13 @@ class Context: ''' if method is not None and isinstance(method, int): method = str(method) - error = llibcoopgamma_native.ibcoopgamma_native_connect(method, site, self.address) - if error is not None: - if error == 0: + (successful, value) = libcoopgamma_native.libcoopgamma_native_connect(method, site, self.address) + if not successful: + if value == 0: raise ServerInitialisationError() else: - raise ErrorReport.create_error(error) + raise ErrorReport.create_error(value) + self.fd = value def detach(self): ''' diff --git a/src/libcoopgamma_native.pyx.gpp b/src/libcoopgamma_native.pyx.gpp index 1947981..b673340 100644 --- a/src/libcoopgamma_native.pyx.gpp +++ b/src/libcoopgamma_native.pyx.gpp @@ -989,7 +989,7 @@ def libcoopgamma_native_context_marshal(address : int): return int(errno) try: libcoopgamma_context_marshal(this, buf) - return bytes(<int>(buf[i]) for i in range(int(n))) + return bytes(int(<int><unsigned char>(buf[i])) for i in range(int(n))) finally: free(buf) @@ -1085,7 +1085,7 @@ def libcoopgamma_native_async_context_marshal(address : int): if buf is NULL: return int(errno) libcoopgamma_async_context_marshal(this, buf) - ret = bytes(<int>(buf[i]) for i in range(int(n))) + ret = bytes(int(<int><unsigned char>(buf[i])) for i in range(int(n))) finally: free(buf) return ret @@ -1133,16 +1133,33 @@ def libcoopgamma_native_connect(method : str, site : str, address : int): SIGCHLD must not be ignored or blocked - @param method:str? The adjustment method, `NULL` for automatic - @param site:str? The site, `NULL` for automatic - @param address:int The address of the state of the library, must be initialised - @return :int? `None`, 0 if the server on could not be initialised, - the value `errno` on other errors + @param method:str? The adjustment method, `NULL` for automatic + @param site:str? The site, `NULL` for automatic + @param address:int The address of the state of the library, must be initialised + @return :(:bool, :int) If [0] = `True`: [1] is the socket's file descriptor. + If [0] = `False`: [1] is 0 if the server on could not be + initialised, or the value `errno` on other errors ''' + cdef char* cmethod = NULL + cdef char* csite = NULL cdef libcoopgamma_context_t* ctx = <libcoopgamma_context_t*><void*><intptr_t>address - if libcoopgamma_connect(method, site, ctx) < 0: - return int(errno) - return None + try: + if method is not None: + buf = method.encode('utf-8') + bytes([0]) + cmethod = <char*>malloc(len(buf) * sizeof(char)) + for i in range(len(buf)): + cmethod[i] = <char>(buf[i]) + if site is not None: + buf = site.encode('utf-8') + bytes([0]) + csite = <char*>malloc(len(buf) * sizeof(char)) + for i in range(len(buf)): + csite[i] = <char>(buf[i]) + if libcoopgamma_connect(cmethod, csite, ctx) < 0: + return (False, int(errno)) + return (True, int(ctx.fd)) + finally: + free(cmethod) + free(csite) def libcoopgamma_native_set_nonblocking(address : int, nonblocking : bool): |