unsigned LogCommandParser::Subscribe(SystemAddress systemAddress, const char *channelName) { unsigned i; unsigned channelIndex=(unsigned)-1; if (channelName) { channelIndex = GetChannelIndexFromName(channelName); if (channelIndex==(unsigned)-1) return channelIndex; } for (i=0; i < remoteUsers.Size(); i++) { if (remoteUsers[i].systemAddress==systemAddress) { if (channelName) remoteUsers[i].channels|=1<<channelIndex; // Set this bit for an existing user else remoteUsers[i].channels=0xFFFF; return channelIndex; } } // Make a new user SystemAddressAndChannel newUser; newUser.systemAddress = systemAddress; if (channelName) newUser.channels=1<<channelIndex; else newUser.channels=0xFFFF; remoteUsers.Insert(newUser); return channelIndex; }
unsigned LogCommandParser::Unsubscribe(SystemAddress systemAddress, const char *channelName) { unsigned i; for (i=0; i < remoteUsers.Size(); i++) { if (remoteUsers[i].systemAddress==systemAddress) { if (channelName==0) { // Unsubscribe from all and delete this user. remoteUsers[i]=remoteUsers[remoteUsers.Size()-1]; remoteUsers.RemoveFromEnd(); return 0; } else { unsigned channelIndex; channelIndex = GetChannelIndexFromName(channelName); if (channelIndex!=(unsigned)-1) { remoteUsers[i].channels&=0xFFFF ^ (1<<channelIndex); // Unset this bit } return channelIndex; } } } return (unsigned)-1; }
unsigned LogCommandParser::Subscribe(PlayerID playerId, const char *channelName) { unsigned i; unsigned channelIndex=(unsigned)-1; if (channelName) { channelIndex = GetChannelIndexFromName(channelName); if (channelIndex==(unsigned)-1) return channelIndex; } for (i=0; i < remoteUsers.Size(); i++) { if (remoteUsers[i].playerId==playerId) { if (channelName) remoteUsers[i].channels|=1<<channelIndex; // Set this bit for an existing user else remoteUsers[i].channels=0xFFFF; return channelIndex; } } // Make a new user PlayerIDAndChannel newUser; newUser.playerId = playerId; if (channelName) newUser.channels=1<<channelIndex; else newUser.channels=0xFFFF; remoteUsers.Insert(newUser); return channelIndex; }
unsigned LogCommandParser::Unsubscribe(PlayerID playerId, const char *channelName) { unsigned i; for (i=0; i < remoteUsers.Size(); i++) { if (remoteUsers[i].playerId==playerId) { if (channelName==0) { // Unsubscribe from all and delete this user. remoteUsers[i]=remoteUsers[remoteUsers.Size()-1]; remoteUsers.Del(); return 0; } else { unsigned channelIndex; channelIndex = GetChannelIndexFromName(channelName); if (channelIndex!=(unsigned)-1) { remoteUsers[i].channels&=0xFFFF ^ (1<<channelIndex); // Unset this bit } return channelIndex; } } } return (unsigned)-1; }
void LogCommandParser::WriteLog(const char *channelName, const char *format, ...) { if (channelName==0 || format==0) return; unsigned channelIndex; channelIndex = GetChannelIndexFromName(channelName); if (channelIndex==(unsigned)-1) { AddChannel(channelName); } char text[REMOTE_MAX_TEXT_INPUT]; va_list ap; va_start(ap, format); _vsnprintf(text, REMOTE_MAX_TEXT_INPUT, format, ap); va_end(ap); text[REMOTE_MAX_TEXT_INPUT-1]=0; // Make sure that text ends in \r\n int textLen; textLen=(int)strlen(text); if (textLen==0) return; if (text[textLen-1]=='\n') { text[textLen-1]=0; } if (textLen < REMOTE_MAX_TEXT_INPUT-4) strcat(text, "\r\n"); else { text[textLen-3]='\r'; text[textLen-2]='\n'; text[textLen-1]=0; } // For each user that subscribes to this channel, send to them. unsigned i; for (i=0; i < remoteUsers.Size(); i++) { if (remoteUsers[i].channels & (1 << channelIndex)) { trans->Send(remoteUsers[i].systemAddress, text); } } }
void LogCommandParser::AddChannel(const char *channelName) { unsigned channelIndex; channelIndex = GetChannelIndexFromName(channelName); // Each channel can only be added once. assert(channelIndex==(unsigned)-1); unsigned i; for (i=0; i < 32; i++) { if (channelNames[i]==0) { // Assuming a persistent static string. channelNames[i]=channelName; return; } } // No more available channels - max 32 with this implementation where I save subscribed channels with bit operations assert(0); }