]> code.citadel.org Git - citadel.git/commitdiff
Switched to native BSD sockets for communication. wxSocket was too slow.
authorArt Cancro <ajc@citadel.org>
Tue, 23 Feb 1999 04:57:19 +0000 (04:57 +0000)
committerArt Cancro <ajc@citadel.org>
Tue, 23 Feb 1999 04:57:19 +0000 (04:57 +0000)
daphne/Makefile
daphne/citclient.cpp
daphne/citclient.hpp
daphne/tcp_sockets.cpp [new file with mode: 0644]
daphne/tcp_sockets.hpp [new file with mode: 0644]

index b0917d32ce68492ed5b91ddd7ae6d1765a645b31..3274b8f8cbeef42fc5a5ae2579723038a8e7ba97 100644 (file)
@@ -1,8 +1,10 @@
 CFLAGS=`wx-config --cflags`
 LFLAGS=`wx-config --libs`
 
-daphne: main.o citclient.o userlogin.o testwindow.o who.o utils.o
-       c++ main.o citclient.o userlogin.o testwindow.o who.o utils.o \
+daphne: main.o citclient.o userlogin.o testwindow.o who.o \
+       utils.o tcp_sockets.o
+       c++ main.o citclient.o userlogin.o testwindow.o who.o \
+       utils.o tcp_sockets.o \
        $(LFLAGS) -o daphne
 
 main.o: main.cpp
@@ -22,3 +24,6 @@ who.o: who.cpp
 
 utils.o: utils.c
        c++ -c $(CFLAGS) utils.c
+
+tcp_sockets.o: tcp_sockets.cpp tcp_sockets.hpp
+       c++ -c $(CFLAGS) tcp_sockets.cpp
index 318a89ed0311b7596e592305f8d942c6272d7c98..3b586c703286aa73f59197b51c360f07b7a013d0 100644 (file)
@@ -3,39 +3,51 @@
 
 #include <wx/wx.h>
 
+#include "tcp_sockets.hpp"
 #include "citclient.hpp"
 
 
-// methods
+//  
+//              LOW LEVEL OPERATIONS
+//
 
 // Attach to the Citadel server
+// FIX (add check for not allowed to log in)
 int CitClient::attach(const wxString& host, const wxString& port) {
        wxString ServerReady;
 
-       if (sock.IsConnected())
-               sock.Close();
-
-       addr.Hostname(host);
-       addr.Service(port);
-       sock.SetNotify(0);
-       sock.Connect(addr, TRUE);
-       if (sock.IsConnected()) {
+       //if (sock.IsConnected())
+       //      sock.Close();
+       //addr.Hostname(host);
+       //addr.Service(port);
+       //sock.SetNotify(0);
+       //sock.Connect(addr, TRUE);
+       //if (sock.IsConnected()) {
+       //      serv_gets(ServerReady);
+       //      initialize_session();
+       //      return(0);
+       //}
+       //else return(1);
+
+       if (sock.is_connected())
+               sock.detach();
+       if (sock.attach("uncnsrd.mt-kisco.ny.us", "citadel")==0) {
                serv_gets(ServerReady);
-               // FIX ... add check for not allowed to log in
                initialize_session();
                return(0);
        }
        else return(1);
+
 }
 
 
 
 // constructor
 CitClient::CitClient(void) {
-  wxSocketHandler::Master();
-  sock.SetFlags(wxSocketBase::WAITALL);
-  wxSocketHandler::Master().Register(&sock);
-  sock.SetNotify(wxSocketBase::REQ_LOST);
+  //wxSocketHandler::Master();
+  //sock.SetFlags(wxSocketBase::WAITALL);
+  //wxSocketHandler::Master().Register(&sock);
+  //sock.SetNotify(wxSocketBase::REQ_LOST);
 }
 
 
@@ -44,37 +56,43 @@ CitClient::CitClient(void) {
 CitClient::~CitClient(void) {
 
        // Be nice and log out from the server if it's still connected
-       detach();
+       sock.detach();
 }
 
 void CitClient::detach(void) {
        wxString buf;
 
-       if (sock.IsConnected()) {
+       //if (sock.IsConnected()) {
+       if (sock.is_connected()) {
                serv_puts("QUIT");
                serv_gets(buf);
-               sock.Close();
+               //sock.Close();
+               sock.detach();
        }
 }
 
 
 // Is this client connected?  Simply return the IsConnected status of sock.
 bool CitClient::IsConnected(void) {
-       return sock.IsConnected();
+       //return sock.IsConnected();
+       return sock.is_connected();
 }
 
 
 
 // Read a line of text from the server
 void CitClient::serv_gets(wxString& buf) {
-       char charbuf[2];
-
-       buf.Empty();
-       do {
-               while (sock.IsData()==FALSE) ;;
-               sock.Read(charbuf, 1);
-               if (isprint(charbuf[0])) buf.Append(charbuf[0], 1);
-       } while(isprint(charbuf[0]));
+       char charbuf[256];
+       
+       sock.serv_gets(charbuf);
+       buf = charbuf;
+
+       //buf.Empty();
+       //do {
+       //      while (sock.IsData()==FALSE) ;;
+       //      sock.Read(charbuf, 1);
+       //      if (isprint(charbuf[0])) buf.Append(charbuf[0], 1);
+       //} while(isprint(charbuf[0]));
        printf("<%s\n", (const char *)buf);
 }
 
@@ -82,10 +100,17 @@ void CitClient::serv_gets(wxString& buf) {
 // Write a line of text to the server
 void CitClient::serv_puts(wxString buf) {
        printf(">%s\n", (const char *)buf);
-       sock.Write(buf, strlen(buf));
-       sock.Write("\n", 1);
+       //sock.Write(buf, strlen(buf));
+       //sock.Write("\n", 1);
+       sock.serv_puts(buf);
 }
 
+
+//
+//            HIGH LEVEL COMMANDS
+//
+
+
 // Server transaction (returns first digit of server response code)
 int CitClient::serv_trans(
                        wxString& command,
index cf533093fb617f73ea09b27807a8ff6114b0848e..0875c768cd0fe60d721b7e7ea5b7b847f71dff10 100644 (file)
@@ -34,8 +34,9 @@ public:
 private:
        void serv_gets(wxString& buf);
        void serv_puts(wxString buf);
-       wxSocketClient sock;
-       wxIPV4address addr;
+       // wxSocketClient sock = (-1);
+       // wxIPV4address addr;
+       TCPsocket sock;
        void CitClient::initialize_session(void);
 };
 
diff --git a/daphne/tcp_sockets.cpp b/daphne/tcp_sockets.cpp
new file mode 100644 (file)
index 0000000..e91a99f
--- /dev/null
@@ -0,0 +1,199 @@
+/*
+ * tcp_sockets.c
+ * 
+ * TCP socket module for WebCit
+ *
+ * $Id$
+ */
+
+#include <stdlib.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+#include <stdio.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <string.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <wx/wx.h>
+#include "tcp_sockets.hpp"
+
+#ifndef INADDR_NONE
+#define INADDR_NONE 0xffffffff
+#endif
+
+
+TCPsocket::TCPsocket(void) {
+       serv_sock = (-1);
+}
+
+void TCPsocket::timeout(int signum) {
+       fprintf(stderr, "Connection timed out.\n");
+       serv_sock = (-1);
+}
+
+int TCPsocket::connectsock(char *host, char *service, char *protocol)
+{
+       struct hostent *phe;
+       struct servent *pse;
+       struct protoent *ppe;
+       struct sockaddr_in sin;
+       int s,type;
+
+       bzero((char *)&sin,sizeof(sin));
+       sin.sin_family = AF_INET;
+
+       pse=getservbyname(service,protocol);
+       if (pse) {
+               sin.sin_port = pse->s_port;
+               }
+       else if ((sin.sin_port = htons((u_short)atoi(service))) == 0) {
+               fprintf(stderr, "Can't get %s service entry\n", service);
+               return(-1);
+               }
+       
+       phe=gethostbyname(host);
+       if (phe) {
+               bcopy(phe->h_addr,(char *)&sin.sin_addr,phe->h_length);
+               }
+       else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
+               fprintf(stderr, "Can't get %s host entry: %s\n",
+                       host,strerror(errno));
+               return(-1);
+               }
+
+       if ((ppe=getprotobyname(protocol))==0) {
+               fprintf(stderr, "Can't get %s protocol entry: %s\n",
+                       protocol,strerror(errno));
+               return(-1);
+               }
+
+       if (!strcmp(protocol,"udp"))
+               type = SOCK_DGRAM;
+       else
+               type = SOCK_STREAM;
+
+       s = socket(PF_INET,type,ppe->p_proto);
+       if (s<0) {
+               fprintf(stderr, "Can't create socket: %s\n", strerror(errno));
+               return(-1);
+               }
+
+
+       signal(SIGALRM, &timeout);
+       alarm(30);
+
+       if (connect(s,(struct sockaddr *)&sin,sizeof(sin))<0) {
+               fprintf(stderr,"can't connect to %s.%s: %s\n",
+                       host,service,strerror(errno));
+               return(-1);
+               }
+
+       alarm(0);
+       signal(SIGALRM,SIG_IGN);
+
+       serv_sock = s;
+       return(s);
+       }
+
+
+
+
+/*
+ * Input binary data from socket
+ */
+void TCPsocket::serv_read(char *buf, int bytes)
+{
+        int len,rlen;
+
+        len = 0;
+        while(len<bytes) {
+                rlen = read(serv_sock,&buf[len],bytes-len);
+                if (rlen<1) {
+                        fprintf(stderr, "Server connection broken: %s\n",
+                               strerror(errno));
+                        serv_sock = (-1);
+                       return;
+                        }
+                len = len + rlen;
+                }
+        }
+
+
+/*
+ * input string from pipe
+ */
+void TCPsocket::serv_gets(char *strbuf)
+{
+       int ch,len;
+       char buf[2];
+
+       len = 0;
+       strcpy(strbuf,"");
+       do {
+               serv_read(&buf[0], 1);
+               ch = buf[0];
+               strbuf[len++] = ch;
+               } while((ch!=10)&&(ch!=13)&&(ch!=0)&&(len<255));
+       strbuf[len-1] = 0;
+       /* fprintf(stderr, ">%s\n", strbuf); */
+       }
+
+
+
+/*
+ * send binary to server
+ */
+void TCPsocket::serv_write(char *buf, int nbytes)
+{
+        int bytes_written = 0;
+        int retval;
+        while (bytes_written < nbytes) {
+                retval = write(serv_sock, &buf[bytes_written],
+                        nbytes - bytes_written);
+                if (retval < 1) {
+                        fprintf(stderr, "Server connection broken: %s\n",
+                               strerror(errno));
+                        serv_sock = (-1);
+                       return;
+                        }
+                bytes_written = bytes_written + retval;
+                }
+        }
+
+
+/*
+ * send line to server
+ */
+void TCPsocket::serv_puts(char *string)
+{
+       char buf[256];
+
+       sprintf(buf,"%s\n", string);
+       serv_write(buf, strlen(buf));
+       }
+
+
+int TCPsocket::attach(char *host, char *port) {
+       printf("attach() called\n");
+       serv_sock = connectsock(host, port, "tcp");
+       if (serv_sock >= 0) return 0;
+       else return (-1);
+}
+
+void TCPsocket::detach() {
+       close(serv_sock);
+       serv_sock = (-1);
+}
+
+bool TCPsocket::is_connected(void) {
+       printf("is_connected() called, serv_sock is %d\n", serv_sock);
+       if (serv_sock >= 0) return TRUE;
+       else return FALSE;
+       }
diff --git a/daphne/tcp_sockets.hpp b/daphne/tcp_sockets.hpp
new file mode 100644 (file)
index 0000000..cca840e
--- /dev/null
@@ -0,0 +1,16 @@
+class TCPsocket {
+public:
+       TCPsocket::TCPsocket(void);
+       int attach(char *, char *);
+       void detach(void);
+       void serv_read(char *, int);
+       void serv_write(char *, int);
+       void serv_gets(char *);
+       void serv_puts(char *);
+       bool is_connected(void);
+private:
+       int serv_sock = (-1);
+       int connectsock(char *, char *, char *);
+       void timeout(int);
+};
+