MCPcopy
hub / github.com/coyove/goflyway / Write

Method Write

proxy/udp.go:227–308  ·  view source on GitHub ↗
(b []byte)

Source from the content-addressed store, hash-verified

225}
226
227func (c *udpBridgeConn) Write(b []byte) (n int, err error) {
228 // For simplicity, when return "n", it should always equal to the total length of "b" if writing succeeded
229 // No ErrShortWrite shall happen
230 defer func() {
231 if err == nil {
232 n = len(b)
233 c.logger.D("UDP", "Write bytes", n)
234 } else {
235 c.logger.D("UDP", "Write error", err)
236 }
237 }()
238
239 if b == nil || len(b) == 0 {
240 return
241 }
242
243 var ln int
244 var buf []byte
245
246 if c.incompleteLen {
247 c.incompleteLen = false
248
249 ln = int(binary.BigEndian.Uint16([]byte{c.waitingMore.buf[0], b[0]}))
250 buf = b[1:]
251
252 goto TEST
253 }
254
255 if c.waitingMore.remain > 0 {
256 remain := c.waitingMore.remain
257 c.waitingMore.remain -= len(b)
258
259 if c.waitingMore.remain == 0 {
260 // Best case
261 return c.write(append(c.waitingMore.buf, b...))
262 }
263
264 if c.waitingMore.remain > 0 {
265 // We still don't have enough data to write
266 c.waitingMore.buf = append(c.waitingMore.buf, b...)
267 return len(b), nil
268 }
269
270 // b contains more than what we need
271 if n, err = c.write(append(c.waitingMore.buf, b[:remain]...)); err != nil {
272 return
273 }
274
275 b = b[remain:]
276 // Let's deal with the trailing bytes
277 }
278
279 if len(b) == 1 {
280 c.logger.D("UDP", "Incomplete header")
281 c.waitingMore.buf = b
282 c.incompleteLen = true // "len" should have 2 bytes, we got 1
283 return 1, nil
284 }

Callers 12

servePACFileMethod · 0.45
wsWriteFunction · 0.45
CopyMethod · 0.45
manInTheMiddleMethod · 0.45
writeMethod · 0.45
handleUDPtoTCPMethod · 0.45
sendTrafficStatsFunction · 0.45
dialUpstreamMethod · 0.45
dialUpstreamAndBridgeMethod · 0.45
dialHostAndBridgeMethod · 0.45
handleSocksMethod · 0.45

Calls 1

writeMethod · 0.95

Tested by

no test coverage detected