MCPcopy Create free account
hub / github.com/HyBuildNet/quic-relay / backendToClient

Method backendToClient

internal/handler/forwarder.go:129–178  ·  view source on GitHub ↗

backendToClient reads packets from backend and sends to client. Uses buffer pool to avoid per-session 64KB allocations.

(ctx *Context, session *Session)

Source from the content-addressed store, hash-verified

127// backendToClient reads packets from backend and sends to client.
128// Uses buffer pool to avoid per-session 64KB allocations.
129func (h *ForwarderHandler) backendToClient(ctx *Context, session *Session) {
130 for {
131 // Check if session is closed before reading
132 if session.IsClosed() {
133 return
134 }
135
136 // Get buffer from pool for this read
137 buf := GetBuffer()
138
139 // Set read deadline to detect idle connections
140 session.BackendConn.SetReadDeadline(time.Now().Add(5 * time.Minute))
141
142 n, err := session.BackendConn.Read(*buf)
143 if err != nil {
144 // Connection closed or timed out
145 PutBuffer(buf)
146 return
147 }
148
149 // Check again after read (session may have closed during blocking read)
150 if session.IsClosed() {
151 PutBuffer(buf)
152 return
153 }
154
155 // Update activity timestamp (bidirectional tracking)
156 session.Touch()
157
158 // Notify proxy of server packets to learn server's SCID(s)
159 // This enables routing subsequent client packets that use server's CID as DCID
160 ctx.NotifyServerPacket((*buf)[:n])
161
162 debug.Printf(" backend->client: %d bytes, first byte: 0x%02x", n, (*buf)[0])
163
164 // Send to client via proxy's UDP connection
165 if ctx.ProxyConn != nil {
166 _, err = ctx.ProxyConn.WriteToUDP((*buf)[:n], session.ClientAddr())
167 if err != nil {
168 log.Printf("[forwarder] write to client failed: %v", err)
169 PutBuffer(buf)
170 return
171 }
172 debug.Printf(" sent to client %s", session.ClientAddr())
173 }
174
175 // Return buffer to pool after use
176 PutBuffer(buf)
177 }
178}

Callers 1

OnConnectMethod · 0.95

Calls 7

PrintfFunction · 0.92
GetBufferFunction · 0.85
PutBufferFunction · 0.85
IsClosedMethod · 0.80
TouchMethod · 0.80
NotifyServerPacketMethod · 0.80
ClientAddrMethod · 0.80

Tested by

no test coverage detected