public void OnDisconnect(ref PacketReader _pReader, PacketType pt, ref ClientManager _client) { GridServer.player_list[_client.id]._client.Close(); GridServer.player_list[_client.id]._stream.Close(); GridServer.player_list[_client.id] = null; _client.Stop(true); }
public void OnServerListPing(ref PacketReader _pReader, PacketType pt, ref ClientManager _client) { GridServer.player_list[_client.id].WaitToRead = false; DisconnectKick p = new DisconnectKick(PacketType.Disconnect_Kick); p.Reason = "Ping"; p.BuildPacket(); _client.SendPacket(p, _client.id, ref _client, true, false); /*ServerListPingPacket p = new ServerListPingPacket(); p.NumberOfSlots = (int)Config.Configuration["MaxPlayers"]; p.ServerDescription = (string)Config.Configuration["ServerDescription"]; p.NumberOfUsers = GridServer.PlayerCount() - 1; p.BuildPacket(); _client.SendPacket(p, _client.id, ref _client, true, false);*/ //GridServer.player_list[_client.id] = null; //p = null; }
public PacketHandler OnBlockChange(PacketType p_type, string CustomPacketType, ref PacketReader packet_reader, PacketHandler _p, ref ClientManager cm) { return _p; }
public void Recieve() { Nanosecond nano = new Nanosecond(); //System.Diagnostics. int _id = id; System.GC.KeepAlive(GridServer.player_list[_id]); _stream = new NetworkStream(_client.Client); NetworkStream stream = new NetworkStream(_client.Client); PacketReader p_reader = new PacketReader(stream); //bool connected = true; while (GridServer.player_list[_id] != null && !GridServer.player_list[_id]._shouldStop) { if (nano.RunMiliSecond()) System.Threading.Thread.Sleep(1); try { if (GridServer.player_list[_id] == null) { break; } if (GridServer.player_list[_id].Suspendv == false) { if (GridServer.player_list[_id]._client.Connected == false) { GridServer.player_list[_id].Stop(true); return; } if (GridServer.player_list[_id]._client.Client.Available > 0) { //System.Threading.Thread.Sleep(1); GridServer.player_list[_id].WaitToRead = true; byte read = p_reader.ReadByte(); PacketType p_type = (PacketType)read; //Console.WriteLine("Packet Recieved: " + p_type.ToString() + " Packet Byte:" + (byte)p_type); if (ModuleHandler.Eventmodules.Keys.Contains(p_type)) { Thread.BeginCriticalRegion(); ModuleHandler.Eventmodules[p_type](ref p_reader, p_type, ref GridServer.player_list[_id]); Thread.EndCriticalRegion(); //GridServer.player_list[_id].WaitToRead = false; } else { System.Console.WriteLine(p_type.ToString() + " is not implemented:" + (byte)p_type); byte[] unsupported_garbage = new byte[(int)p_reader.reader.Length]; p_reader.reader.Read(unsupported_garbage, 0, (int)p_reader.reader.Length); GridServer.player_list[_id].WaitToRead = false; } if (GridServer.player_list[_id] != null && GridServer.player_list[_id]._stream != null && DateTime.Now.Minute > GridServer.player_list[_id].keep_alive.Minute || GridServer.player_list[_id].keep_alive.Second + 1 < DateTime.Now.Second) { Random r = new Random(1789); if (GridServer.player_list[_id].customAttributes.ContainsKey("PayLoad")) GridServer.player_list[_id].customAttributes["PayLoad"] = (object)r.Next(1024, 4096); LibOpenCraft.ServerPackets.KeepAlivePacket p = new LibOpenCraft.ServerPackets.KeepAlivePacket(PacketType.KeepAlive); p.ID = (int)GridServer.player_list[_id].customAttributes["PayLoad"]; p.BuildPacket(); #region ProcessLatency ms_latency.Start(); #endregion ProcessLatency SendPacket(p, _id, ref GridServer.player_list[_id], false, false); } } else { if (_player.name == "") Thread.Sleep(50); else { if (GridServer.player_list[_id]._player.EntityUpdateCount < (int)Config.Configuration["EntityUpdate"]) { ClientManager[] player = GridServer.player_list; for (int i = 0; i < player.Length; i++) { if (player[i] == null || player[i].id == _id || player[i].PreChunkRan != 1) { } else { LibOpenCraft.ServerPackets.EntityPacket e = new LibOpenCraft.ServerPackets.EntityPacket(PacketType.Entity); e.EntityID = _id; e.BuildPacket(); player[i].SendPacket(e, player[i].id, ref player[i], false, false); GridServer.player_list[_id]._player.EntityUpdateCount++; } } } else GridServer.player_list[_id]._player.EntityUpdateCount = 0; } } } } catch (Exception e) { Console.WriteLine("ERROR: " + e.Message + "\nSource:" + e.Source + "\nMethod:" + e.TargetSite + "\nData:" + e.Data + "\nStackTrace:" + e.StackTrace); if (GridServer.player_list[_id] != null) { GridServer.player_list[_id].Stop(true); } else { Stop(true); } } } }