private void ThreadMainConnect() { while (true) { if (connectThreadAbortFlag) { connectThreadAbortFlag = false; break; } Array.Clear(recvBuffer, 0, recvBuffer.Length); int byteTransferred = 0; try { byteTransferred = socket.ReceiveFrom(recvBuffer, SocketFlags.None, ref remoteEndPoint); } catch (Exception e) { Debug.Log(e.GetType().Name + " " + e.Message + " " + e.StackTrace); OnConnectFail(); ResetConnector(); continue; } Queue <RUDPPayload> payloads = RUDPPayload.BytesToPayloads(recvBuffer, 0, byteTransferred); if (payloads.Count == 1) { RUDPPayload payload = payloads.Dequeue(); if (payload != null) { switch (payload.Tag) { case RUDPPayload.PAYLOAD_TAG.SYN: { ProcessSYN(); break; } case RUDPPayload.PAYLOAD_TAG.SYN_ACK: { ProcessSYN_ACK(); break; } case RUDPPayload.PAYLOAD_TAG.ACK: { ProcessACK(); break; } } } } } }
private void OnReceive(object sender, SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { Queue <RUDPPayload> recvPayloads = RUDPPayload.BytesToPayloads(e.Buffer, e.Offset, e.BytesTransferred); this.OnReceivePayloads.Invoke(recvPayloads); this.StartReceive(); } else { Debug.Log("패킷 수신에 문제가 생겼습니다. " + e.SocketError.ToString()); } }
public void RUDPPayloadTest() { RUDPPayload payload1 = new RUDPPayload(RUDPPayload.PAYLOAD_TAG.RAW, 20); RUDPPayload payload2 = new RUDPPayload(RUDPPayload.PAYLOAD_TAG.ACK, 21); RUDPPayload payload3 = new RUDPPayload(RUDPPayload.PAYLOAD_TAG.DELIVERY_NOFIFY, 22); Packet test1 = new Packet(Packet.HEADER.RUDP); test1.Push(23); test1.Push("If you see this, you turn into great one."); Packet test2 = new Packet(Packet.HEADER.MESSAGE); test2.Push(34); test2.Push("I will not give up, my life is totally mine"); // 아무 패킷이 안붙을때 붙는 빈 더미 패킷 Packet test3 = new Packet(Packet.HEADER.RUDP, 8); payload1.Packet = test1; payload2.Packet = test2; payload3.Packet = test3; byte[] allbytes = new byte[payload1.Size + payload2.Size + payload3.Size]; Buffer.BlockCopy(payload1.ToBytes(), 0, allbytes, 0, payload1.Size); Buffer.BlockCopy(payload2.ToBytes(), 0, allbytes, payload1.Size, payload2.Size); Buffer.BlockCopy(payload3.ToBytes(), 0, allbytes, payload1.Size + payload2.Size, payload3.Size); Queue <RUDPPayload> queue = RUDPPayload.BytesToPayloads(allbytes, 0, allbytes.Length); RUDPPayload popPayload1 = queue.Dequeue(); RUDPPayload popPayload2 = queue.Dequeue(); RUDPPayload popPayload3 = queue.Dequeue(); Assert.IsTrue(popPayload3.PayloadHeader.SequenceEqual(payload3.PayloadHeader)); Assert.IsTrue(payload3.Packet.Data.Take(payload3.Packet.SizeIncludedFixedArea).SequenceEqual(popPayload3.Packet.Data)); Assert.IsTrue(popPayload2.PayloadHeader.SequenceEqual(payload2.PayloadHeader)); Assert.IsTrue(payload2.Packet.Data.Take(payload2.Packet.SizeIncludedFixedArea).SequenceEqual(popPayload2.Packet.Data)); Assert.IsTrue(popPayload1.PayloadHeader.SequenceEqual(payload1.PayloadHeader)); Assert.IsTrue(payload1.Packet.Data.Take(payload1.Packet.SizeIncludedFixedArea).SequenceEqual(popPayload1.Packet.Data)); }