aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorFritz Elfert <felfert@to.com>2002-03-05 17:58:11 +0000
committerFritz Elfert <felfert@to.com>2002-03-05 17:58:11 +0000
commitcb2577b29fe7b93e9b168ded7f35da748fdeaf1d (patch)
treed7cf962ead89069f885f8da7137feb94acb3dfec /lib
parent8f9ae0a93ba3ea860a28933c2a411eae9365c859 (diff)
downloadplptools-cb2577b29fe7b93e9b168ded7f35da748fdeaf1d.tar.gz
plptools-cb2577b29fe7b93e9b168ded7f35da748fdeaf1d.tar.bz2
plptools-cb2577b29fe7b93e9b168ded7f35da748fdeaf1d.zip
- Re-Implemented lower levels of ncpd (packet and link).
ncpd is now multithreaded. Results in much better performance and less CPU usage.
Diffstat (limited to 'lib')
-rw-r--r--lib/bufferstore.cc47
-rw-r--r--lib/bufferstore.h18
-rw-r--r--lib/ppsocket.cc18
-rw-r--r--lib/ppsocket.h2
4 files changed, 63 insertions, 22 deletions
diff --git a/lib/bufferstore.cc b/lib/bufferstore.cc
index 183ae6c..811c690 100644
--- a/lib/bufferstore.cc
+++ b/lib/bufferstore.cc
@@ -22,36 +22,45 @@
*
*/
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
#include <stream.h>
// Should be iostream.h, but won't build on Sun WorkShop C++ 5.0
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
+#include <assert.h>
#include "bufferstore.h"
-bufferStore::bufferStore() {
- lenAllocd = 0;
- buff = 0L;
- len = 0;
- start = 0;
+bufferStore::bufferStore()
+ : len(0)
+ , lenAllocd(0)
+ , start(0)
+ , buff(0)
+{
}
-bufferStore::bufferStore(const bufferStore &a) {
+bufferStore::bufferStore(const bufferStore &a)
+ : start(0)
+{
lenAllocd = (a.getLen() > MIN_LEN) ? a.getLen() : MIN_LEN;
buff = (unsigned char *)malloc(lenAllocd);
+ assert(buff);
len = a.getLen();
memcpy(buff, a.getString(0), len);
- start = 0;
}
-bufferStore::bufferStore(const unsigned char *_buff, long _len) {
+bufferStore::bufferStore(const unsigned char *_buff, long _len)
+ : start(0)
+{
lenAllocd = (_len > MIN_LEN) ? _len : MIN_LEN;
buff = (unsigned char *)malloc(lenAllocd);
+ assert(buff);
len = _len;
memcpy(buff, _buff, len);
- start = 0;
}
bufferStore &bufferStore::operator =(const bufferStore &a) {
@@ -77,8 +86,8 @@ void bufferStore::init(const unsigned char *_buff, long _len) {
}
bufferStore::~bufferStore() {
- if (buff != 0L)
- free(buff);
+ if (buff)
+ ::free(buff);
}
unsigned long bufferStore::getLen() const {
@@ -133,7 +142,9 @@ void bufferStore::checkAllocd(long newLen) {
do {
lenAllocd = (lenAllocd < MIN_LEN) ? MIN_LEN : (lenAllocd * 2);
} while (newLen >= lenAllocd);
+ assert(lenAllocd);
buff = (unsigned char *)realloc(buff, lenAllocd);
+ assert(buff);
}
}
@@ -190,6 +201,20 @@ void bufferStore::truncate(long newLen) {
len = newLen;
}
+void bufferStore::prependByte(unsigned char cc) {
+ checkAllocd(len + 1);
+ memmove(&buff[1], buff, len++);
+ buff[0] = cc;
+}
+
+void bufferStore::prependWord(int a) {
+ checkAllocd(len + 2);
+ memmove(&buff[2], buff, len);
+ len += 2;
+ buff[0] = a & 0xff;
+ buff[1] = (a>>8) & 0xff;
+}
+
/*
* Local variables:
* c-basic-offset: 4
diff --git a/lib/bufferstore.h b/lib/bufferstore.h
index 3a69159..5a1d8bf 100644
--- a/lib/bufferstore.h
+++ b/lib/bufferstore.h
@@ -216,7 +216,7 @@ public:
* whole content of @p b is appended.
*/
void addBuff(const bufferStore &b, long maxLen = -1);
-
+
/**
* Truncates the buffer.
* If the buffer is smaller, does nothing.
@@ -224,7 +224,21 @@ public:
* @param newLen The new length of the buffer.
*/
void truncate(long newLen);
-
+
+ /**
+ * Prepends a byte to the content of this instance.
+ *
+ * @param c The byte to append.
+ */
+ void prependByte(unsigned char c);
+
+ /**
+ * Prepends a word to the content of this instance.
+ *
+ * @param w The word to append.
+ */
+ void prependWord(int);
+
private:
void checkAllocd(long newLen);
diff --git a/lib/ppsocket.cc b/lib/ppsocket.cc
index a5982b3..8f52988 100644
--- a/lib/ppsocket.cc
+++ b/lib/ppsocket.cc
@@ -83,8 +83,11 @@ ppsocket::~ppsocket()
void ppsocket::
setWatch(IOWatch *watch) {
- if (watch)
+ if (watch) {
+ if (myWatch && (m_Socket != INVALID_SOCKET))
+ myWatch->remIO(m_Socket);
myWatch = watch;
+ }
}
bool ppsocket::
@@ -192,14 +195,11 @@ listen(const char * const Host, int Port)
m_LastError = errno;
return (false);
}
- // Our accept member function relies on non-blocking accepts,
- // so set the flag here (rather than every time around the loop)
- fcntl(m_Socket, F_SETFL, O_NONBLOCK);
return (true);
}
ppsocket *ppsocket::
-accept(string *Peer)
+accept(string *Peer, IOWatch *iow)
{
#ifdef sun
int len;
@@ -214,6 +214,8 @@ accept(string *Peer)
//*****************************************************
accepted = new ppsocket;
+ if (!iow)
+ iow = myWatch;
if (!accepted) {
m_LastError = errno;
return NULL;
@@ -251,9 +253,9 @@ accept(string *Peer)
if (peer)
*Peer = peer;
}
- if (accepted && myWatch) {
- accepted->setWatch(myWatch);
- myWatch->addIO(accepted->m_Socket);
+ if (accepted && iow) {
+ accepted->setWatch(iow);
+ iow->addIO(accepted->m_Socket);
}
return accepted;
}
diff --git a/lib/ppsocket.h b/lib/ppsocket.h
index 05593d3..bde8f97 100644
--- a/lib/ppsocket.h
+++ b/lib/ppsocket.h
@@ -101,7 +101,7 @@ public:
* @returns A pointer to a new instance for the accepted connection or NULL
* if an error happened.
*/
- ppsocket *accept(string *Peer);
+ ppsocket *accept(string *Peer, IOWatch *);
/**
* Check and optionally wait for incoming data.