|
@@ -1,62 +1,41 @@
|
|
|
-#include <iostream>
|
|
|
-#include <stdexcept>
|
|
|
-#include <iostream>
|
|
|
#include <cstring>
|
|
|
-
|
|
|
-#include <sys/socket.h>
|
|
|
+#include <netinet/in.h>
|
|
|
|
|
|
#include "common/stream/Stream.h"
|
|
|
|
|
|
-Stream::Stream(size_t minSize) : dataLength(getSize(minSize)),
|
|
|
- data(new unsigned char[dataLength]), writeIndex(0), readIndex(0)
|
|
|
+Stream::Stream() : error(false), writeIndex(0), readIndex(0)
|
|
|
{
|
|
|
}
|
|
|
|
|
|
-Stream::~Stream()
|
|
|
+Stream::Stream(size_t capacity) : data(capacity), error(false), writeIndex(0), readIndex(0)
|
|
|
{
|
|
|
- delete[] data;
|
|
|
}
|
|
|
|
|
|
-size_t Stream::getSize(size_t minSize) const
|
|
|
+bool Stream::hasData() const
|
|
|
{
|
|
|
- size_t size = 1;
|
|
|
- while(size < minSize && size <= MAX_SIZE)
|
|
|
- {
|
|
|
- size <<= 1;
|
|
|
- }
|
|
|
- return size;
|
|
|
+ return readIndex < writeIndex;
|
|
|
}
|
|
|
|
|
|
-bool Stream::hasData() const
|
|
|
+bool Stream::hasError() const
|
|
|
{
|
|
|
- return readIndex < writeIndex;
|
|
|
+ return error;
|
|
|
}
|
|
|
|
|
|
-bool Stream::resize(size_t minSize)
|
|
|
+void Stream::clearError()
|
|
|
{
|
|
|
- if(minSize > MAX_SIZE)
|
|
|
- {
|
|
|
- return true;
|
|
|
- }
|
|
|
- size_t newSize = getSize(minSize);
|
|
|
-
|
|
|
- unsigned char* newData = new unsigned char[newSize];
|
|
|
- memcpy(newData, data, writeIndex);
|
|
|
- delete[] data;
|
|
|
- data = newData;
|
|
|
- dataLength = newSize;
|
|
|
- return false;
|
|
|
+ error = false;
|
|
|
}
|
|
|
|
|
|
void Stream::write(const void* writeData, size_t length)
|
|
|
{
|
|
|
- if(writeIndex + length > dataLength && resize(writeIndex + length))
|
|
|
+ if(data.write(writeIndex, writeData, length))
|
|
|
+ {
|
|
|
+ writeIndex += length;
|
|
|
+ }
|
|
|
+ else
|
|
|
{
|
|
|
- std::cout << "write over max stream size" << std::endl;
|
|
|
- return;
|
|
|
+ error = true;
|
|
|
}
|
|
|
- memcpy(data + writeIndex, writeData, length);
|
|
|
- writeIndex += length;
|
|
|
}
|
|
|
|
|
|
void Stream::write(const char* writeData)
|
|
@@ -64,181 +43,65 @@ void Stream::write(const char* writeData)
|
|
|
write(writeData, strlen(writeData));
|
|
|
}
|
|
|
|
|
|
-void Stream::writeChar(char c)
|
|
|
-{
|
|
|
- write(&c, sizeof(char));
|
|
|
-}
|
|
|
-
|
|
|
-void Stream::writeShort(int16_t s)
|
|
|
-{
|
|
|
- write(&s, sizeof(short));
|
|
|
-}
|
|
|
-
|
|
|
-void Stream::writeInt(int32_t i)
|
|
|
-{
|
|
|
- write(&i, sizeof(int));
|
|
|
-}
|
|
|
-
|
|
|
-void Stream::writeLong(int64_t l)
|
|
|
-{
|
|
|
- write(&l, sizeof(long));
|
|
|
-}
|
|
|
-
|
|
|
void Stream::writeUnsignedChar(uint8_t uc)
|
|
|
{
|
|
|
- write(&uc, sizeof(unsigned char));
|
|
|
+ write(&uc, sizeof(uint8_t));
|
|
|
}
|
|
|
|
|
|
void Stream::writeUnsignedShort(uint16_t us)
|
|
|
{
|
|
|
- write(&us, sizeof(unsigned short));
|
|
|
+ us = htons(us);
|
|
|
+ write(&us, sizeof(uint16_t));
|
|
|
}
|
|
|
|
|
|
void Stream::writeUnsignedInt(uint32_t ui)
|
|
|
{
|
|
|
- write(&ui, sizeof(unsigned int));
|
|
|
+ ui = htonl(ui);
|
|
|
+ write(&ui, sizeof(uint32_t));
|
|
|
}
|
|
|
|
|
|
-void Stream::writeUnsignedLong(uint64_t ul)
|
|
|
+void Stream::read(void* buffer, size_t length)
|
|
|
{
|
|
|
- write(&ul, sizeof(unsigned long));
|
|
|
-}
|
|
|
-
|
|
|
-bool Stream::read(void* buffer, size_t length)
|
|
|
-{
|
|
|
- if(readIndex + length > writeIndex)
|
|
|
+ if(readIndex + length <= writeIndex && data.read(readIndex, buffer, length))
|
|
|
{
|
|
|
- std::cout << "read over stream data length" << std::endl;
|
|
|
- return true;
|
|
|
+ readIndex += length;
|
|
|
}
|
|
|
- memcpy(buffer, data + readIndex, length);
|
|
|
- readIndex += length;
|
|
|
- return false;
|
|
|
-}
|
|
|
-
|
|
|
-char Stream::readChar(char error)
|
|
|
-{
|
|
|
- char c;
|
|
|
- if(read(&c, sizeof(char)))
|
|
|
+ else
|
|
|
{
|
|
|
- return error;
|
|
|
+ error = true;
|
|
|
}
|
|
|
- return c;
|
|
|
}
|
|
|
|
|
|
-int16_t Stream::readShort(int16_t error)
|
|
|
-{
|
|
|
- short s;
|
|
|
- if(read(&s, sizeof(short)))
|
|
|
- {
|
|
|
- return error;
|
|
|
- }
|
|
|
- return s;
|
|
|
-}
|
|
|
-
|
|
|
-int32_t Stream::readInt(int32_t error)
|
|
|
-{
|
|
|
- int i;
|
|
|
- if(read(&i, sizeof(int)))
|
|
|
- {
|
|
|
- return error;
|
|
|
- }
|
|
|
- return i;
|
|
|
-}
|
|
|
-
|
|
|
-int64_t Stream::readLong(int64_t error)
|
|
|
-{
|
|
|
- long l;
|
|
|
- if(read(&l, sizeof(long)))
|
|
|
- {
|
|
|
- return error;
|
|
|
- }
|
|
|
- return l;
|
|
|
-}
|
|
|
-
|
|
|
-unsigned char Stream::readUnsignedChar(unsigned char error)
|
|
|
+uint8_t Stream::readUnsignedChar()
|
|
|
{
|
|
|
unsigned char uc;
|
|
|
- if(read(&uc, sizeof(unsigned char)))
|
|
|
- {
|
|
|
- return error;
|
|
|
- }
|
|
|
+ read(&uc, sizeof(unsigned char));
|
|
|
return uc;
|
|
|
}
|
|
|
|
|
|
-uint16_t Stream::readUnsignedShort(uint16_t error)
|
|
|
+uint16_t Stream::readUnsignedShort()
|
|
|
{
|
|
|
- unsigned short us;
|
|
|
- if(read(&us, sizeof(unsigned short)))
|
|
|
- {
|
|
|
- return error;
|
|
|
- }
|
|
|
+ uint16_t us;
|
|
|
+ read(&us, sizeof(uint16_t));
|
|
|
+ us = ntohs(us);
|
|
|
return us;
|
|
|
}
|
|
|
|
|
|
-uint32_t Stream::readUnsignedInt(uint32_t error)
|
|
|
+uint32_t Stream::readUnsignedInt()
|
|
|
{
|
|
|
- unsigned int ui;
|
|
|
- if(read(&ui, sizeof(unsigned int)))
|
|
|
- {
|
|
|
- return error;
|
|
|
- }
|
|
|
+ uint32_t ui;
|
|
|
+ read(&ui, sizeof(uint32_t));
|
|
|
+ ui = ntohl(ui);
|
|
|
return ui;
|
|
|
}
|
|
|
|
|
|
-uint64_t Stream::readUnsignedLong(uint64_t error)
|
|
|
-{
|
|
|
- unsigned long ul;
|
|
|
- if(read(&ul, sizeof(unsigned long)))
|
|
|
- {
|
|
|
- return error;
|
|
|
- }
|
|
|
- return ul;
|
|
|
-}
|
|
|
-
|
|
|
-void Stream::readSocket(int socket)
|
|
|
+bool Stream::readSocket(int socket)
|
|
|
{
|
|
|
- writeIndex = 0;
|
|
|
readIndex = 0;
|
|
|
-
|
|
|
- size_t bufferOffset = 0;
|
|
|
- ssize_t freeSpace = dataLength - writeIndex;
|
|
|
-
|
|
|
- while(true)
|
|
|
- {
|
|
|
- ssize_t readLength = recv(socket, data + bufferOffset, freeSpace, MSG_DONTWAIT);
|
|
|
- if(readLength <= 0)
|
|
|
- {
|
|
|
- break;
|
|
|
- }
|
|
|
- writeIndex += readLength;
|
|
|
- if(readLength < freeSpace)
|
|
|
- {
|
|
|
- break;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- resize(dataLength + 1);
|
|
|
- freeSpace = dataLength - writeIndex;
|
|
|
- bufferOffset += readLength;
|
|
|
- }
|
|
|
- }
|
|
|
+ return data.readSocket(socket, writeIndex);
|
|
|
}
|
|
|
|
|
|
-void Stream::sendToSocket(int socket)
|
|
|
+void Stream::sendToSocket(int socket) const
|
|
|
{
|
|
|
- size_t bufferOffset = 0;
|
|
|
- size_t sendLength = writeIndex;
|
|
|
-
|
|
|
- while(sendLength > 0)
|
|
|
- {
|
|
|
- ssize_t writtenLength = send(socket, data + bufferOffset, sendLength, MSG_NOSIGNAL);
|
|
|
- if(writtenLength == -1)
|
|
|
- {
|
|
|
- perror("Cannot send data");
|
|
|
- return;
|
|
|
- }
|
|
|
- sendLength -= writtenLength;
|
|
|
- bufferOffset += writtenLength;
|
|
|
- }
|
|
|
+ data.sendToSocket(socket, writeIndex);
|
|
|
}
|