// comm_client.cpp #include "comm_client.h" CommClient::CommClient() { fd = -1; msg_size = 0; } CommClient::~CommClient() { fd = -1; msg_size = 0; inbuf = ""; from = ""; mudname = ""; to = ""; message = ""; } BOOL CommClient::read_comm() { CHAR buffer[kMaxSocketInputBufferLength] = {0}; INT bytes_read = 0, total_read = 0, i = 0; BOOL ret = false; STRING msize; bytes_read = read(fd, buffer, kMaxSocketInputBufferLength); // keep reading from socket until we don't read the maximum allowed while(bytes_read == kMaxSocketInputBufferLength) { inbuf += buffer; total_read += bytes_read; bytes_read = read(fd, buffer, kMaxSocketInputBufferLength); } if(bytes_read > 0) { inbuf += buffer; total_read += bytes_read; } if(total_read > 0) { ret = true; if(msg_size == 0) { // we haven't parsed this out yet, try it i = inbuf.find(':'); if(i > 0 && i < inbuf.length()) { msize = inbuf.substr(0, i - 1); // pull out everything up to the first ':' msg_size = atoi(msize.c_str()); if(msg_size > 0) inbuf = inbuf.substr(i + 1); // take off what we just evaluated } } } return ret; } BOOL CommClient::evaluate() { BOOL ret = false; STRING mode; std::vector tag; INT i = 0; if(msg_size > 0) { if(inbuf.length() == msg_size) { mode = inbuf.substr(0, 3); if(mode == "msg") { inbuf = inbuf.substr(4); // take off the 'msg:' i = inbuf.find(':'); while(i < inbuf.length()) { tag.push_back(i); i = inbuf.find(':', i+1); } if(tag.size() >= 3) { to = inbuf.substr(0, tag[0] - 1); from = inbuf.substr(tag[0] + 1, tag[1] - tag[0]); mudname = inbuf.substr(tag[1] + 1, tag[2] - tag[1]); message = inbuf.substr(tag[2] + 1); ret = true; } } else if(mode == "who") { // TODO: place WHO code here } } } return ret; } const STRING CommClient::get_message() { std::stringstream s; s << from << "@" << mudname << ": " << message; return s.str(); }