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(); }
private void ProcessACK() { if (state == CONNECTOR_STATE.WAIT_ACK) { state = CONNECTOR_STATE.CONNECTED; OnConnectSuccess(this.socket, remoteEndPoint); this.socket = null; this.remoteEndPoint = null; ResetConnector(); } else { Debug.Log("ACK 대기중 아닌데 ACK 패킷 들어옴."); } }
public void ConnectWait(int port) { if (state != CONNECTOR_STATE.IDLE) { throw new Exception("This Peer Not Idle State"); } state = CONNECTOR_STATE.WAIT_SYN; socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); remoteEndPoint = new IPEndPoint(IPAddress.Any, port); socket.Bind(remoteEndPoint); OnWaitStart?.Invoke(); connectThreadAbortFlag = false; connectThread = new Thread(ThreadMainConnect); connectThread.Start(); }
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 ResetConnector() { try { socket?.Close(); connectThreadAbortFlag = true; connectThread = null; remoteEndPoint = null; socket = null; Array.Clear(this.sendBuffer, 0, sendBuffer.Length); Array.Clear(this.recvBuffer, 0, recvBuffer.Length); state = CONNECTOR_STATE.IDLE; } catch (Exception e) { Debug.Log(e.GetType().Name + " " + e.Message + " " + e.StackTrace); } }
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 패킷 들어옴."); } }