/// <summary>Opens a connection to the Teamspeak Server and obtains all necessary server information.</summary> public void performOpenConnection() { // -- Wait ( 10 seconds ) for data to be initialized. for (int secondsSlept = 0; secondsSlept < 10 && ts3ServerIp == "Teamspeak Ip"; secondsSlept++) Thread.Sleep(1000); // -- Required: Connection, Login, Use, Staging Channel. consoleWrite("[Connection] Establishing a connection to a Teamspeak 3 Server."); mTsResponse = mTsConnection.open(ts3ServerIp, ts3QueryPort); if (!performResponseHandling(Queries.OpenConnectionEstablish)) return; consoleWrite("[Connection] ^2Established a connection to {0}:{1}.", ts3ServerIp, ts3QueryPort); consoleWrite("[Connection] Attempting to login as a Server Query Client."); sendTeamspeakQuery(TeamspeakQuery.buildLoginQuery(ts3QueryUsername, ts3QueryPassword)); if (!performResponseHandling(Queries.OpenConnectionLogin)) return; consoleWrite("[Connection] ^2Logged in as {0}.", ts3QueryUsername); consoleWrite("[Connection] Attempting to select the correct virtual server."); sendTeamspeakQuery(TeamspeakQuery.buildUsePortQuery(ts3ServerPort)); if (!performResponseHandling(Queries.OpenConnectionUse)) return; consoleWrite("[Connection] ^2Selected the virtual server using port {0}.", ts3ServerPort); consoleWrite("[Connection] Attempting to find the staging channel."); sendTeamspeakQuery(TeamspeakQuery.buildChannelFindQuery(ts3StgChannelName)); if (!performResponseHandling(Queries.OpenConnectionStaging)) return; mStagingChannel.setBasicData(mTsResponse.Sections[0].Groups[0]); consoleWrite("[Connection] ^2Found the channel named {0}.", mStagingChannel.tsName); if (ts3EnableDropoff) { consoleWrite("[Connection] Attempting to find the staging channel."); sendTeamspeakQuery(TeamspeakQuery.buildChannelFindQuery(ts3DropoffChannelName)); if (!performResponseHandling(Queries.OpenConnectionStaging)) return; mDropoffChannel.setBasicData(mTsResponse.Sections[0].Groups[0]); consoleWrite("[Connection] ^2Found the channel named {0}.", mDropoffChannel.tsName); } // -- Optional: Change Nickname. consoleWrite("[Connection] Attempting to alter the Server Query Client's name."); sendTeamspeakQuery(TeamspeakQuery.buildChangeNicknameQuery(ts3QueryNickname)); if (!performResponseHandling(Queries.OpenConnectionNickname)) return; if (mTsResponse.Id != "513") consoleWrite("[Connection] ^2Changed the Server Query Client's name to {0}.", ts3QueryNickname); mTsResponse = new TeamspeakResponse("error id=0 msg=ok"); // -- Trivial: Find Existing Channels. consoleWrite("[Connection] Attempting to find existing pickup, team, and squad channels."); sendTeamspeakQuery(TeamspeakQuery.buildChannelListQuery()); List<TeamspeakChannel> tsChannels = new List<TeamspeakChannel>(); foreach (TeamspeakResponseSection tsResponseSection in mTsResponse.Sections) foreach (TeamspeakResponseGroup tsResponseGroup in tsResponseSection.Groups) tsChannels.Add(new TeamspeakChannel(tsResponseGroup)); foreach (TeamspeakChannel tsChannel in tsChannels) foreach (String tsName in ts3PckChannelNames) if (tsChannel.tsName == tsName) { mPickupChannels.Add(tsChannel); consoleWrite("[Connection] ^2Found ^bPickup^n Channel: {0} ({1}).", tsChannel.tsName, tsChannel.tsId); break; } foreach (TeamspeakChannel tsChannel in tsChannels) if (tsChannel.medPId == 0 || tsChannel.medPId == mStagingChannel.tsId) for (int i = 0; i < chnTeamNames.Length; i++) if (!mTeamChannels.ContainsKey(i + 1) && tsChannel.tsName == chnTeamNames[i]) { mTeamChannels.Add(i + 1, tsChannel); mSquadChannels.Add(i + 1, new Dictionary<Int32, TeamspeakChannel>()); consoleWrite("[Connection] ^2Found ^bTeam^n Channel: {0} ({1}).", tsChannel.tsName, tsChannel.tsId); break; } foreach (TeamspeakChannel tsChannel in tsChannels) foreach (Int32 teamId in mTeamChannels.Keys) if (tsChannel.medPId == mTeamChannels[teamId].tsId) for (int i = 0; i < chnSquadNames.Length; i++) if (!mSquadChannels[teamId].ContainsKey(i + 1) && tsChannel.tsName == chnSquadNames[i]) { mSquadChannels[teamId].Add(i + 1, tsChannel); consoleWrite("[Connection] ^2Found ^bSquad^n Channel: {0} ({1}).", tsChannel.tsName, tsChannel.tsId); break; } // -- Done. consoleWrite("[Connection] Teamspeak 3 Sync started."); mEnabled = true; }
/// <summary>Sends a query to the teamspeak server (delayed if necessary) and sets the response.</summary> public void sendTeamspeakQuery(TeamspeakQuery query) { if (synDelayQueries) { TimeSpan delay = TimeSpan.FromMilliseconds(synDelayQueriesAmount); TimeSpan delta = DateTime.Now - mTsPrevSendTime; if (delta <= delay) Thread.Sleep(delay - delta); } mTsResponse = mTsConnection.send(query); mTsPrevSendTime = DateTime.Now; }
/// <summary>Closes a connection to the Teamspeak Server and clears all the data stored within the plugin.</summary> public void performCloseConnection() { consoleWrite("[Closing] Shutting down Teamspeak 3 Sync."); mTsConnection.close(); consoleWrite("[Closing] Cleaning up resources."); mClientAllInfo.Clear(); mClientTsInfo.Clear(); mClientGmInfo.Clear(); mClientPbInfo.Clear(); mTeamChannels.Clear(); mSquadChannels.Clear(); mPickupChannels.Clear(); mTsResponse = new TeamspeakResponse("error id=0 msg=ok"); mCurrentAction = null; mPreviousAction = null; consoleWrite("[Closing] Teamspeak 3 Sync stopped."); mEnabled = false; }