void Process() { try { stage = ProtocalStage.Connected; Packet.BeginReadPacket(skt, this, ReceivePacket, Disconnect); NWriter wtr = new NWriter(new NetworkStream(skt)); while (sendLock.WaitOne()) { if (stage == ProtocalStage.Disconnected) { break; } while (pending.Count > 0) { Packet pkt = pending.Dequeue(); //if (pkt.ID != PacketID.New_Tick && // pkt.ID != PacketID.Ping) // Console.WriteLine(pkt.ID); Packet.WritePacket(wtr, pkt, this); } } } catch { Disconnect(); } }
void ReceivePacket(Packet pkt) { if (stage == ProtocalStage.Disconnected) { return; } if (stage == ProtocalStage.Ready && (entity == null || entity != null && entity.Owner == null)) { return; } Packet.BeginReadPacket(skt, this, ReceivePacket, Disconnect); try { if (pkt.ID == PacketID.Hello) { ProcessHelloPacket(pkt as HelloPacket); } else if (pkt.ID == PacketID.Create) { ProcessCreatePacket(pkt as CreatePacket); } else if (pkt.ID == PacketID.Load) { ProcessLoadPacket(pkt as LoadPacket); } else if (pkt.ID == PacketID.Pong) { entity.Pong(pkt as PongPacket); } else if (pkt.ID == PacketID.Move) { RealmManager.AddPendingAction(t => entity.Move(t, pkt as MovePacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.PlayerShoot) { RealmManager.AddPendingAction(t => entity.PlayerShoot(t, pkt as PlayerShootPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.EnemyHit) { RealmManager.AddPendingAction(t => entity.EnemyHit(t, pkt as EnemyHitPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.OtherHit) { RealmManager.AddPendingAction(t => entity.OtherHit(t, pkt as OtherHitPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.SquareHit) { RealmManager.AddPendingAction(t => entity.SquareHit(t, pkt as SquareHitPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.PlayerHit) { RealmManager.AddPendingAction(t => entity.PlayerHit(t, pkt as PlayerHitPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.ShootAck) { RealmManager.AddPendingAction(t => entity.ShootAck(t, pkt as ShootAckPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.InvSwap) { RealmManager.AddPendingAction(t => entity.InventorySwap(t, pkt as InvSwapPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.InvDrop) { RealmManager.AddPendingAction(t => entity.InventoryDrop(t, pkt as InvDropPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.UseItem) { RealmManager.AddPendingAction(t => entity.UseItem(t, pkt as UseItemPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.UsePortal) { RealmManager.AddPendingAction(t => entity.UsePortal(t, pkt as UsePortalPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.PlayerText) { RealmManager.AddPendingAction(t => entity.PlayerText(t, pkt as PlayerTextPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.ChooseName) { RealmManager.AddPendingAction(t => ProcessChooseNamePacket(pkt as ChooseNamePacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.Escape) { RealmManager.AddPendingAction(t => ProcessEscapePacket(pkt as EscapePacket), PendingPriority.Emergent); } else if (pkt.ID == PacketID.Teleport) { RealmManager.AddPendingAction(t => entity.Teleport(t, pkt as TeleportPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.GotoAck) { RealmManager.AddPendingAction(t => entity.GotoAck(t, pkt as GotoAckPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.EditAccountList) { RealmManager.AddPendingAction(t => entity.EditAccountList(t, pkt as EditAccountListPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.Buy) { RealmManager.AddPendingAction(t => entity.Buy(t, pkt as BuyPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.RequestTrade) { RealmManager.AddPendingAction(t => entity.RequestTrade(t, pkt as RequestTradePacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.ChangeTrade) { RealmManager.AddPendingAction(t => entity.ChangeTrade(t, pkt as ChangeTradePacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.AcceptTrade) { RealmManager.AddPendingAction(t => entity.AcceptTrade(t, pkt as AcceptTradePacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.CancelTrade) { RealmManager.AddPendingAction(t => entity.CancelTrade(t, pkt as CancelTradePacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.AOEAck) { RealmManager.AddPendingAction(t => entity.AOEAck(t, pkt as AOEAckPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.GroundDamage) { RealmManager.AddPendingAction(t => entity.GroundDamage(t, pkt as GroundDamagePacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.CheckCredits) { RealmManager.AddPendingAction(t => entity.CheckCredits(t, pkt as CheckCreditsPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.CreateGuild) { RealmManager.AddPendingAction(t => entity.CreateGuild(pkt as CreateGuildPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.JoinGuild) { RealmManager.AddPendingAction(t => entity.JoinGuild(pkt as JoinGuildPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.GuildInvite) { RealmManager.AddPendingAction(t => entity.InviteToGuild(pkt as InviteToGuildPacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.ChangeGuildRank) { RealmManager.AddPendingAction(t => entity.ChangeRank(pkt as GuildRankChangePacket), PendingPriority.Networking); } else if (pkt.ID == PacketID.GuildRemove) { RealmManager.AddPendingAction(t => entity.RemoveFromGuild(pkt as GuildRemovePacket), PendingPriority.Networking); } else if (pkt.ID != PacketID.Packet) { Console.WriteLine("Unhandled packet: " + pkt.ToString()); } else { Console.WriteLine((int)pkt.ID); } } catch { Disconnect(); } }