public static void Send(Clientplayer x, byte[] buffer, byte cmd) { if (IsAlive(x)) { byte[] sendingbyter; if (buffer != null) { sendingbyter = new byte[buffer.Length + 1]; sendingbyter[0] = cmd; Buffer.BlockCopy(buffer, 0, sendingbyter, 1, buffer.Length); } else { sendingbyter = new byte[] { cmd } }; try { x.ClientSocket.Client.Send(sendingbyter); } catch { Disconnect(x, "Packet Sending Failed!"); } } }
public static Int32 GetRemotePort(Clientplayer x) { int port; Int32.TryParse(GetRemoteEndPointString(x).Split(new char[] { ':' })[1], out port); return(port); }
public static void CodeEvent(Clientplayer x) { Random efiller = new Random(); Array.Clear(x.codebyte, 0, x.codebyte.Length); Array.Clear(x.codebyte0, 0, x.codebyte0.Length); efiller.NextBytes(x.codebyte0); efiller.NextBytes(x.codebyte); }
public static void StopALL() { Broadcast(null, (byte)Enums.PacketID.shuttindown); Log.Write("Broadcasting Shutdown command completed"); lock (Globals.ConnectionPool) while (Globals.ConnectionPool.Count > 0) { Clientplayer.Disconnect(Globals.ConnectionPool.Dequeue(), "Server Shutting Down"); } Globals.Clients.Clear(); Log.Write("All Clients Disconnected!, Command Executed!"); }
private void TCP_Server() { TcpListener listener = null; Log.Write("Launching TCP Server and Initializing the Network...."); try { listener = new TcpListener(IPAddress.Parse(IP), port); listener.Start(); Log.Write("Network Initialized, listening for Connections on: {0}", listener.LocalEndpoint.ToString()); Processor.Run(procesors); new ProcessorWatcher().Start(watch); new ClientWatcher().Start(watchclient); Log.Write("Mission Completed Generel, you can go now!"); Console.ForegroundColor = ConsoleColor.DarkYellow; Console.WriteLine("**================================================================================================**"); while (this.Online) { TcpClient NewClient = listener.AcceptTcpClient(); if (Globals.Shield.IsBlocked(Clientplayer.GetRemoteEndPointInt(NewClient))) { try { NewClient.Close(); } catch { } continue; } if (Functions.IsServerFull) { Log.Write("New Player Queued, Server is FULL! ----- RemoteEndPoint: {0}", Clientplayer.GetRemoteEndPointString(NewClient)); Globals.ServerFullClients.Enqueue(NewClient); } else { Log.Write("New Player Connected! ----- RemoteEndPoint: {0}", Clientplayer.GetRemoteEndPointString(NewClient)); Globals.ConnectionPool.Enqueue(new Clientplayer(NewClient)); } } Shutdown(listener); } catch (Exception e) { Log.Write(e.ToString()); Shutdown(listener); } }
private void ClientsWatcher() // Heartbeats { bool check = false; short c = 0; do { Thread.Sleep(watch_delay); if (Globals.Clients.Count > 0) { if (c > 99) { foreach (Clientplayer temp in Globals.Clients.GetList()) { Clientplayer.CodeEvent(temp); } c = 0; } else { foreach (Clientplayer temp in Globals.Clients.GetList()) { if (check) { if (Clientplayer.IsAlive(temp)) { //lock(temp) if (!Clientplayer.Key(temp).Equals(Cryption.Decrypt( getkey(Clientplayer.GetRemoteEndPointIntPort(temp)), Clientplayer.GetRemoteEndPointString(temp), Clientplayer.GetCode16(temp), "SHA1", 10, Clientplayer.GetCodeByte(temp), 192))) { Warning.AddWarningToClient(Clientplayer.GetWarning(temp), true); } } } else { Clientplayer.Send(temp, UpdateKey(Clientplayer.GetRemoteEndPointIntPort(temp)), (byte)Enums.PacketID.key); } } check = !check; c++; } } } while (true); }
public static void Disconnect(Clientplayer x, string msg) { if (IsAlive(x)) { Log.Write("Client {0} disconnected, Reason: {1}", GetRemoteEndPointString(x), msg); x.networkStream.Close(); x.ClientSocket.Close(); } Globals.Clients.Remove(x); if (Warning.GetLevel(x.clientwarns) < 5 && Warning.GetKeyErrors(x.clientwarns) < 3) { Globals.Shield.UnBlock(GetRemoteEndPointInt(x)); } //Warning.Terminate(x.clientwarns); // done by Garbage collector since no pointer on element if client is off x.streambytes.Clear(); }
private void Shutdown(TcpListener me) { if (me != null && me.Server.IsBound) { me.Stop(); } Clientplayer.StopALL(); Processor.StopAll(); Globals.Processors.Clear(); Globals.Clients.Clear(); for (int i = 6; i > 0; i--) { Log.Write("We crashed! close me within {0} seconds, or i'll restart automaticly!", i); System.Threading.Thread.Sleep(1100); } string[] x = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Split(new char[] { '/' }); System.Diagnostics.Process.Start(x[x.Length - 1]); Environment.Exit(0); }
public static bool IsVaildProcess(Clientplayer x) { if (Warning.GetLevel(x.clientwarns) > 10) { if (DateTime.Compare(Warning.GetTime(x.clientwarns).AddSeconds(2), DateTime.Now) > 0) { Globals.Shield.Block(GetRemoteEndPointInt(x), x.clientwarns); Disconnect(x, "Illegal Packet delays"); return(false); } else { Warning.Reset(x.clientwarns); // prevent overrun } } else if (Warning.GetKeyErrors(x.clientwarns) > 2) { Globals.Shield.Block(GetRemoteEndPointInt(x), x.clientwarns); Disconnect(x, "Client Key Error!"); return(false); } return(IsAlive(x)); }
private void RunProcessor() { while (process) { Clientplayer client = null; lock (Globals.ConnectionPool.SyncRoot) { if (Globals.ConnectionPool.Count > 0) { client = Globals.ConnectionPool.Dequeue(); } } if (client != null) { client.Process(); if (Clientplayer.IsAlive(client)) { Globals.ConnectionPool.Enqueue(client); } } //Monitor.Pulse(this); if (Monitor.TryEnter(Globals.ServerFullClients.SyncRoot, 10))//fix bug, holding processors from client handling by full server { //lock (Globals.ServerFullClients.SyncRoot) //{ while (Globals.ServerFullClients.Count > 0) { Clientplayer.SendServerFull(Globals.ServerFullClients.DequeueT()); } //} Monitor.Exit(Globals.ServerFullClients.SyncRoot); } Thread.Sleep(100); // save CPU XDD } }
public static NetworkStream Stream(Clientplayer x) { return(IsAlive(x) ? x.networkStream : null); }
public void Remove(Clientplayer x) { lock (_lock) ClientList.Remove(x); }
public static byte[] GetCode16(Clientplayer x) { return(x.codebyte); }
public static string GetRemoteEndPointString(Clientplayer x) { return(IsAlive(x) ? x.ClientSocket.Client.RemoteEndPoint.ToString() : "IP.IP.IP.IP:PORT"); }
public static void SendServerFull(Clientplayer x) { Send(x, null, (byte)Enums.PacketID.ServerFULL); Disconnect(x, "Server Full"); }
public static int GetRemoteEndPointInt(Clientplayer x) { return(Functions.ipToInt(GetRemoteEndPointString(x).Split(new char[] { ':' })[0])); }
public static byte[] GetCodeByte(Clientplayer x) { return(x.codebyte0); }
public void AddToList(Clientplayer x) { lock (_lock) ClientList.Add(x); }
public static string GetRemoteEndPointIntPort(Clientplayer x) { string[] Ipport = GetRemoteEndPointString(x).Split(new char[] { ':' }); return(Functions.ipToInt(Ipport[0]) + "." + Ipport[1]); }
public static EndPoint PeerInfo(Clientplayer x) { return(IsAlive(x) ? x.ClientSocket.Client.RemoteEndPoint : null); }
public static string Key(Clientplayer x) { lock (x.key) // Watchers access return(x.key); }
public static bool IsAlive(Clientplayer x) { return(x.networkStream != null && x.ClientSocket != null && x.ClientSocket.Client != null && x.networkStream.CanRead && x.ClientSocket.Client.IsBound);//&& ClientSocket.Client.Available > 0; }
public static Warning GetWarning(Clientplayer x) { return(IsAlive(x) ? x.clientwarns : null); }
public static Int32 GetAid(Clientplayer x) { return(IsAlive(x) ? x.AID : 0); }
public static TcpClient Client(Clientplayer x) { return(IsAlive(x) ? x.ClientSocket : null); }
public void Enqueue(Clientplayer client) { SyncdQ.Enqueue(client); }