/*! *\brief parent (child?): send data to other process * *\param session Contains session information * data Data to send * size Bytes to send * *\return 0 : success * -1 : error */ gint session_send_data(Session *session, const guchar *data, guint size) { gboolean ret; g_return_val_if_fail(session->write_buf == NULL, -1); g_return_val_if_fail(data != NULL, -1); g_return_val_if_fail(size != 0, -1); session->state = SESSION_SEND; session->write_buf = g_malloc(size); session->write_buf_p = session->write_buf; memcpy(session->write_buf, data, size); session->write_buf_len = size; gettimeofday(&session->tv_prev, NULL); ret = session_write_data_cb(session->sock, G_IO_OUT, session); if (ret == TRUE) session->io_tag = sock_add_watch(session->sock, G_IO_OUT, session_write_data_cb, session); else if (session->state == SESSION_ERROR) return -1; return 0; }
static gboolean session_recv_data_idle_cb(gpointer data) { Session *session = SESSION(data); gboolean ret; ret = session_read_data_cb(session->sock, G_IO_IN, session); if (ret == TRUE) session->io_tag = sock_add_watch(session->sock, G_IO_IN, session_read_data_cb, session); return FALSE; }
static gboolean sieve_read_chunk_idle_cb(gpointer data) { Session *session = SESSION(data); gboolean ret; ret = sieve_read_chunk_cb(session->sock, G_IO_IN, session); if (ret == TRUE) session->io_tag = sock_add_watch(session->sock, G_IO_IN, sieve_read_chunk_cb, session); return G_SOURCE_REMOVE; }
gint session_recv_msg(Session *session) { g_return_val_if_fail(session->read_msg_buf->len == 0, -1); session->state = SESSION_RECV; if (session->read_buf_len > 0) g_idle_add(session_recv_msg_idle_cb, session); else session->io_tag = sock_add_watch(session->sock, G_IO_IN, session_read_msg_cb, session); return 0; }
/* Get data of specified length. * If needed elsewhere, this should be put in session.c */ static gint sieve_session_recv_chunk(SieveSession *sieve_session, guint bytes) { Session *session = &sieve_session->session; cm_return_val_if_fail(session->read_msg_buf->len == 0, -1); session->state = SESSION_RECV; sieve_session->octets_remaining = bytes; if (session->read_buf_len > 0) g_idle_add(sieve_read_chunk_idle_cb, session); else session->io_tag = sock_add_watch(session->sock, G_IO_IN, sieve_read_chunk_cb, session); return 0; }
gint session_recv_data(Session *session, guint size, const gchar *terminator) { g_return_val_if_fail(session->read_data_buf->len == 0, -1); session->state = SESSION_RECV; g_free(session->read_data_terminator); session->read_data_terminator = g_strdup(terminator); gettimeofday(&session->tv_prev, NULL); if (session->read_buf_len > 0) g_idle_add(session_recv_data_idle_cb, session); else session->io_tag = sock_add_watch(session->sock, G_IO_IN, session_read_data_cb, session); return 0; }
gint session_send_msg(Session *session, SessionMsgType type, const gchar *msg) { gboolean ret; g_return_val_if_fail(session->write_buf == NULL, -1); g_return_val_if_fail(msg != NULL, -1); g_return_val_if_fail(msg[0] != '\0', -1); session->state = SESSION_SEND; session->write_buf = g_strconcat(msg, "\r\n", NULL); session->write_buf_p = session->write_buf; session->write_buf_len = strlen(msg) + 2; ret = session_write_msg_cb(session->sock, G_IO_OUT, session); if (ret == TRUE) session->io_tag = sock_add_watch(session->sock, G_IO_OUT, session_write_msg_cb, session); else if (session->state == SESSION_ERROR) return -1; return 0; }
static gint session_connect_cb(SockInfo *sock, gpointer data) { Session *session = SESSION(data); session->conn_id = 0; if (!sock) { g_warning("can't connect to server."); session->state = SESSION_ERROR; return -1; } session->sock = sock; #if USE_OPENSSL if (session->ssl_type == SSL_TUNNEL) { sock_set_nonblocking_mode(sock, FALSE); if (!ssl_init_socket(sock)) { g_warning("can't initialize SSL."); session->state = SESSION_ERROR; return -1; } } #endif sock_set_nonblocking_mode(sock, session->nonblocking); debug_print("session (%p): connected\n", session); session->state = SESSION_RECV; session->io_tag = sock_add_watch(session->sock, G_IO_IN, session_read_msg_cb, session); return 0; }