]> code.citadel.org Git - citadel.git/blobdiff - webcit/tcp_sockets.c
* buybuy serv_read
[citadel.git] / webcit / tcp_sockets.c
index 4b0e51c1f38a2303a60c82217c5ba41bb5380f58..b1205c7e8dd2eb4f58878e3888406059b23b7c77 100644 (file)
@@ -1,35 +1,31 @@
 /*
  * $Id$
  */
-/** 
- * \defgroup TcpSockets TCP client socket module for WebCit
- * \ingroup CitadelCommunitacion
- */
-/*@{*/
 
 /*
  * Uncomment this to log all communications with the Citadel server
 #define SERV_TRACE 1
  */
 
+
 #include "webcit.h"
 #include "webserver.h"
 
-/**
- * \brief register the timeout
- * \param signum signalhandler number
+/*
+ *  register the timeout
+ *  signum signalhandler number
  * \return signals
  */
 RETSIGTYPE timeout(int signum)
 {
-       lprintf(1, "Connection timed out.\n");
-       exit(3);
+       lprintf(1, "Connection timed out; unable to reach citserver\n");
+       /* no exit here, since we need to server the connection unreachable thing. exit(3); */
 }
 
 
-/**
- * \brief Connect a unix domain socket
- * \param sockpath where to open a unix domain socket
+/*
+ *  Connect a unix domain socket
+ *  sockpath where to open a unix domain socket
  */
 int uds_connectsock(char *sockpath)
 {
@@ -42,13 +38,15 @@ int uds_connectsock(char *sockpath)
 
        s = socket(AF_UNIX, SOCK_STREAM, 0);
        if (s < 0) {
-               lprintf(1, "Can't create socket: %s\n",
+               lprintf(1, "Can't create socket[%s]: %s\n",
+                       sockpath,
                        strerror(errno));
                return(-1);
        }
 
        if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-               lprintf(1, "Can't connect: %s\n",
+               lprintf(1, "Can't connect [%s]: %s\n",
+                       sockpath,
                        strerror(errno));
                close(s);
                return(-1);
@@ -58,10 +56,10 @@ int uds_connectsock(char *sockpath)
 }
 
 
-/**
- * \brief Connect a TCP/IP socket
- * \param host the host to connect to
- * \param service the service on the host to call
+/*
+ *  Connect a TCP/IP socket
+ *  host the host to connect to
+ *  service the service on the host to call
  */
 int tcp_connectsock(char *host, char *service)
 {
@@ -118,17 +116,17 @@ int tcp_connectsock(char *host, char *service)
 
 
 
-/**
- * \brief Input binary data from socket
- * \param buf the buffer to get the input to
- * \param bytes the maximal number of bytes to read
+/*
+ *  Input binary data from socket
+ *  buf the buffer to get the input to
+ *  bytes the maximal number of bytes to read
  */
-inline void _serv_read(char *buf, int bytes, struct wcsession *WCC)
+inline void _serv_read(char *buf, int bytes, wcsession *WCC)
 {
        int len, rlen;
 
        len = 0;
-       while (len < bytes) {
+       while ((len < bytes) && (WCC->serv_sock != -1)){
                rlen = read(WCC->serv_sock, &buf[len], bytes - len);
                if (rlen < 1) {
                        lprintf(1, "Server connection broken: %s\n",
@@ -145,21 +143,16 @@ inline void _serv_read(char *buf, int bytes, struct wcsession *WCC)
        }
 }
 
-void serv_read(char *buf, int bytes)
-{
-       struct wcsession *WCC = WC;
-       _serv_read(buf, bytes, WCC);
-}
-
-/**
- * \brief input string from pipe
+/*
+ *  input string from pipe
  */
 int serv_getln(char *strbuf, int bufsize)
 {
-       struct wcsession *WCC = WC;
+       wcsession *WCC = WC;
        int ch, len;
        char buf[2];
 
+       WCC->ReadPos = NULL;
        len = 0;
        strbuf[0] = 0;
        do {
@@ -168,7 +161,7 @@ int serv_getln(char *strbuf, int bufsize)
                if ((ch != 13) && (ch != 10)) {
                        strbuf[len++] = ch;
                }
-       } while ((ch != 10) && (ch != 0) && (len < (bufsize-1)));
+       } while ((ch != 10) && (ch != 0) && (len < (bufsize-1)) && (WCC->serv_sock != -1));
        strbuf[len] = 0;
 #ifdef SERV_TRACE
        lprintf(9, "%3d>%s\n", WC->serv_sock, strbuf);
@@ -176,14 +169,102 @@ int serv_getln(char *strbuf, int bufsize)
        return len;
 }
 
+int StrBuf_ServGetln(StrBuf *buf)
+{
+       wcsession *WCC = WC;
+       const char *ErrStr;
+       int rc;
+
+       WCC->ReadPos = NULL;
+       rc = StrBufTCP_read_line(buf, &WCC->serv_sock, 0, &ErrStr);
+       if (rc < 0)
+       {
+               lprintf(1, "Server connection broken: %s\n",
+                       ErrStr);
+               wc_backtrace();
+               WCC->serv_sock = (-1);
+               WCC->connected = 0;
+               WCC->logged_in = 0;
+       }
+       return rc;
+}
+
+int StrBuf_ServGetlnBuffered(StrBuf *buf)
+{
+       wcsession *WCC = WC;
+       const char *ErrStr;
+       int rc;
+
+       rc = StrBufTCP_read_buffered_line_fast(buf, 
+                                              WCC->ReadBuf, 
+                                              &WCC->ReadPos, 
+                                              &WCC->serv_sock, 
+                                              5, 1, 
+                                              &ErrStr);
+       if (rc < 0)
+       {
+               lprintf(1, "Server connection broken: %s\n",
+                       ErrStr);
+               wc_backtrace();
+               WCC->serv_sock = (-1);
+               WCC->connected = 0;
+               WCC->logged_in = 0;
+       }
+       return rc;
+}
 
+int StrBuf_ServGetBLOBBuffered(StrBuf *buf, long BlobSize)
+{
+       wcsession *WCC = WC;
+       const char *Err;
+       int rc;
+       
+       rc = StrBufReadBLOBBuffered(buf, 
+                                   WCC->ReadBuf, 
+                                   &WCC->ReadPos,
+                                   &WCC->serv_sock, 
+                                   1, 
+                                   BlobSize, 
+                                   NNN_TERM,
+                                   &Err);
+       if (rc < 0)
+       {
+               lprintf(1, "Server connection broken: %s\n",
+                       Err);
+               wc_backtrace();
+               WCC->serv_sock = (-1);
+               WCC->connected = 0;
+               WCC->logged_in = 0;
+       }
+       return rc;
+}
 
-/**
- * \brief send binary to server
- * \param buf the buffer to write to citadel server
- * \param nbytes how many bytes to send to citadel server
+int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize)
+{
+       wcsession *WCC = WC;
+       const char *Err;
+       int rc;
+       
+       WCC->ReadPos = NULL;
+       rc = StrBufReadBLOB(buf, &WCC->serv_sock, 1, BlobSize, &Err);
+       if (rc < 0)
+       {
+               lprintf(1, "Server connection broken: %s\n",
+                       Err);
+               wc_backtrace();
+               WCC->serv_sock = (-1);
+               WCC->connected = 0;
+               WCC->logged_in = 0;
+       }
+       return rc;
+}
+
+/*
+ *  send binary to server
+ *  buf the buffer to write to citadel server
+ *  nbytes how many bytes to send to citadel server
  */
-void serv_write(char *buf, int nbytes)
+void serv_write(const char *buf, int nbytes)
 {
        int bytes_written = 0;
        int retval;
@@ -204,31 +285,56 @@ void serv_write(char *buf, int nbytes)
 }
 
 
-/**
- * \brief send line to server
- * \param string the line to send to the citadel server
+/*
+ *  send line to server
+ *  string the line to send to the citadel server
  */
-void serv_puts(char *string)
+void serv_puts(const char *string)
 {
+       wcsession *WCC = WC;
 #ifdef SERV_TRACE
        lprintf(9, "%3d<%s\n", WC->serv_sock, string);
 #endif
+       FlushStrBuf(WCC->ReadBuf);
+       WCC->ReadPos = NULL;
+
        serv_write(string, strlen(string));
        serv_write("\n", 1);
 }
 
+/*
+ *  send line to server
+ *  string the line to send to the citadel server
+ */
+void serv_putbuf(const StrBuf *string)
+{
+       wcsession *WCC = WC;
+#ifdef SERV_TRACE
+       lprintf(9, "%3d<%s\n", WC->serv_sock, ChrPtr(string));
+#endif
+       FlushStrBuf(WCC->ReadBuf);
+       WCC->ReadPos = NULL;
+
+       serv_write(ChrPtr(string), StrLength(string));
+       serv_write("\n", 1);
+}
 
-/**
- * \brief convenience function to send stuff to the server
- * \param format the formatstring
- * \param ... the entities to insert into format 
+
+/*
+ *  convenience function to send stuff to the server
+ *  format the formatstring
+ *  ... the entities to insert into format 
  */
 void serv_printf(const char *format,...)
 {
+       wcsession *WCC = WC;
        va_list arg_ptr;
        char buf[SIZ];
        size_t len;
 
+       FlushStrBuf(WCC->ReadBuf);
+       WCC->ReadPos = NULL;
+
        va_start(arg_ptr, format);
        vsnprintf(buf, sizeof buf, format, arg_ptr);
        va_end(arg_ptr);
@@ -242,5 +348,3 @@ void serv_printf(const char *format,...)
 #endif
 }
 
-
-/*@}*/