private static byte[] UDPtoBytes(UDPFrame frame) { using (MemoryStream ms = new MemoryStream()) { Serializer.Serialize(ms, frame); return(ms.ToArray()); } }
/// <summary> /// Sends a UDPFrame packet to the connected partner /// </summary> /// <param name="frame"></param> /// <returns></returns> public static int SendFrame(UDPFrame frame) { if (SaveData.GameSettings.coOp && partner != null) { System.Diagnostics.Debug.WriteLine("Sending frame: " + frame.subject.ToString()); frame.packetID = Guid.NewGuid(); UDPMessager.SendFrame(frame, partner); if (frame.subject != UDPFrame.FrameType.Confirmation) { lock (confirmationQueue) confirmationQueue.Add(frame); } return(1); } return(0); }
public static void RetreivalLoop() { IPEndPoint EP = new IPEndPoint(IPAddress.Any, port); while (SaveData.GameSettings.coOp) { byte[] data = server.Receive(ref EP); UDPFrame frame = BytestoUDP(data); if (receieved.Contains(frame.packetID)) { continue; } else { receieved.Add(frame.packetID); SessionManager.CommandSwitching(frame, EP); } if (receieved.Count > 200) //Keep track of the last 200 packets to allow rejection of duplicates { receieved.Clear(); } } }
public static void CommandSwitching(UDPFrame frame, IPEndPoint ep) { switch (frame.subject) { case UDPFrame.FrameType.Confirmation: int index = -1; lock (confirmationQueue) { for (int i = 0; i < confirmationQueue.Count; i++) { if (confirmationQueue[i].packetID.Equals(frame.uid)) { index = i; } } if (index >= 0) { confirmationQueue.RemoveAt(index); } } return; case UDPFrame.FrameType.Heartbeat: System.Diagnostics.Debug.WriteLine("Heartbeat"); break; case UDPFrame.FrameType.Movement: //Packet received by guest to update position of an object that moved on the host's end System.Diagnostics.Debug.WriteLine("Movement"); /* Movement currently broken * if (frame.floatData.Length > 1 && frame.stringData.Length > 0 && WorldData.currentRegions.ContainsKey(frame.stringData[0])) { * lock(WorldData.currentRegions) * foreach (World.Objects.WorldObjectBase b in WorldData.currentRegions[frame.stringData[0]].GetWorldObjects()) { * if (b.uid.Equals(frame.uid)) { * b.SetWorldPosition(frame.floatData[0], frame.floatData[1]); * break; * } * } * } */ break; case UDPFrame.FrameType.Quest: System.Diagnostics.Debug.WriteLine("Quest"); break; case UDPFrame.FrameType.Player: switch (frame.intData[0]) { case 0: System.Diagnostics.Debug.WriteLine("Loaded new guest player"); WorldData.AddPlayer(frame.uid); if (isHost) //If host received guest, return the host character to the guest { SendFrame(new UDPFrame() { subject = UDPFrame.FrameType.Player, intData = new int[] { 0 }, uid = WorldData.player.uid }); SendFrame(new UDPFrame() { subject = UDPFrame.FrameType.Player, intData = new int[] { 1 }, floatData = new float[] { WorldData.player.worldX, WorldData.player.worldY } }); } break; case 1: if (WorldData.partner != null) { WorldData.partner.SetWorldPosition(frame.floatData[0], frame.floatData[1]); } break; } break; case UDPFrame.FrameType.Connection: System.Diagnostics.Debug.WriteLine("Connection"); if (frame.intData.Length == 0) { break; } switch (frame.intData[0]) { case 0: //Sever connection EndPartner(); break; case 1: //New Partner is Guest NewPartner(ep); break; case 2: //Text message GUI.Windows.NotificationWindow.NewNotification(frame.stringData[0], 120); System.Diagnostics.Debug.WriteLine("message: " + frame.stringData[0]); break; case 3: //Confirmation from host SendFrame(new UDPFrame() { subject = UDPFrame.FrameType.Player, uid = SaveData.GameSettings.coOpUID, intData = new int[] { 0 } }); break; default: System.Diagnostics.Debug.WriteLine("Connection Value: " + frame.intData[0]); break; } break; case UDPFrame.FrameType.WorldObject: //World Objects not supported by packets currently //WorldData.PacketToWorldObject(frame); break; } SendFrame(new UDPFrame() { subject = UDPFrame.FrameType.Confirmation, uid = frame.packetID }); }
public static void SendFrame(UDPFrame frame, IPEndPoint p) { byte[] buff = UDPtoBytes(frame); server.Send(buff, buff.Length, p); }