public void ConnectTo(IPAddress ip, int port) { if (state != CONNECTOR_STATE.IDLE) { throw new Exception("This Peer Not Idle State"); } state = CONNECTOR_STATE.WAIT_SYN_ACK; socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, CONNECTION_TIMEOUT); remoteEndPoint = new IPEndPoint(ip, port); byte[] synPayloadBytes = SYN.ToBytes(); Array.Clear(sendBuffer, 0, sendBuffer.Length); //SYN 패킷 전송 Array.Copy(synPayloadBytes, sendBuffer, synPayloadBytes.Length); socket.SendTo(sendBuffer, remoteEndPoint); Debug.Log("Connect Start" + " " + remoteEndPoint.ToString()); OnConnectStart?.Invoke(); connectThread = new Thread(ThreadMainConnect); connectThread.Start(); }
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)); }
private void ProcessSYN() { if (state == CONNECTOR_STATE.WAIT_SYN) { state = CONNECTOR_STATE.WAIT_ACK; socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, CONNECTION_TIMEOUT); Array.Clear(sendBuffer, 0, sendBuffer.Length); //SYN_ACK 패킷 전송 byte[] synAckPacketBytes = SYN_ACK.ToBytes(); Array.Copy(synAckPacketBytes, sendBuffer, synAckPacketBytes.Length); socket.SendTo(sendBuffer, remoteEndPoint); } else { Debug.Log("SYN 대기 중 아닌데 SYN 패킷 들어옴."); } }
private void ProcessSYN_ACK() { if (state == CONNECTOR_STATE.WAIT_SYN_ACK) { Array.Clear(sendBuffer, 0, sendBuffer.Length); byte[] ackPacketBytes = ACK.ToBytes(); Array.Copy(ackPacketBytes, sendBuffer, ackPacketBytes.Length); socket.SendTo(sendBuffer, remoteEndPoint); state = CONNECTOR_STATE.CONNECTED; //연결완료 this.OnConnectSuccess(this.socket, this.remoteEndPoint); this.socket = null; this.remoteEndPoint = null; ResetConnector(); } else { Debug.Log("SYN_ACK 대기 중 아닌데 SYN_ACK 패킷 들어옴."); } }