public void AcceptCallback(IAsyncResult ar) { int IdPlayer = 0; try { if (Ciclo == true) { allDone.Set(); Socket listener = (Socket)ar.AsyncState; Socket handler = listener.EndAccept(ar); //Agrego cliente para identificarlo IPEndPoint remoteIpEndPoint = handler.RemoteEndPoint as IPEndPoint; //Busco si ya esta en la lista IdPlayer = AcelNetwork.SchClientList(remoteIpEndPoint.Address.ToString(), ref IpClients); //Si no se encontro agrego if (IdPlayer == -1) { if (GameLogic.CurrentGameState == Game_Logic.Game_States.SEL_PLY || GameLogic.CurrentGameState == Game_Logic.Game_States.MENU_MODE || GameLogic.CurrentGameState == Game_Logic.Game_States.INTRO) { IdPlayer = AcelNetwork.AddClientList(remoteIpEndPoint.Address.ToString(), ref IpClients); if (IdPlayer == -1) { Console.WriteLine("ERROR - Max Client\n"); } else { Console.WriteLine("New Client: " + remoteIpEndPoint.Address); //Call callBack for new player if (CallBackAddPlayer != null) { CallBackAddPlayer(IdPlayer); } } } } else { Console.WriteLine("Old Client: " + IdPlayer.ToString() + " - " + remoteIpEndPoint.Address); } // Create the state object StateObject state = new StateObject(); state.workSocket = handler; handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); } } catch (Exception Ex) { Console.WriteLine(Ex.ToString()); } }
public void ReadCallback(IAsyncResult ar) { String content = String.Empty; int bytesRead; string[] AcelVariables; int Idplayer = 0; // Retrieve the state object and the handler socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; //Ip Client IPEndPoint remoteIpEndPoint = handler.RemoteEndPoint as IPEndPoint; try { // Read data from the client socket. bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { //Transformo los datos a typo string content = Encoding.ASCII.GetString(state.buffer, 0, bytesRead); /************ Decodificacion de datos ********/ //Tomo los Datos antes del LF y CF content = content.Split('\n')[0]; content = content.Replace("*", ""); content = content.Replace(" ", ""); //Separo los Daros de X, Y, Z AcelVariables = content.Split('/'); if (AcelVariables.Length > 2) { #if GEN_DEBUG //Decodifico Console.Clear(); Console.WriteLine("X " + AcelVariables[0]); Console.WriteLine("Y " + AcelVariables[1]); Console.WriteLine("Z " + AcelVariables[2]); Console.WriteLine("Btn Fire " + AcelVariables[3]); Console.WriteLine("Btn Gas " + AcelVariables[4]); Console.WriteLine("Btn brake " + AcelVariables[5]); #endif //Adquiero Id del Jugador Idplayer = AcelNetwork.SchClientList(remoteIpEndPoint.Address.ToString(), ref IpClients); //Axes PlyControl[Idplayer].AcelValue[0] = Convert.ToInt32(AcelVariables[0]); PlyControl[Idplayer].AcelValue[1] = Convert.ToInt32(AcelVariables[1]); PlyControl[Idplayer].AcelValue[2] = Convert.ToInt32(AcelVariables[2]); //Botones if (Convert.ToInt32(AcelVariables[3]) == 10) { PlyControl[Idplayer].Buttons[0] = true; } else { PlyControl[Idplayer].Buttons[0] = false; } if (Convert.ToInt32(AcelVariables[4]) == 10) { PlyControl[Idplayer].Buttons[1] = true; } else { PlyControl[Idplayer].Buttons[1] = false; } if (Convert.ToInt32(AcelVariables[5]) == 10) { PlyControl[Idplayer].Buttons[2] = true; } else { PlyControl[Idplayer].Buttons[2] = false; } } if (Ciclo) { handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); } return; } } catch (Exception Exa) { Console.WriteLine("ReadCallback" + Exa.ToString()); //Elimino cliente de la lista //AcelNetwork.RmClientList(remoteIpEndPoint.Address.ToString(), ref IpClients); if (Ciclo) { handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); } return; } }