public bool SendPacket(Socket sck,Packet pck) { try { //TODO: id mitsenden sck.Send(pck.value); return true; } catch (Exception ex) { PacketReadFailAttempts++; return false; } }
public Packet ReadPacket(Socket sck,int length = 128) { try { Packet p = new Packet(ReadBuffer(sck, length)); return p; } catch (Exception ex) { //CommandLine.Write("Error while ReadPacket(): " + ex); PacketReadFailAttempts++; return new Packet(Packets.ERROR); } }
private void SendPacket(Socket sck, Packet pck) { try { pck.Send(sck); } catch { //TODO:PacketReadAttempts++; } }
public Packet GetPacketFromNewSocket(PacketInfo pckInfo, Socket sck) { byte[] buffer = ReadBuffer(sck, 100); Packet pck = new Packet(pckInfo, buffer); if (pck.byteInfo.ToString() != pckInfo.ToByte().ToString()) { CommandLine.Write("Invalid Packet " + pckInfo.ToString() + " or wrong byteInfo(" + pck.byteInfo + ")", "warning", ConsoleColor.Yellow); } return pck; }
public void Acceptor() { try { while (true) { tcpServer.Start(); //CommandLine.Write("Ready for Client Connection"); TcpClient tcpclient = tcpServer.AcceptTcpClient(); NetworkStream stm = tcpclient.GetStream(); CommandLine.Write("Got Connection from " + tcpclient.Client.RemoteEndPoint); Packet userdata = new Packet(); userdata.Parse(stm); Packet p = new Packet(); p["HEADER"] = "test"; p[4] = "lol"; p[false] = true; p["testmessage"] = "message"; p.Build(stm); CommandLine.Write("[STX]" + (string)userdata[5] + "[ETX]"); CommandLine.Write("[STX]" + (string)userdata[true] + "[ETX]"); if((bool)userdata[true]) { CommandLine.Write("succes"); } if (!CheckIfUserDataIsValid((string)userdata["username"], (string)userdata["password"])) { //mysql abfrage spieler und id bekommen(hier ist es 1) CommandLine.Write((string)userdata["username"] + " failed to login "); } //mysql abfrage spieler und id bekommen(hier ist es 1) else { CommandLine.Write((string)userdata["username"] + " logged in"); int lID = 0; //prüfen welche ID verfügbar ist for (int i = 0; i < PlayerQueue.Length; i++) { if (PlayerQueue[i] == null) { lID = i; break; } else if (i == maxPlayers) { CommandLine.Write("max player reached"); return; } else CommandLine.Write("shit"); } PlayerQueue[lID] = new Player(); //PlayerQueue[lID].PlayerSocket = sck; //die globale id wird aus dem dantenbank index verwendet //TODO:überlegen ob gID key oder wert der Property globalID sein soll PlayerQueue[lID].ID = 2; int gID = 0; for (int i = 0; i < Player.Length; i++) { if (Player[i] == null) { gID = i; break; } else if (i == maxPlayers) { CommandLine.Write("max player reached"); return; } else CommandLine.Write("shit"); } //TODO: der Spieler befindet sich nun auf der login seite und wählt aus das spiel zu starten try { //die globale spieler id als arrayindex verwenden if (PlayerQueue[lID].isConnected) { CommandLine.Write("moving client from LoginServer to GameServer: " + PlayerQueue[lID].PlayerSocket.RemoteEndPoint+"\n Player ID: "+gID); //den spieler aus der warteschlange zu den spielern hinzufügen Player[gID] = PlayerQueue[lID]; Player[gID].MaxReadAttempts = MaxReadAttempts; //den spieler aus der warteschlange löschen DeletePlayerFromQueue(lID); //die methode der plugins wenn ein spieler in das spiel eintritt wird aufgerufen pManager.RunCommand("penis.123"); //thread für den neuen spieler erstellen Player[gID].EventHandler = new Thread(new ThreadStart(delegate { EventHandler(gID); })); Player[gID].RecieverThread = new Thread(new ThreadStart(delegate { Reciever(gID); })); //UND HIER WID DANN EIN NEUER THREAD FÜR DEN SPIELER GESTARTET :DDDDDD Player[gID].EventHandler.Start(); Player[gID].RecieverThread.Start(); } else { CommandLine.Write("client lost connection: " + Player[lID].PlayerSocket.RemoteEndPoint + "\n Player ID: " + lID); //den spieler aus der warteschlange entfernen DeletePlayerFromQueue(lID); } } catch (Exception ex) { //CommandLine.Write("No players connected"); } } } } catch (Exception ex) { CommandLine.Write("Error in Acceptor() maybe there is another Server on this Port " + serverPort + ": " + ex.Message+"\n\r"+ex.StackTrace, "error", ConsoleColor.Red); Console.ResetColor(); } }