MCPcopy Create free account
hub / github.com/apple/coremltools / AdapterSocket

Class AdapterSocket

coremltools/modelrunner/ModelRunner/Socket.swift:48–167  ·  view source on GitHub ↗

An actor that adapts generic Request and Response types to a Socket interface. `AdapterSocket` provides a type-safe wrapper around socket operations, allowing for strongly-typed request and response handling while conforming to the `Socket` protocol.

Source from the content-addressed store, hash-verified

46/// `AdapterSocket` provides a type-safe wrapper around socket operations,
47/// allowing for strongly-typed request and response handling while conforming to the `Socket` protocol.
48actor AdapterSocket<Request, Response>: Socket where Request: Sendable, Response: Sendable {
49 /// Dictionary to store active continuations
50 private var continuations: [Int : AsyncStream<Request>.Continuation] = [:]
51 /// ID for the next continuation
52 private var nextContinuationID: Int = 0
53 /// The underlying connection used for sending and receiving payloads.
54 private let underlyingSocket: any Socket
55 /// A closure responsible for sending responses over the underlying connection.
56 private let sendResponse: (Response) async throws -> Void
57 /// A task responsible for processing incoming requests from the underlying connection. and informing listeners.
58 private var task: Task<Void, Error>? = nil
59 /// The response stream.
60 nonisolated let responseStream: AsyncStream<Response>
61 /// The response continuation.
62 private let responseContinuation: AsyncStream<Response>.Continuation
63
64 /// Initializes a new AdapterSocket with an underlying socket and conversion functions.
65 ///
66 /// This initializer sets up a AdapterSocket that wraps another Socket,
67 /// providing conversion between the underlying socket's request/response types and
68 /// the AdapterSocket's request/response types.
69 ///
70 /// - Parameters:
71 /// - underlyingConnection: The connection to be wrapped. It must conform to the `Connection` protocol.
72 /// - from: A closure that converts from the underlying connection's request type to this connection's request type.
73 /// This closure may throw an error if the conversion fails.
74 /// - to: A closure that converts from this connection's response type to the underlying connection's response type.
75 /// This closure may throw an error if the conversion fails.
76 init<T: Socket>(socket: T,
77 receive: @escaping (T.Request) -> Request?,
78 send: @escaping (Response) -> T.Response) async throws {
79 (responseStream, responseContinuation) = AsyncStream.makeStream(of: Response.self)
80 underlyingSocket = socket
81 sendResponse = { [socket] (response: Response) async throws -> Void in
82 try await socket.send(value: send(response))
83 }
84
85 let sequence = try await socket.receive().compactMap { receive($0) }
86 task = Task {[weak self] in
87 for try await request in sequence {
88 if Task.isCancelled {
89 return
90 }
91
92 guard let self else {
93 continue
94 }
95
96 await self.inform(request: request)
97 }
98 }
99 }
100
101 deinit {
102 task?.cancel()
103 }
104
105 /// Informs listeners.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected