Fix I/O callback example.

This commit is contained in:
Marcus Brinkmann 2011-05-12 14:45:46 +02:00
parent f61abeb0cf
commit 5f3de0bfff
2 changed files with 14 additions and 7 deletions

View File

@ -1,5 +1,7 @@
2011-05-12 Marcus Brinkmann <marcus@g10code.com> 2011-05-12 Marcus Brinkmann <marcus@g10code.com>
* gpgme.texi (I/O Callback Example): Fix example code.
* gpgme.texi (Generating Keys): Fix OpenPGP parameters and reference * gpgme.texi (Generating Keys): Fix OpenPGP parameters and reference
GPG and GPGSM manual. GPG and GPGSM manual.

View File

@ -5318,6 +5318,9 @@ structure because the number of file descriptors needed for a crypto
operation in @acronym{GPGME} is not predictable. operation in @acronym{GPGME} is not predictable.
@example @example
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h> #include <pthread.h>
#include <sys/types.h> #include <sys/types.h>
#include <gpgme.h> #include <gpgme.h>
@ -5337,6 +5340,7 @@ struct one_fd
int dir; int dir;
gpgme_io_cb_t fnc; gpgme_io_cb_t fnc;
void *fnc_data; void *fnc_data;
void *loop;
@}; @};
struct event_loop struct event_loop
@ -5368,6 +5372,7 @@ add_io_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc, void *fnc_data,
fds[i].dir = dir; fds[i].dir = dir;
fds[i].fnc = fnc; fds[i].fnc = fnc;
fds[i].fnc_data = fnc_data; fds[i].fnc_data = fnc_data;
fds[i].loop = loop;
break; break;
@} @}
@} @}
@ -5382,6 +5387,7 @@ void
remove_io_cb (void *tag) remove_io_cb (void *tag)
@{ @{
struct one_fd *fd = tag; struct one_fd *fd = tag;
struct event_loop *loop = fd->loop;
pthread_mutex_lock (&loop->lock); pthread_mutex_lock (&loop->lock);
fd->fd = -1; fd->fd = -1;
@ -5413,11 +5419,12 @@ do_select (struct event_loop *loop)
fd_set wfds; fd_set wfds;
int i, n; int i, n;
int any = 0; int any = 0;
struct one_fd *fdlist = loop->fds;
pthread_mutex_lock (&loop->lock); pthread_mutex_lock (&loop->lock);
FD_ZERO (&rfds); FD_ZERO (&rfds);
FD_ZERO (&wfds); FD_ZERO (&wfds);
for (i = 0; i < FDLIST_MAX; i++) for (i = 0; i < MAX_FDS; i++)
if (fdlist[i].fd != -1) if (fdlist[i].fd != -1)
FD_SET (fdlist[i].fd, fdlist[i].dir ? &rfds : &wfds); FD_SET (fdlist[i].fd, fdlist[i].dir ? &rfds : &wfds);
pthread_mutex_unlock (&loop->unlock); pthread_mutex_unlock (&loop->unlock);
@ -5432,7 +5439,7 @@ do_select (struct event_loop *loop)
return n; /* Error or timeout. */ return n; /* Error or timeout. */
pthread_mutex_lock (&loop->lock); pthread_mutex_lock (&loop->lock);
for (i = 0; i < FDLIST_MAX && n; i++) for (i = 0; i < MAX_FDS && n; i++)
@{ @{
if (fdlist[i].fd != -1) if (fdlist[i].fd != -1)
@{ @{
@ -5463,7 +5470,6 @@ wait_for_op (struct event_loop *loop, struct op_result *result)
ret = do_select (loop); ret = do_select (loop);
@} @}
while (ret >= 0 && !result->done); while (ret >= 0 && !result->done);
return ret;
@} @}
@end example @end example
@ -5478,7 +5484,6 @@ main (int argc, char *argv[])
gpgme_ctx_t ctx; gpgme_ctx_t ctx;
gpgme_error_t err; gpgme_error_t err;
gpgme_data_t sig, text; gpgme_data_t sig, text;
gpgme_sig_stat_t status;
int i; int i;
struct gpgme_io_cb_ts io_cbs = struct gpgme_io_cb_ts io_cbs =
@{ @{
@ -5492,7 +5497,7 @@ main (int argc, char *argv[])
init_gpgme (void); init_gpgme (void);
/* Initialize the loop structure. */ /* Initialize the loop structure. */
loop.lock = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_init (&loop.lock, NULL);
for (i = 0; i < MAX_FDS; i++) for (i = 0; i < MAX_FDS; i++)
loop->fds[i].fd = -1; loop->fds[i].fd = -1;
@ -5507,7 +5512,7 @@ main (int argc, char *argv[])
if (!err) if (!err)
@{ @{
gpgme_set_io_cbs (ctx, &io_cbs); gpgme_set_io_cbs (ctx, &io_cbs);
err = gpgme_op_verify_start (ctx, sig, text, &status); err = gpgme_op_verify_start (ctx, sig, text, NULL);
@} @}
if (err) if (err)
@{ @{
@ -5528,7 +5533,7 @@ main (int argc, char *argv[])
gpgme_strsource (result.err), gpgme_strerror (result.err)); gpgme_strsource (result.err), gpgme_strerror (result.err));
exit (1); exit (1);
@} @}
/* Evaluate STATUS. */ /* Evaluate verify result. */
@dots{} @dots{}
return 0; return 0;
@} @}