Пример #1
0
    // 데이터를 서버로 전송(UDP)
    public int SendUnreliable <T>(IPacket <T> packet)
    {
        int sendSize = 0;

        if (transportUDP != null)
        {
            // 헤더 정보 생성
            PacketHeader     header     = new PacketHeader();
            HeaderSerializer serializer = new HeaderSerializer();

            // FIX THIS : 명시적 형변환 해줌. 소스코드와 다름
            header.packetId = (int)packet.GetPacketId();

            byte[] headerData = null;
            if (serializer.Serialize(header) == true)
            {
                headerData = serializer.GetSerializedData();
            }
            byte[] packetData = packet.GetData();

            byte[] data = new byte[headerData.Length + packetData.Length];

            int headerSize = Marshal.SizeOf(typeof(PacketHeader));
            Buffer.BlockCopy(headerData, 0, data, 0, headerSize);
            Buffer.BlockCopy(packetData, 0, data, headerSize, packetData.Length);

            sendSize = transportUDP.Send(data, data.Length);
        }
        return(sendSize);
    }
Пример #2
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Vector3    targetPos = Vector3.zero;
            Ray        ray       = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 10000f))
            {
                targetPos = hit.point;
            }
            // 좌표를 상대에게 전달.
            //byte[] buffer = V3ToStr(targetPos);
            //byte[] buffer = System.Text.Encoding.UTF8.GetBytes(data);
            // 송신 엔드포인트
            //Debug.Log(targetPos + " " + buffer.Length);

            /*
             * byte[] buffer = new byte[3];
             * buffer[0] = (byte)((int)targetPos.x);
             * buffer[1] = (byte)((int)targetPos.y);
             * buffer[2] = (byte)((int)targetPos.z);
             */
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(targetPos.x.ToString() + ';' + targetPos.y.ToString() + ';' + targetPos.z.ToString());
            socket.Send(buffer, buffer.Length);
            if (myMove != null)
            {
                StopCoroutine(myMove);
            }
            // 위치를 얻었으므로 행동 시작. 프로토타입에서는 '이동' 행동
            myMove = StartCoroutine(Move(targetPos, player));
        }
        if (true)
        {
            // 명시적 버퍼 사이즈
            byte[] buffer   = new byte[1024];
            int    recvSize = socket.Receive(ref buffer, buffer.Length);

            // 아직 '전부' 수신되지 않았음.
            if (recvSize <= 0)
            {
                return;
            }

            string   msg = System.Text.Encoding.UTF8.GetString(buffer);
            string[] pos = msg.Split(';');

            // 수신 정보를 위치로 변환
            //Vector3 targetPos = StrToV3(buffer);
            Vector3 targetPos = new Vector3(float.Parse(pos[0]), float.Parse(pos[1]), float.Parse(pos[2]));
            print(targetPos);
            if (oppMove != null)
            {
                StopCoroutine(oppMove);
            }
            // 위치를 얻었으므로 행동 시작. 프로토타입에서는 '이동' 행동
            oppMove = StartCoroutine(Move(targetPos, opponent));
        }
    }
Пример #3
0
    private void GameAttackUpdate()
    {
        if (Vector3.Distance(player.position, opponent.position) <= 3f)
        {
            if (attackCor == null && !player.GetComponent <Animator>().GetBool("Attack"))
            {
                // 내 캐릭터가 공격모션취함
                attackCor = StartCoroutine(AttackCor(player));
                // 이제 상대 단말에서도 내 캐릭터가 공격모션 취함
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes("Attack;" + 0.ToString() + ';' + 0.ToString() + ';' + 0.ToString());
                transport.Send(buffer, buffer.Length);

                PlayHitAnim(opponent);
            }
        }
        else
        {
            attackCor = null;
        }
    }
Пример #4
0
	//송수신해서 동기합니다.
	public bool UpdateSync()
	{
		if (IsConnected() == false && syncState == SyncState.NotStarted)
		{
			// 접속될 때까지 상대에게 접속 요구를 합니다.
			// TransportUDP.AcceptClient 함수에서 처음으로 패킷을 수신했을 때
			// 접속 플래그가 켜지므로 더미 패킷을 던집니다.
			byte[] request = System.Text.Encoding.UTF8.GetBytes(requestData);
			m_transport.Send(request, request.Length);
			return false;
		}

		// 키버퍼에 현재 프레임의 키 정보를 추가합니다. 
		bool update = EnqueueMouseData();

		// 송신.
		if (update)
		{
			SendInputData();
		}

		// 수신.
		ReceiveInputData();

		// 키 버퍼 선두의 키 입력 정보를 반영합니다.
		if (IsSync() == false)
		{    //동기화된 채라면 아무것도 하지 않습니다.
			DequeueMouseData();
		}

#if EMURATE_INPUT
        EmurateInput(); //디버그 중엔 입력을 위장합니다.
#endif

		return IsSync();
	}