aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ppsocket.cc
diff options
context:
space:
mode:
authorFritz Elfert <felfert@to.com>1999-06-28 23:51:15 +0000
committerFritz Elfert <felfert@to.com>1999-06-28 23:51:15 +0000
commitbd48f19dd8931c871c9b79371ff68cf87d2ccc11 (patch)
tree2b024f2b707fb97fc0796611fb4e7527afcad876 /lib/ppsocket.cc
parent7c9e56cea427fb98d84056482f801c4d5264f40e (diff)
downloadplptools-bd48f19dd8931c871c9b79371ff68cf87d2ccc11.tar.gz
plptools-bd48f19dd8931c871c9b79371ff68cf87d2ccc11.tar.bz2
plptools-bd48f19dd8931c871c9b79371ff68cf87d2ccc11.zip
Optimization for speed.
Diffstat (limited to 'lib/ppsocket.cc')
-rw-r--r--lib/ppsocket.cc112
1 files changed, 51 insertions, 61 deletions
diff --git a/lib/ppsocket.cc b/lib/ppsocket.cc
index c9ef495..1caa027 100644
--- a/lib/ppsocket.cc
+++ b/lib/ppsocket.cc
@@ -283,74 +283,64 @@ bool ppsocket::dataToGet() const {
}
int ppsocket::getBufferStore(bufferStore &a, bool wait) {
- /* Returns a 0 for for no message, 1 for message OK, and -1 for socket problem */
- if (!wait && !dataToGet()) return 0;
- a.init();
- bool hadEscape = false;
- do {
- char data;
- int j = readTimeout(&data, 1, 0);
- if (j == SOCKET_ERROR || j == 0) {
- return -1;
- }
- if (hadEscape) {
- a.addByte(data);
- hadEscape = false;
- }
- else {
- if (data == '\\')
- hadEscape = true;
- else if (data == '\0')
- break;
- else
- a.addByte(data);
- }
- } while (true);
+ /* Returns a 0 for for no message,
+ * 1 for message OK, and -1 for socket problem
+ */
+
+ long l;
+ long count = 0;
+ unsigned char *buff;
+ unsigned char *bp;
+ if (!wait && !dataToGet()) return 0;
+ a.init();
+
+ if (readTimeout((char *)&l, sizeof(l), 0) != sizeof(l))
+ return -1;
+ l = ntohl(l);
+ bp = buff = new unsigned char[l];
+ while (l > 0) {
+ int j = readTimeout((char *)bp, l, 0);
+ if (j == SOCKET_ERROR || j == 0) {
+ delete [] buff;
+ return -1;
+ }
+ count += j;
+ l -= j;
+ bp += j;
+ };
+ a.init(buff, count);
+ delete [] buff;
#ifdef SOCKET_DIAGNOSTICS
- cout << "ppsocket got " << a << endl;
+ cout << "ppsocket got " << a << endl;
#endif
- return (a.getLen() == 0) ? 0 : 1;
+ return (a.getLen() == 0) ? 0 : 1;
}
bool ppsocket::sendBufferStore(const bufferStore &a) {
#ifdef SOCKET_DIAGNOSTICS
- cout << "ppsocket sending " << a << endl;
+ cout << "ppsocket sending " << a << endl;
#endif
- bufferStore s;
- long l = a.getLen();
- for (long i = 0; i<l; i++) {
- unsigned char cc = a.getByte(i);
- if (cc == '\0' || cc == '\\') {
- s.addByte('\\');
- }
- s.addByte(cc);
- }
- s.addByte('\0');
-
- {
- int tosend, sent, retries, i;
-
- tosend = s.getLen();
- sent = retries = 0;
-
- while (tosend > 0)
- {
- i = writeTimeout(s.getString(sent), tosend, 0);
-
- if (i == SOCKET_ERROR || i == 0)
- return( false);
-
- sent += i;
- tosend -= i;
-
- if (++retries > 5)
- {
- m_LastError = 0;
- return(false);
- }
- }
- }
- return true;
+ long l = a.getLen();
+ long hl = htonl(l);
+ long sent = 0;
+ int retries = 0;
+ int i;
+
+ i = writeTimeout((char *)&hl, sizeof(hl), 0);
+ if (i != sizeof(hl))
+ return false;
+ while (l > 0) {
+ i = writeTimeout(a.getString(sent), l, 0);
+ if (i == SOCKET_ERROR || i == 0)
+ return(false);
+ sent += i;
+ l -= i;
+ if (++retries > 5) {
+ m_LastError = 0;
+ return(false);
+ }
+ }
+ return true;
}
int ppsocket::readEx(char* Data, int cTerm, int MaxLen)