@@ -123,7 +123,7 @@ public function initialize(Configuration $config): Response|Error
123123 }
124124
125125 /**
126- * Send a request to the server.
126+ * Send a request to the server and wait for response .
127127 *
128128 * If a response is immediately available (sync HTTP), returns it.
129129 * Otherwise, suspends the Fiber and waits for the transport to resume it.
@@ -144,16 +144,8 @@ public function request(Request $request, int $timeout, bool $withProgress = fal
144144 $ request = $ request ->withMeta (['progressToken ' => $ progressToken ]);
145145 }
146146
147- $ this ->logger ->debug ('Sending request ' , [
148- 'id ' => $ requestId ,
149- 'method ' => $ request ::getMethod (),
150- ]);
151-
152- $ encoded = json_encode ($ request , \JSON_THROW_ON_ERROR );
153- $ this ->session ->queueOutgoing ($ encoded , ['type ' => 'request ' ]);
154147 $ this ->session ->addPendingRequest ($ requestId , $ timeout );
155-
156- $ this ->flushOutgoing ();
148+ $ this ->sendRequest ($ request );
157149
158150 $ immediate = $ this ->session ->consumeResponse ($ requestId );
159151 if (null !== $ immediate ) {
@@ -171,6 +163,20 @@ public function request(Request $request, int $timeout, bool $withProgress = fal
171163 ]);
172164 }
173165
166+ /**
167+ * Send a request to the server.
168+ */
169+ private function sendRequest (Request $ request ): void
170+ {
171+ $ this ->logger ->debug ('Sending request ' , [
172+ 'id ' => $ request ->getId (),
173+ 'method ' => $ request ::getMethod (),
174+ ]);
175+
176+ $ encoded = json_encode ($ request , \JSON_THROW_ON_ERROR );
177+ $ this ->transport ?->send($ encoded );
178+ }
179+
174180 /**
175181 * Send a notification to the server (fire and forget).
176182 */
@@ -179,8 +185,20 @@ public function sendNotification(Notification $notification): void
179185 $ this ->logger ->debug ('Sending notification ' , ['method ' => $ notification ::getMethod ()]);
180186
181187 $ encoded = json_encode ($ notification , \JSON_THROW_ON_ERROR );
182- $ this ->session ->queueOutgoing ($ encoded , ['type ' => 'notification ' ]);
183- $ this ->flushOutgoing ();
188+ $ this ->transport ?->send($ encoded );
189+ }
190+
191+ /**
192+ * Send a response back to the server (for server-initiated requests).
193+ *
194+ * @param Response<mixed>|Error $response
195+ */
196+ private function sendResponse (Response |Error $ response ): void
197+ {
198+ $ this ->logger ->debug ('Sending response ' , ['id ' => $ response ->getId ()]);
199+
200+ $ encoded = json_encode ($ response , \JSON_THROW_ON_ERROR );
201+ $ this ->transport ?->send($ encoded );
184202 }
185203
186204 /**
@@ -204,9 +222,9 @@ public function processMessage(string $input): void
204222 if ($ message instanceof Response || $ message instanceof Error) {
205223 $ this ->handleResponse ($ message );
206224 } elseif ($ message instanceof Request) {
207- $ this ->handleServerRequest ($ message );
225+ $ this ->handleRequest ($ message );
208226 } elseif ($ message instanceof Notification) {
209- $ this ->handleServerNotification ($ message );
227+ $ this ->handleNotification ($ message );
210228 }
211229 }
212230 }
@@ -224,17 +242,13 @@ private function handleResponse(Response|Error $response): void
224242
225243 $ this ->logger ->debug ('Handling response ' , ['id ' => $ requestId ]);
226244
227- if ($ response instanceof Response) {
228- $ this ->session ->storeResponse ($ requestId , $ response ->jsonSerialize ());
229- } else {
230- $ this ->session ->storeResponse ($ requestId , $ response ->jsonSerialize ());
231- }
245+ $ this ->session ->storeResponse ($ requestId , $ response ->jsonSerialize ());
232246 }
233247
234248 /**
235249 * Handle a request from the server (e.g., sampling request).
236250 */
237- private function handleServerRequest (Request $ request ): void
251+ private function handleRequest (Request $ request ): void
238252 {
239253 $ method = $ request ::getMethod ();
240254
@@ -259,9 +273,7 @@ private function handleServerRequest(Request $request): void
259273 );
260274 }
261275
262- $ encoded = json_encode ($ response , \JSON_THROW_ON_ERROR );
263- $ this ->session ->queueOutgoing ($ encoded , ['type ' => $ response instanceof Response ? 'response ' : 'error ' ]);
264- $ this ->flushOutgoing ();
276+ $ this ->sendResponse ($ response );
265277
266278 return ;
267279 }
@@ -272,15 +284,13 @@ private function handleServerRequest(Request $request): void
272284 $ request ->getId ()
273285 );
274286
275- $ encoded = json_encode ($ error , \JSON_THROW_ON_ERROR );
276- $ this ->session ->queueOutgoing ($ encoded , ['type ' => 'error ' ]);
277- $ this ->flushOutgoing ();
287+ $ this ->sendResponse ($ error );
278288 }
279289
280290 /**
281291 * Handle a notification from the server.
282292 */
283- private function handleServerNotification (Notification $ notification ): void
293+ private function handleNotification (Notification $ notification ): void
284294 {
285295 $ method = $ notification ::getMethod ();
286296
@@ -301,21 +311,6 @@ private function handleServerNotification(Notification $notification): void
301311 }
302312 }
303313
304- /**
305- * Flush any queued outgoing messages.
306- */
307- private function flushOutgoing (): void
308- {
309- if (null === $ this ->transport ) {
310- return ;
311- }
312-
313- $ messages = $ this ->session ->consumeOutgoingMessages ();
314- foreach ($ messages as $ item ) {
315- $ this ->transport ->send ($ item ['message ' ], $ item ['context ' ]);
316- }
317- }
318-
319314 public function getSession (): ClientSessionInterface
320315 {
321316 return $ this ->session ;
0 commit comments