mirror of
https://github.com/debauchee/barrier.git
synced 2026-11-29 07:15:54 +08:00
Checkpoint. Code does not run. Still converting over to new
event loop model. Streams, stream filters, and sockets are converted. Client proxies are almost converted. CServer is in progress. Removed all HTTP code. Haven't converted the necessary win32 arch stuff.
This commit is contained in:
@@ -13,8 +13,7 @@
|
||||
*/
|
||||
|
||||
#include "CProtocolUtil.h"
|
||||
#include "IInputStream.h"
|
||||
#include "IOutputStream.h"
|
||||
#include "IStream.h"
|
||||
#include "CLog.h"
|
||||
#include "stdvector.h"
|
||||
#include <cctype>
|
||||
@@ -25,51 +24,77 @@
|
||||
//
|
||||
|
||||
void
|
||||
CProtocolUtil::writef(IOutputStream* stream, const char* fmt, ...)
|
||||
CProtocolUtil::writef(IStream* stream, const char* fmt, ...)
|
||||
{
|
||||
assert(stream != NULL);
|
||||
assert(fmt != NULL);
|
||||
LOG((CLOG_DEBUG2 "writef(%s)", fmt));
|
||||
|
||||
va_list args;
|
||||
|
||||
// determine total size to write
|
||||
va_start(args, fmt);
|
||||
UInt32 count = getLength(fmt, args);
|
||||
UInt32 size = getLength(fmt, args);
|
||||
va_end(args);
|
||||
|
||||
// done if nothing to write
|
||||
if (count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// fill buffer
|
||||
UInt8* buffer = new UInt8[count];
|
||||
va_start(args, fmt);
|
||||
writef(buffer, fmt, args);
|
||||
vwritef(stream, fmt, size, args);
|
||||
va_end(args);
|
||||
|
||||
// write buffer
|
||||
UInt8* scan = buffer;
|
||||
while (count > 0) {
|
||||
const UInt32 n = stream->write(scan, count);
|
||||
LOG((CLOG_DEBUG2 "wrote %d of %d bytes", n, count));
|
||||
count -= n;
|
||||
scan += n;
|
||||
}
|
||||
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
void
|
||||
CProtocolUtil::readf(IInputStream* stream, const char* fmt, ...)
|
||||
bool
|
||||
CProtocolUtil::readf(IStream* stream, const char* fmt, ...)
|
||||
{
|
||||
assert(stream != NULL);
|
||||
assert(fmt != NULL);
|
||||
LOG((CLOG_DEBUG2 "readf(%s)", fmt));
|
||||
|
||||
bool result;
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
try {
|
||||
vreadf(stream, fmt, args);
|
||||
result = true;
|
||||
}
|
||||
catch (XIO&) {
|
||||
result = false;
|
||||
}
|
||||
va_end(args);
|
||||
return result;
|
||||
}
|
||||
|
||||
void
|
||||
CProtocolUtil::vwritef(IStream* stream,
|
||||
const char* fmt, UInt32 size, va_list args)
|
||||
{
|
||||
assert(stream != NULL);
|
||||
assert(fmt != NULL);
|
||||
|
||||
// done if nothing to write
|
||||
if (size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// fill buffer
|
||||
// FIXME -- can we use alloca?
|
||||
UInt8* buffer = new UInt8[size];
|
||||
writef(buffer, fmt, args);
|
||||
|
||||
try {
|
||||
// write buffer
|
||||
stream->write(buffer, size);
|
||||
LOG((CLOG_DEBUG2 "wrote %d bytes", size));
|
||||
|
||||
delete[] buffer;
|
||||
}
|
||||
catch (XBase&) {
|
||||
delete[] buffer;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
CProtocolUtil::vreadf(IStream* stream, const char* fmt, va_list args)
|
||||
{
|
||||
assert(stream != NULL);
|
||||
assert(fmt != NULL);
|
||||
|
||||
// begin scanning
|
||||
while (*fmt) {
|
||||
@@ -242,8 +267,6 @@ CProtocolUtil::readf(IInputStream* stream, const char* fmt, ...)
|
||||
++fmt;
|
||||
}
|
||||
}
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
UInt32
|
||||
@@ -485,7 +508,7 @@ CProtocolUtil::eatLength(const char** pfmt)
|
||||
}
|
||||
|
||||
void
|
||||
CProtocolUtil::read(IInputStream* stream, void* vbuffer, UInt32 count)
|
||||
CProtocolUtil::read(IStream* stream, void* vbuffer, UInt32 count)
|
||||
{
|
||||
assert(stream != NULL);
|
||||
assert(vbuffer != NULL);
|
||||
@@ -493,7 +516,7 @@ CProtocolUtil::read(IInputStream* stream, void* vbuffer, UInt32 count)
|
||||
UInt8* buffer = reinterpret_cast<UInt8*>(vbuffer);
|
||||
while (count > 0) {
|
||||
// read more
|
||||
UInt32 n = stream->read(buffer, count, -1.0);
|
||||
UInt32 n = stream->read(buffer, count);
|
||||
|
||||
// bail if stream has hungup
|
||||
if (n == 0) {
|
||||
|
||||
Reference in New Issue
Block a user