Browse Source

all possible BCE achieved

master
Loki Verloren 2 months ago
parent
commit
02f4144efc
3 changed files with 21 additions and 13 deletions
  1. 4
    2
      conn.go
  2. 2
    1
      listener.go
  3. 15
    10
      rudp.go

+ 4
- 2
conn.go View File

@@ -108,7 +108,8 @@ func (r *RudpConn) rudpRecv(data []byte) error {
108 108
 			break
109 109
 		}
110 110
 		bts := make([]byte, n)
111
-		copy(bts, data[:n])
111
+		dataRead := data[:n]
112
+		copy(bts, dataRead)
112 113
 		r.recvChan <- bts
113 114
 	}
114 115
 	return nil
@@ -121,7 +122,8 @@ func (r *RudpConn) conectedRecvLoop() {
121 122
 			r.recvErr <- err
122 123
 			return
123 124
 		}
124
-		r.rudp.Input(data[:n])
125
+		dataRead := data[:n]
126
+		r.rudp.Input(dataRead)
125 127
 		if r.rudpRecv(data) != nil {
126 128
 			return
127 129
 		}

+ 2
- 1
listener.go View File

@@ -73,7 +73,8 @@ func (rl *RudpListener) run() {
73 73
 			rl.newRudpConn <- rudpConn
74 74
 		}
75 75
 		bts := make([]byte, n)
76
-		copy(bts, data[:n])
76
+		dataRead := data[:n]
77
+		copy(bts, dataRead)
77 78
 		rudpConn.in <- bts
78 79
 	}
79 80
 }

+ 15
- 10
rudp.go View File

@@ -253,20 +253,20 @@ func (r *Rudp) Input(bts []byte) {
253 253
 		r.lastRecvTick = r.currentTick
254 254
 	}
255 255
 	for sz > 0 {
256
-		len := int(bts[0])
257
-		if len > 127 {
256
+		length := int(bts[0])
257
+		if length > 127 {
258 258
 			if sz <= 1 {
259 259
 				r.corrupt.Store(ERROR_MSG_SIZE)
260 260
 				return
261 261
 			}
262
-			len = (len*256 + int(bts[1])) & 0x7fff
262
+			length = (length*256 + int(bts[1])) & 0x7fff
263 263
 			bts = bts[2:]
264 264
 			sz -= 2
265 265
 		} else {
266 266
 			bts = bts[1:]
267 267
 			sz -= 1
268 268
 		}
269
-		switch len {
269
+		switch length {
270 270
 		case TYPE_PING:
271 271
 			r.checkMissing(false)
272 272
 		case TYPE_EOF:
@@ -281,22 +281,27 @@ func (r *Rudp) Input(bts []byte) {
281 281
 			}
282 282
 			exe := r.addRequest
283 283
 			max := r.sendID
284
-			if len == TYPE_MISSING {
284
+			if length == TYPE_MISSING {
285 285
 				exe = r.addMissing
286 286
 				max = r.recvIDMax
287 287
 			}
288
+			// this eliminates multiple BCs in the exe function invocation
289
+			_ = bts[3]
288 290
 			exe(r.getID(max, bts[0], bts[1]), r.getID(max, bts[2], bts[3]))
289 291
 			bts = bts[4:]
290 292
 			sz -= 4
291 293
 		default:
292
-			len -= TYPE_NORMAL
293
-			if sz < len+2 {
294
+			length -= TYPE_NORMAL
295
+			if sz < length+2 {
294 296
 				r.corrupt.Store(ERROR_MSG_SIZE)
295 297
 				return
296 298
 			}
297
-			r.insertMessage(r.getID(r.recvIDMax, bts[0], bts[1]), bts[2:len+2])
298
-			bts = bts[len+2:]
299
-			sz -= len + 2
299
+			// this prevents most of the bounds checks in the following code and
300
+			// would fail in the next function call anyway if this is outside
301
+			_ = bts[4]
302
+			r.insertMessage(r.getID(r.recvIDMax, bts[0], bts[1]), bts[2:length+2])
303
+			bts = bts[length+2:]
304
+			sz -= length + 2
300 305
 		}
301 306
 	}
302 307
 	r.checkMissing(false)

Loading…
Cancel
Save