本文整理汇总了C++中PQsocket函数的典型用法代码示例。如果您正苦于以下问题:C++ PQsocket函数的具体用法?C++ PQsocket怎么用?C++ PQsocket使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PQsocket函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: pgut_wait
int
pgut_wait(int num, PGconn *connections[], struct timeval *timeout)
{
/* all connections are busy. wait for finish */
while (!interrupted)
{
int i;
fd_set mask;
int maxsock;
FD_ZERO(&mask);
maxsock = -1;
for (i = 0; i < num; i++)
{
int sock;
if (connections[i] == NULL)
continue;
sock = PQsocket(connections[i]);
if (sock >= 0)
{
FD_SET(sock, &mask);
if (maxsock < sock)
maxsock = sock;
}
}
if (maxsock == -1)
{
errno = ENOENT;
return -1;
}
i = wait_for_sockets(maxsock + 1, &mask, timeout);
if (i == 0)
break; /* timeout */
for (i = 0; i < num; i++)
{
if (connections[i] && FD_ISSET(PQsocket(connections[i]), &mask))
{
PQconsumeInput(connections[i]);
if (PQisBusy(connections[i]))
continue;
return i;
}
}
}
errno = EINTR;
return -1;
}
示例2: libpq_select
/*
* Wait until we can read WAL stream, or timeout.
*
* Returns true if data has become available for reading, false if timed out
* or interrupted by signal.
*
* This is based on pqSocketCheck.
*/
static bool
libpq_select(int timeout_ms)
{
int ret;
Assert(streamConn != NULL);
if (PQsocket(streamConn) < 0)
ereport(ERROR,
(errcode_for_socket_access(),
errmsg("socket not open")));
/* We use poll(2) if available, otherwise select(2) */
{
#ifdef HAVE_POLL
struct pollfd input_fd;
input_fd.fd = PQsocket(streamConn);
input_fd.events = POLLIN | POLLERR;
input_fd.revents = 0;
ret = poll(&input_fd, 1, timeout_ms);
#else /* !HAVE_POLL */
fd_set input_mask;
struct timeval timeout;
struct timeval *ptr_timeout;
FD_ZERO(&input_mask);
FD_SET(PQsocket(streamConn), &input_mask);
if (timeout_ms < 0)
ptr_timeout = NULL;
else
{
timeout.tv_sec = timeout_ms / 1000;
timeout.tv_usec = (timeout_ms % 1000) * 1000;
ptr_timeout = &timeout;
}
ret = select(PQsocket(streamConn) + 1, &input_mask,
NULL, NULL, ptr_timeout);
#endif /* HAVE_POLL */
}
if (ret == 0 || (ret < 0 && errno == EINTR))
return false;
if (ret < 0)
ereport(ERROR,
(errcode_for_socket_access(),
errmsg("select() failed: %m")));
return true;
}
示例3: DoConnect
bool DoConnect()
{
sql = PQconnectStart(GetDSN().c_str());
if (!sql)
return false;
if(PQstatus(sql) == CONNECTION_BAD)
return false;
if(PQsetnonblocking(sql, 1) == -1)
return false;
/* OK, we've initalised the connection, now to get it hooked into the socket engine
* and then start polling it.
*/
this->fd = PQsocket(sql);
if(this->fd <= -1)
return false;
if (!ServerInstance->SE->AddFd(this, FD_WANT_NO_WRITE | FD_WANT_NO_READ))
{
ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "BUG: Couldn't add pgsql socket to socket engine");
return false;
}
/* Socket all hooked into the engine, now to tell PgSQL to start connecting */
return DoPoll();
}
示例4: evpg_connect
int
evpg_connect(struct evpg_cfg *config, const char *connstr)
{
int pgsock;
int status;
struct evpg_db_node *dbnode;
void **usrdata;
if (!(dbnode = calloc(sizeof(struct evpg_db_node), 1)))
return -1;
dbnode->dbconn = PQconnectStart(connstr);
pgsock = PQsocket(dbnode->dbconn);
/* set this dbnode into an active state since it is not
ready to be used by the calling application */
evpg_set_active(config, dbnode);
/* we want to pass both our config, and our node */
usrdata = malloc(sizeof(void *) * 2);
usrdata[0] = config;
usrdata[1] = dbnode;
/* start the non-blocking connect event */
event_set(&dbnode->event, pgsock, EV_WRITE,
(void *)evpg_connect_check, usrdata);
event_add(&dbnode->event, 0);
}
示例5: SWITCH_DECLARE
SWITCH_DECLARE(switch_pgsql_status_t) switch_pgsql_handle_connect(switch_pgsql_handle_t *handle)
{
#ifdef SWITCH_HAVE_PGSQL
if (handle->state == SWITCH_PGSQL_STATE_CONNECTED) {
switch_pgsql_handle_disconnect(handle);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Re-connecting %s\n", handle->dsn);
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Connecting %s\n", handle->dsn);
handle->con = PQconnectdb(handle->dsn);
if (PQstatus(handle->con) != CONNECTION_OK) {
char *err_str;
if ((err_str = switch_pgsql_handle_get_error(handle))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "%s\n", err_str);
switch_safe_free(err_str);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to connect to the database [%s]\n", handle->dsn);
switch_pgsql_handle_disconnect(handle);
}
return SWITCH_PGSQL_FAIL;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Connected to [%s]\n", handle->dsn);
handle->state = SWITCH_PGSQL_STATE_CONNECTED;
handle->sock = PQsocket(handle->con);
return SWITCH_PGSQL_SUCCESS;
#else
return SWITCH_PGSQL_FAIL;
#endif
}
示例6: do_postgres_cCommand_execute_async
PGresult * do_postgres_cCommand_execute_async(VALUE self, VALUE connection, PGconn *db, VALUE query) {
PGresult *response;
char* str = StringValuePtr(query);
while ((response = PQgetResult(db))) {
PQclear(response);
}
struct timeval start;
int retval;
gettimeofday(&start, NULL);
retval = PQsendQuery(db, str);
if (!retval) {
if (PQstatus(db) != CONNECTION_OK) {
PQreset(db);
if (PQstatus(db) == CONNECTION_OK) {
retval = PQsendQuery(db, str);
}
else {
do_postgres_full_connect(connection, db);
retval = PQsendQuery(db, str);
}
}
if (!retval) {
rb_raise(eDO_ConnectionError, "%s", PQerrorMessage(db));
}
}
int socket_fd = PQsocket(db);
fd_set rset;
while (1) {
FD_ZERO(&rset);
FD_SET(socket_fd, &rset);
retval = rb_thread_select(socket_fd + 1, &rset, NULL, NULL, NULL);
if (retval < 0) {
rb_sys_fail(0);
}
if (retval == 0) {
continue;
}
if (PQconsumeInput(db) == 0) {
rb_raise(eDO_ConnectionError, "%s", PQerrorMessage(db));
}
if (PQisBusy(db) == 0) {
break;
}
}
data_objects_debug(connection, query, &start);
return PQgetResult(db);
}
示例7: set_connection_status_bad
/*
* set_connection_status_bad does not remove the given connection from the connection hash.
* It simply shuts down the underlying socket. On success, it returns true.
*/
Datum
set_connection_status_bad(PG_FUNCTION_ARGS)
{
char *nodeName = PG_GETARG_CSTRING(0);
int32 nodePort = PG_GETARG_INT32(1);
int socket = -1;
int shutdownStatus = 0;
int pqStatus PG_USED_FOR_ASSERTS_ONLY = 0;
PGconn *connection = GetOrEstablishConnection(nodeName, nodePort);
if (connection == NULL)
{
PG_RETURN_BOOL(false);
}
/* Prevent further reads/writes... */
socket = PQsocket(connection);
shutdownStatus = shutdown(socket, SHUT_RDWR);
if (shutdownStatus != 0)
{
ereport(ERROR, (errcode_for_socket_access(), errmsg("shutdown failed")));
}
/* ... and make libpq notice by reading data. */
pqStatus = PQconsumeInput(connection);
Assert(pqStatus == 0); /* expect failure */
PG_RETURN_BOOL(true);
}
示例8: MultiClientRegisterWait
/*
* MultiClientRegisterWait adds a connection to be waited upon, waiting for
* executionStatus.
*/
void
MultiClientRegisterWait(WaitInfo *waitInfo, TaskExecutionStatus executionStatus,
int32 connectionId)
{
PGconn *connection = NULL;
struct pollfd *pollfd = NULL;
Assert(waitInfo->registeredWaiters < waitInfo->maxWaiters);
if (executionStatus == TASK_STATUS_READY)
{
waitInfo->haveReadyWaiter = true;
return;
}
else if (executionStatus == TASK_STATUS_ERROR)
{
waitInfo->haveFailedWaiter = true;
return;
}
connection = ClientConnectionArray[connectionId];
pollfd = &waitInfo->pollfds[waitInfo->registeredWaiters];
pollfd->fd = PQsocket(connection);
if (executionStatus == TASK_STATUS_SOCKET_READ)
{
pollfd->events = POLLERR | POLLIN;
}
else if (executionStatus == TASK_STATUS_SOCKET_WRITE)
{
pollfd->events = POLLERR | POLLOUT;
}
waitInfo->registeredWaiters++;
}
示例9: PgStartNotifyEventSource
void
PgStartNotifyEventSource(Pg_ConnectionId * connid)
{
/* Start the notify event source if it isn't already running */
if (!connid->notifier_running)
{
int pqsock = PQsocket(connid->conn);
if (pqsock >= 0)
{
#if TCL_MAJOR_VERSION >= 8
Tcl_CreateChannelHandler(connid->notifier_channel,
TCL_READABLE,
Pg_Notify_FileHandler,
(ClientData) connid);
#else
/* In Tcl 7.5 and 7.6, we need to gin up a Tcl_File. */
Tcl_File tclfile = Tcl_GetFile((ClientData) pqsock, TCL_UNIX_FD);
Tcl_CreateFileHandler(tclfile, TCL_READABLE,
Pg_Notify_FileHandler, (ClientData) connid);
connid->notifier_socket = pqsock;
#endif
connid->notifier_running = 1;
}
}
}
示例10: PgNotifyTransferEvents
void
PgNotifyTransferEvents(Pg_ConnectionId * connid)
{
PGnotify *notify;
while ((notify = PQnotifies(connid->conn)) != NULL)
{
NotifyEvent *event = (NotifyEvent *) ckalloc(sizeof(NotifyEvent));
event->header.proc = Pg_Notify_EventProc;
event->notify = notify;
event->connid = connid;
Tcl_QueueEvent((Tcl_Event *) event, TCL_QUEUE_TAIL);
}
/*
* This is also a good place to check for unexpected closure of the
* connection (ie, backend crash), in which case we must shut down the
* notify event source to keep Tcl from trying to select() on the now-
* closed socket descriptor. But don't kill on-connection-loss
* events; in fact, register one.
*/
if (PQsocket(connid->conn) < 0)
PgConnLossTransferEvents(connid);
}
示例11: init_slot
static void
init_slot(ParallelSlot *slot, PGconn *conn)
{
slot->connection = conn;
slot->isFree = true;
slot->sock = PQsocket(conn);
}
示例12: initDatabase
struct connection_struct* initDatabase(struct event_base* base) {
struct connection_struct* database = malloc(sizeof(struct connection_struct));
database->query_count = 0;
database->report_errors = 0;
database->queries = NULL;
database->last_query = NULL;
database->conn = PQconnectdb(db_connect);
if (PQstatus(database->conn) != CONNECTION_OK) {
fprintf(stderr, "%s\n", PQerrorMessage(database->conn));
PQfinish(database->conn);
exit(1);
} else
PQsetnonblocking(database->conn, 1);
struct event* event = event_new(base, PQsocket(database->conn), EV_READ|EV_PERSIST, pq_event, database);
event_add(event, NULL);
if (all_databases == NULL) {
all_databases = malloc(sizeof(struct database_list));
memset(all_databases, 0, sizeof(struct database_list));
all_databases->db = database;
} else {
struct database_list* node = all_databases;
while (node->next)
node = node->next;
node->next = malloc(sizeof(struct database_list));
memset(node->next, 0, sizeof(struct database_list));
node->next->db = database;
}
return database;
};
示例13: wait_for_flush
static void
wait_for_flush(PlxFn *plx_fn, PGconn *pq_conn)
{
struct epoll_event listenev;
struct epoll_event event;
int res;
res = PQflush(pq_conn);
if (!res)
return;
if (res == -1)
plx_error(plx_fn, "PQflush error %s", PQerrorMessage(pq_conn));
listenev.events = EPOLLOUT;
listenev.data.fd = PQsocket(pq_conn);
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listenev.data.fd, &listenev) < 0)
plx_error(plx_fn, "epoll: socket adding failed");
while (res)
{
CHECK_FOR_INTERRUPTS();
epoll_wait(epoll_fd, &event, 1, 1);
res = PQflush(pq_conn);
if (res == -1)
{
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, listenev.data.fd, &listenev);
plx_error(plx_fn, "%s", PQerrorMessage(pq_conn));
}
}
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, listenev.data.fd, &listenev);
}
示例14: PgSetConnectionId
/*
* Create and register a new channel for the connection
*/
void
PgSetConnectionId(Tcl_Interp *interp, PGconn *conn)
{
Tcl_Channel conn_chan;
Pg_ConnectionId *connid;
int i;
connid = (Pg_ConnectionId *) ckalloc(sizeof(Pg_ConnectionId));
connid->conn = conn;
connid->res_count = 0;
connid->res_last = -1;
connid->res_max = RES_START;
connid->res_hardmax = RES_HARD_MAX;
connid->res_copy = -1;
connid->res_copyStatus = RES_COPY_NONE;
connid->results = (PGresult **) ckalloc(sizeof(PGresult *) * RES_START);
for (i = 0; i < RES_START; i++)
connid->results[i] = NULL;
connid->notify_list = NULL;
connid->notifier_running = 0;
sprintf(connid->id, "pgsql%d", PQsocket(conn));
#if TCL_MAJOR_VERSION >= 8
connid->notifier_channel = Tcl_MakeTcpClientChannel((ClientData) PQsocket(conn));
Tcl_RegisterChannel(NULL, connid->notifier_channel);
#else
connid->notifier_socket = -1;
#endif
#if TCL_MAJOR_VERSION == 7 && TCL_MINOR_VERSION == 5
/* Original signature (only seen in Tcl 7.5) */
conn_chan = Tcl_CreateChannel(&Pg_ConnType, connid->id, NULL, NULL, (ClientData) connid);
#else
/* Tcl 7.6 and later use this */
conn_chan = Tcl_CreateChannel(&Pg_ConnType, connid->id, (ClientData) connid,
TCL_READABLE | TCL_WRITABLE);
#endif
Tcl_SetChannelOption(interp, conn_chan, "-buffering", "line");
Tcl_SetResult(interp, connid->id, TCL_VOLATILE);
Tcl_RegisterChannel(interp, conn_chan);
}
示例15: psyco_conn_fileno
static PyObject *
psyco_conn_fileno(connectionObject *self)
{
long int socket;
EXC_IF_CONN_CLOSED(self);
socket = (long int)PQsocket(self->pgconn);
return PyInt_FromLong(socket);
}