bool Socket::Flush()
{
int b=0, w=0;
World* world = World::GetPtr();
if (!_outBuffer.length())
{
//if buffer is empty but we need to end compression.
if(_compression == 2) {
_strm.avail_in = 1;
_strm.next_in = (unsigned char*)"\0";
char ch2[1025];//hopefully enough for the zlib data and a terminating null.
std::string buff2;
do {
_strm.avail_out=1024;
_strm.next_out = (unsigned char*)ch2;
deflate(&_strm, Z_FINISH);
ch2[_strm.avail_out] = '\0';
//_outBuffer = _outBuffer+(char*)ch2;
buff2 = buff2+ch2;
}
while(_strm.avail_out == 0);
if(_control!=-1) {
//std::cout << buff2.c_str() << std::endl;
if(send(_control, buff2.c_str(), buff2.length(), 0)) {
//_outBuffer = "";
return false;
}
else {
//_outBuffer = "";
_compression = 0;
deflateEnd(&_strm);
}
}
else {
//_outBuffer = "";
return false;
}
}
return true;
}
//prepend buffer to prompt
if ((_mobile!=NULL)&&(_con==con_game))
{
if(_mobile && !_mobile->pProtocol->WriteOOB) {
std::string telnet = "";
if(_mobile && (_mobile->GetOption("telnetga") != NULL) && (_mobile->GetOption("telnetga")->_data == Variant(1))) {
telnet = ""+TELNET_IAC+TELNET_GA;
}
else {
telnet = "";
}
_outBuffer+="\r\n"+world->BuildPrompt(_mobile->GetPrompt(), _mobile)+telnet;
}
}
if(_mobile) {
char *temp2 = new char[_outBuffer.length()];
strcpy(temp2, _outBuffer.c_str());
int len = _outBuffer.length();
const char* temp = ProtocolOutput(_mobile, temp2, &len);
delete[] temp2;
_outBuffer = temp;
if(_mobile && _mobile->pProtocol->WriteOOB > 0) {
–_mobile->pProtocol->WriteOOB;
}
} // end protocol output stuff.
std::string buff;
if(_compression == 3) { //need to begin compression and immediately start compressing.
_strm.zalloc = Z_NULL;
_strm.zfree = Z_NULL;
_strm.opaque = Z_NULL;
deflateInit(&_strm, Z_DEFAULT_COMPRESSION);
_compression = 1; //now compress.
}
if(_compression == 0) { //no compression needed.
buff =_outBuffer;
}
else if(_compression == 1 || _compression == 2) { //1 means compress, 2 means compress remaining data then finish.
char ch[4097]; //4096 + 1 terminating NULL
_strm.avail_in = _outBuffer.length();
_strm.next_in = (unsigned char*)(_outBuffer.c_str());
//_strm.avail_out = 4096;
//_strm.next_out = ch;
int flush = (_compression == 2)?Z_FINISH:Z_SYNC_FLUSH;
do {
_strm.avail_out = 4096;
_strm.next_out = (unsigned char*)(ch);
deflate(&_strm, flush);
ch[_strm.avail_out] = '\0';
buff = buff+ch;
}
while(_strm.avail_out == 0);
if(_compression == 2) {
_compression = 0;
deflateEnd(&_strm);
}
}
_outBuffer = "";
while (buff.length() > 0) //send all the data.
{
b = (buff.length() < 4096) ? buff.length() : 4096;
//std::cout << buff.c_str() << std::endl;
// any write failures ?
if (_control!=-1)
{
if ((w = send(_control, buff.c_str(), b, 0)) == -1)
{
return false;
}
}
// move the buffer down
buff.erase(0, w);
}
return true;
}
I was looking to put mccp into my mud, along side KaVir's snippet I just got working. However, all of the snippets I can find are either 1. bundled with a whole bunch of other stuff like the telopt handler similar to KaVir's snippets, or use mccp v1, which I hear is bad, or are simply a patch for some other mud like godwars, with no code files, just the .patch (and I definitely don't want to have to read that :P).
Any help in pointing me to a snippet preferably codebase neutral would be great.
Thanks,
-Michael.