MCPcopy Create free account
hub / github.com/Barracuda09/SATPI / findStreamAndClientIDFor

Method findStreamAndClientIDFor

src/StreamManager.cpp:131–204  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

129
130
131SpStream StreamManager::findStreamAndClientIDFor(SocketClient &socketClient, int &clientID) {
132 base::MutexLock lock(_mutex);
133
134 // Here we need to find the correct Stream and StreamClient
135 assert(!_stream.empty());
136 const std::string msg = socketClient.getPercentDecodedMessage();
137 const std::string method = StringConverter::getMethod(msg);
138
139 int streamID = StringConverter::getIntParameter(msg, method, "stream=");
140 const int fe_nr = StringConverter::getIntParameter(msg, method, "fe=");
141 if (streamID == -1 && fe_nr >= 1 && fe_nr <= static_cast<int>(_stream.size())) {
142 streamID = fe_nr - 1;
143 }
144
145 std::string sessionID = StringConverter::getHeaderFieldParameter(msg, "Session:");
146 bool newSession = false;
147 clientID = 0;
148
149 // if no sessionID, then try to find it.
150 if (sessionID.empty()) {
151 if (StringConverter::hasTransportParameters(socketClient.getMessage())) {
152 // Do we need to make a new sessionID (only if there are transport parameters)
153 std::random_device rd;
154 std::mt19937 gen(rd());
155 std::normal_distribution<> dist(0xfffffff, 0xffffff);
156 StringConverter::addFormattedString(sessionID, "%010d", std::lround(dist(gen)) % 0xffffffff);
157 newSession = true;
158 } else {
159 // None of the above.. so it is just an outside session
160 SI_LOG_DEBUG("Found message outside session");
161 return nullptr;
162 }
163 }
164
165 // if no streamID, then we need to find the streamID
166 if (streamID == -1) {
167 if (!sessionID.empty()) {
168 SI_LOG_INFO("Found StreamID x - SessionID: %s", sessionID.c_str());
169 for (SpStream stream : _stream) {
170 if (stream->findClientIDFor(socketClient, newSession, sessionID, method, clientID)) {
171 stream->getStreamClient(clientID).setSessionID(sessionID);
172 return stream;
173 }
174 }
175 } else {
176 SI_LOG_INFO("Found StreamID x - SessionID x - Creating new SessionID: %s", sessionID.c_str());
177 for (SpStream stream : _stream) {
178 if (!stream->streamInUse()) {
179 if (stream->findClientIDFor(socketClient, newSession, sessionID, method, clientID)) {
180 stream->getStreamClient(clientID).setSessionID(sessionID);
181 return stream;
182 }
183 }
184 }
185 }
186 } else {
187 SI_LOG_INFO("Found StreamID %d - SessionID %s", streamID, sessionID.c_str());
188 // Did we find the StreamClient? else try to search in other Streams

Callers 1

Calls 5

getMessageMethod · 0.80
findClientIDForMethod · 0.80
setSessionIDMethod · 0.80
streamInUseMethod · 0.80

Tested by

no test coverage detected