private void Events_OnEventReceived(object myObject, SystemEventPacket eventPacket) { try { SystemEventPacket packet = SystemEventQueue.Pop(); if (packet == null) packet = eventPacket; switch (packet.eventType) { case EventType.connect: Connections++; break; case EventType.disconnect: int index = connectedListBox.FindString(packet.Client.Name); Invoke((MethodInvoker)delegate { connectedListBox.Items.RemoveAt(index); }); Connections--; break; case EventType.login: string newPlayer = packet.Client.Name + " : " + packet.Client.IPAddress.ToString() + ":" + packet.Client.Port.ToString(); Invoke((MethodInvoker)delegate { connectedListBox.Items.Add(newPlayer); }); break; } Invoke((MethodInvoker)delegate { connectedLabel.Text = Connections.ToString(); Console.Items.Add("Event: " + packet.message); }); } catch (Exception e) { Invoke((MethodInvoker)delegate { Console.Items.Add("Event Error: " + e.ToString()); }); } }
private void CheckConnections() { List<Connection> RemoveList = new List<Connection>(); foreach (Connection player in Common.Settings.Players) { if (player.Connected) continue; RemoveList.Add(player); } foreach (Connection player in RemoveList) { Common.Settings.Players.Remove(player.Name, "Lost connection."); player.Room.Players.Remove(player.Name); SystemEventPacket packet = new SystemEventPacket(EventType.disconnect, player.Name + " lost connection."); Common.Settings.SystemEventQueue.Push(packet); player.Shutdown(); player.Dispose(); } }
private void Quit(Packet packet) { Common.Settings.Players.Remove(packet.Client.Name, "Player has left."); packet.Client.Send("See you again soon!".Ansi(Style.white).NewLine().NewLine()); packet.Client.Room.Players.Remove(packet.Client.Name); SystemEventPacket eventPacket = new SystemEventPacket(EventType.disconnect, packet.Client.Name + " has left.", packet.Client); Common.Settings.SystemEventQueue.Push(eventPacket); packet.Client.Shutdown(); packet.Client.Dispose(); }
public void Push(SystemEventPacket packet) { queue.Enqueue(packet); OnEventReceived(this, packet); }
protected void StartReceiving() { SystemEventPacket packet = new SystemEventPacket(EventType.connect, "New Connection begun from " + this.IPAddress.ToString(), this); Common.Settings.SystemEventQueue.Push(packet); try { State.Socket.Client.BeginReceive(State.Buffer, 0, State.Buffer.Length, SocketFlags.None, ReceiveCallback, State); } catch (Exception e) { Common.Settings.SystemMessageQueue.Push("Connection closed: " + e.ToString()); } }
public void StartPlayer() { LoginDispatcher = null; PlayerDispatcher = new PlayerDispatcher(this); Commands = PlayerDispatcher.OnPlayerMessageReceived; SystemEventPacket packet = new SystemEventPacket(EventType.login, this.Name + " has entered the world.", this); Common.Settings.SystemEventQueue.Push(packet); if (Room == null) SetRoom(null); Room.AddPlayer(this); }