00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "LineBufferProtocol.h"
00021
00022 namespace mdw
00023 {
00024
00025 LineBufferProtocol::LineBufferProtocol (StreamServer &server) : _server (server)
00026 {
00027 }
00028
00029
00030 LineBufferProtocol::~LineBufferProtocol()
00031 {
00032 }
00033 void LineBufferProtocol::visitRead (char *buffer, int size, StreamInfo &info)
00034 {
00035 std::string ¤tString = _buffers[info.getStreamId()];
00036 int newStart = 0;
00037 if (currentString.empty())
00038 {
00039
00040 for (int i = 0; i < size; ++i)
00041 {
00042 if (buffer[i] == '\n')
00043 {
00044 if (_currentSizeVisitor != 0 )
00045 _currentSizeVisitor->visitRead(buffer + newStart, i - newStart + 1, 0, i - newStart + 1, info);
00046 if (_currentVisitor != 0 )
00047 _currentVisitor->visitRead(buffer + newStart, i - newStart + 1, info);
00048 newStart = i+1;
00049 }
00050 }
00051 }
00052 for (int i = newStart; i < size; ++i)
00053 {
00054 currentString.push_back(buffer[i]);
00055 if (buffer[i] == '\n')
00056 {
00057 if (_currentSizeVisitor != 0)
00058 _currentSizeVisitor->visitRead((char*)currentString.data(), currentString.size(), 0, currentString.size(), info);
00059 if (_currentVisitor != 0)
00060 _currentVisitor->visitRead((char*)currentString.data(), currentString.size(), info);
00061 currentString.clear();
00062 }
00063 }
00064 }
00065 void LineBufferProtocol::visitConnect (StreamInfo &info)
00066 {
00067 if (_currentVisitor != 0) _currentVisitor->visitConnect(info);
00068 if (_currentSizeVisitor != 0) _currentSizeVisitor->visitConnect(info);
00069 }
00070 void LineBufferProtocol::visitDisconnect (StreamInfo &info)
00071 {
00072 if (_currentVisitor != 0) _currentVisitor->visitDisconnect(info);
00073 if (_currentSizeVisitor != 0) _currentSizeVisitor->visitDisconnect(info);
00074 }
00075
00076 void LineBufferProtocol::accept (StreamSizeServerVisitor &visitor, int timeOutSec, int timeOutMicro)
00077 {
00078 _currentSizeVisitor = &visitor;
00079 _currentVisitor = 0;
00080 _server.accept(*this, timeOutSec, timeOutMicro);
00081 }
00082
00083 void LineBufferProtocol::accept (StreamServerVisitor &visitor, int timeOutSec, int timeOutMicro)
00084 {
00085 _currentVisitor = &visitor;
00086 _currentSizeVisitor = 0;
00087 _server.accept(*this, timeOutSec, timeOutMicro);
00088 }
00089
00090 void LineBufferProtocol::write (const char* buffer, int size, int streamId)
00091 {
00092 _server.write(buffer, size, streamId);
00093 }
00094 Stream &LineBufferProtocol::outStream (int streamId)
00095 {
00096 return _server.outStream(streamId);
00097 }
00098 void LineBufferProtocol::close (int streamId)
00099 {
00100 _server.close(streamId);
00101 }
00102
00103 }