示例#1
0
	public void sendMessage(Paquete p){
		
		NetworkStream stream = client.GetStream ();
		String s = p.GetDataStream ();
		byte[] message = GetBytes (s);
		stream.Write (message, 0,message.Length);
		Debug.Log("TCP Client: Enviado mensaje!");
		
	}
示例#2
0
	public void sendMessage(Paquete p){
		byte[] data	;
		p.id = this.sendingPackagesCounter;
		sendingPackagesCounter++;
		// Obtenemos los bytes del paquete
		data = GetBytes(p.GetDataStream());		
		// Enviar al servidor
		clientSocket.BeginSendTo(data, 0, data.Length, SocketFlags.None, epServer, new System.AsyncCallback(this.SendData), null);	
	}
示例#3
0
	public void sendMessage(Paquete p){
		byte[] data	;
		p.id = this.sendingPackagesCounter;
		sendingPackagesCounter++;
		// Obtenemos los bytes del paquete
		data = GetBytes(p.GetDataStream());		
		foreach (Cliente client in this.listaClientes)
		{
			// Enviar a todos los clientes
			serverSocket.BeginSendTo(data, 0, data.Length, SocketFlags.None, client.endPoint, new AsyncCallback(this.enviarData), client.endPoint);	
		}
	}
示例#4
0
	public void receiveMessage(){
		Debug.Log ("TCP CLIENT: En metodo lectura");
		NetworkStream stream = client.GetStream ();
		byte[] bb=new byte[1024];
		stream.Read (bb,0,bb.Length);
		string s = GetString (bb);
		Paquete p = new Paquete(s);
		Debug.Log ("TCP Client: El XML recibido" + p.GetDataStream ()); 
		Paquete.Identificador accion = p.identificadorPaquete;
		if (accion == Paquete.Identificador.jugadorListo) {
			GameController.controller.opponentReady = true;
			
		} else if (accion == Paquete.Identificador.moverAbajo) {
			//mover el server abajo
			GameController.controller.ship1.rDown = true;
			
		} else if (accion == Paquete.Identificador.moverArriba) {
			//mover el server arriba
			GameController.controller.ship1.rUp = true;
			
		} else if (accion == Paquete.Identificador.moverIzquierda) {
			//mover el server izquierda
			GameController.controller.ship1.rLeft = true;
			
		} else if (accion == Paquete.Identificador.moverDerecha) {
			//mover el server derecha
			GameController.controller.ship1.rRight = true;
			
		} else if (accion == Paquete.Identificador.disparar) {
			GameController.controller.ship1.fireB = true;
		} else if (accion == Paquete.Identificador.colision) {
			
		} else if (accion == Paquete.Identificador.accesoAutorizado) {
			Debug.Log ("TCP: Ya me autorizaron :)");
			GameController.controller.connected = true;
		} else if (accion == Paquete.Identificador.jugadorGana) {
			if (p.jugador == 1) {
				GameController.controller.p1Wins = true;

			} else if (p.jugador == 2) {
				GameController.controller.p2Wins = true;

			}

		
		} else if (accion == Paquete.Identificador.desconectar) {
			GameController.controller.mm2 = true;
			
		} 

		
	}
示例#5
0
	//Metodo que se llama al finalizar recepcion datos
	private void ReceiveData(IAsyncResult asyncResult)
	{
		try
		{
			//Debug.Log("UDP Server: Recibiendo data : ");
			byte[] data	;
			// Paquete para almacenar la data recibida
			Paquete receivedData = new Paquete(GetString(this.dataStream));
			receivedData.GetDataStream();
			//Verificamos que el paquete venga en el ordern correcto.

			//Debug.Log("UDP Server: Received data num : "+receivedData.id);
			//Debug.Log ("UDP Server: Server Counter: "+this.entrantPackagesCounter);
			if(receivedData.id<this.entrantPackagesCounter){
				//Debug.Log("UDP Server: Paquete Descartado : ");
				return; //Descartamos el paquete

			}
			else if(receivedData.id > this.entrantPackagesCounter){
				this.entrantPackagesCounter = receivedData.id;
			}
			else{
				this.entrantPackagesCounter++;
			}
			// Initialise the IPEndPoint for the clients
			IPEndPoint clients = new IPEndPoint(IPAddress.Any, 0);
			// Initialise the EndPoint for the clients
			EndPoint epSender = (EndPoint)clients;
			// Receive all data
			serverSocket.EndReceiveFrom(asyncResult, ref epSender);


			// Actualizamos el mundo del server...Pendiente
			if(receivedData.identificadorPaquete == Paquete.Identificador.conectar){
				/*// Empezamos a crear el paquete a ser enviado
				Paquete sendData = new Paquete();
				sendData.id = this.sendingPackagesCounter;
				sendData.identificadorPaquete = Paquete.Identificador.accesoAutorizado;
				GameController.controller.connected = true;
				Cliente client2 = new Cliente();
				client2.endPoint = epSender;
				client2.nombre = "Player2";

				// Add client to list
				this.listaClientes.Add(client2);*/

				// Obtenemos los bytes del paquete
				//data = GetBytes(sendData.GetDataStream());
				/*Se envia el paquete a todos los clientes*/
				/*foreach (Cliente client in this.listaClientes)
				{
					//Debug.Log("Enviando a cliente..");
				

						// Enviar a todos los clientes
						serverSocket.BeginSendTo(data, 0, data.Length, SocketFlags.None, client.endPoint, new AsyncCallback(this.enviarData), client.endPoint);
						//Debug.Log("Envio Exitoso.");

				}*/

			}
			else if(receivedData.identificadorPaquete == Paquete.Identificador.jugadorListo){
				//GameController.controller.opponentReady=true;


			}

			else if(receivedData.identificadorPaquete == Paquete.Identificador.nuevaPos){

				//Verificando que la nave del cliente se encuentre en una posicion correcta
				bool correctPosition=false;
				//X ->   ,  Y -> 25
				if(GameController.controller.ship2.posx >= receivedData.x +60.0 || GameController.controller.ship2.posy >= receivedData.y +60.0){
					//Corregimos la posicion
					//Debug.Log ("UDP SERVER: DESFASE DE CLIENTE!! Corrigiendo...");
					
					float newx = GameController.controller.ship2.posx;
					float newy = GameController.controller.ship2.posy;
					
					Paquete p = new Paquete();
					p.identificadorPaquete = Paquete.Identificador.corregirPos;
					p.x = newx;
					p.y = newy;
					sendMessage(p);

				}



			}
				/*

				else{
					//Actualizamos tiempos y transforms para prediccion e interpolacion
					GameController.controller.player2.transform.position = new Vector3(receivedData.x,receivedData.y,0);
					syncVelocity = GameController.controller.player2.rigidbody2D.velocity;
					
					syncTime = 0f;
					syncDelay = Time.time -lastSynchronizationTime;
					lastSynchronizationTime = Time.time;
					
					syncEndPosition = syncPosition+syncVelocity*syncDelay;
					syncStartPosition = new Vector3(receivedData.x,receivedData.y,0);

				}

				}*/






			
			// Volvemos a escuchar conexiones nuevamente...
			serverSocket.BeginReceiveFrom(this.dataStream, 0, this.dataStream.Length, SocketFlags.None, ref epSender, new AsyncCallback(this.ReceiveData), epSender);
			// Actualizamos el estado con un delegate
			//this.Invoke(this.updateStatusDelegate, new object[] { sendData.ChatMessage });
		}
		catch (Exception ex)
		{
			throw ex;
		}
	}
示例#6
0
	public void sendMessage(Paquete p){
		
		
		NetworkStream stream = mClient.GetStream ();
		String s = p.GetDataStream ();
		byte[] message = GetBytes (s);
		stream.Write (message, 0,message.Length);
		
	}