public Peer(string name) { CanAppend = true; IncomingMessages = new ConcurrentQueue <string>(); this.Name = name; protocolExample = GemClient.Profile("GemChat") .CreateNetworkProtocolEvent <Package>() .HandleIncoming((sender, package) => { QueueMessage(String.Format("Server sent {0} {1}", sender.Statistics.SentBytes, package.Name)); }) .GenerateSendEvent(); onEvent = GemClient.Profile("GemChat") .CreateNetworkEvent .AndHandleWith(this, x => new Action <string>(x.QueueMessage)); onCommandExecute = GemClient.Profile("GemChat") .CreateNetworkEvent .AndHandleWith(this, x => new Action <string>(x.ExecuteCommand)); onEvent.Send(name + " has joined"); messageAppender = new ParallelTaskStarter(TimeSpan.Zero); messageAppender.Start(DequeueIncomingMessages); }
public void ProtocolHandlerInvokesAction() { string expectedPackage = string.Empty; var protocolInvocation = GemClient.Profile("Test") .CreateNetworkProtocolEvent <Package>() .HandleIncoming((sender, package) => expectedPackage = package.Name); }
public void ChangeName(string newName) { string formattedMessage = String.Format(" >> {0} changed his/her name to {1}", Name, newName); Console.WriteLine("you have changed your name to {0}", newName); this.Name = newName; onEvent.Send(formattedMessage); GemClient.NotifyServer("newname " + newName); }
public void ComplexDynamicEventInvocationTest() { var client = new GemClient("tag", new ConnectionConfig(), PackageConfig.TCP); var tester = new Mock <EventTester>(); var messageHandler = GemClient.Profile("tag") .CreateNetworkEvent .AndHandleWith(tester.Object, x => new Action <string, int, int, string>(x.DoSomethingComplicated)); //Start a server to verify this //messageHandler.Send("say", 1, 2, "error"); // tester.Verify(x => x.DoSomethingComplicated("say", 1, 2, "error"), Times.Once); }
public Actor(string name, ContentManager content, Vector2 location, EventManager eventManager, int maxLives) : this(name, content, location, maxLives) { this.livesRemaining = maxLives; this.eventManager = eventManager; onLocationChange = GemClient.Profile("Shooter") .CreateNetworkEventWithRemoteTime .AndHandleWith(eventManager, x => new Action <string, float, float, double>(x.SetLocation)); onShoot = GemClient.Profile("Shooter") .CreateNetworkEventWithRemoteTime .AndHandleWith(eventManager, x => new Action <string, float, float, float, float, double>(x.Shoot)); }
private static void ClientSetup() { GemNetworkDebugger.Echo = Console.WriteLine; //client = new GemClient("GemChat", "GemChat", "83.212.103.13",, 14242, name); client = new GemClient("GemChat", new ConnectionConfig { ServerName = "GemChat", IPorHost = ipOrHost, Port = 14242, DisconnectMessage = name }, PackageConfig.TCP); GemClient.Profile("GemChat").OnReceivedServerNotification(x => peer.QueueMessage(x.Message)); }
public void DynamicEventInvocationTest() { var tester = new Mock <EventTester>(); var messageHandler = GemClient.Profile("tag") .CreateNetworkEvent .AndHandleWith(tester.Object, x => new Action <string>(x.AppendSomething)); //Start a server to verify this //messageHandler.Send("say"); //tester.Verify(x => x.AppendSomething("say"), Times.Once); //Start a server to verify this //messageHandler.Send("say twice"); //messageHandler.Send("say twice"); // tester.Verify(x => x.AppendSomething("say twice"), Times.Exactly(2)); }
public EventManager(ContentManager content, string name) { this.content = content; this.actors = new Dictionary <string, Actor>(); this.commandTable = new Dictionary <string, Action <string> >(); GemNetworkDebugger.Echo = Console.WriteLine; RegisterCommands(); //client = new GemClient("GemChat", "GemChat", "83.212.103.13",, 14242, name); client = new GemClient("Shooter", new ConnectionConfig { ServerName = "Shooter", IPorHost = "localhost", Port = 14242, DisconnectMessage = name }, PackageConfig.UDPSequenced); GemClient.Profile("Shooter").OnReceivedServerNotification(x => { if (x.Type == NotificationType.Command) { var command = x.Message.Split(' ')[0]; if (commandTable.ContainsKey(command)) { commandTable[command](x.Message); } } if (x.Type == NotificationType.Message) { Console.WriteLine(x.Message); } }); client.RunAsync(() => new ConnectionApprovalMessage { Message = "Incoming client", Sender = name, Password = "******" }); }
private static void RegisterCommands() { CommandTable.Add("-cls", x => { if (HasOnlyOneArgument(x)) { Console.Clear(); return(true); } return(false); }); CommandTable.Add("-clsx", x => { if (HasOnlyOneArgument(x)) { Console.Clear(); peer.CanAppend = false; return(true); } return(false); }); CommandTable.Add("-help", x => { if (HasOnlyOneArgument(x)) { PrintIntroMessage(); return(true); } return(false); }); CommandTable.Add("-lock", x => { if (HasOnlyOneArgument(x)) { Console.WriteLine("[Locked]"); peer.CanAppend = false; return(true); } return(false); }); CommandTable.Add("-unlock", x => { if (HasOnlyOneArgument(x)) { Console.WriteLine("[Unlocked]"); peer.CanAppend = true; return(true); } return(false); }); CommandTable.Add("-gem", x => { if (x.Length < 5) { return(false); } var cmd = x.Substring(5); if (cmd.Length > 0) { GemClient.SendCommand(cmd); return(true); } return(false); }); CommandTable.Add("-setname", x => { var args = x.Split(' '); if (args.Count() >= 2) { peer.ChangeName(args[1]); return(true); } else { Console.WriteLine("Unknown Command"); return(false); } }); CommandTable.Add("-yt", x => { try { if (x.Split(' ').Count() < 2) { Console.WriteLine("Unknown Command"); return(false); } var args = x.Substring(4); if (x.Contains("|")) { var index = x.Substring(x.IndexOf('|') + 1); int indexPos; if (Int32.TryParse(index, out indexPos)) { var breakIndex = args.IndexOf("|"); if (breakIndex > 0) { args = args.Substring(0, breakIndex); } peer.Send(String.Format("[ {0} searched for {1} ]", peer.Name, args)); search.Run(args, indexPos).Wait(); return(true); } else { Console.WriteLine("Invalid index"); return(true); } } else { peer.Send(String.Format("[ {0} searched for {1} ]", peer.Name, args)); search.Run(args, 5).Wait(); return(true); } } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); return(false); } }); CommandTable.Add("-play", x => { var args = x.Split(' '); if (args.Count() == 2) { int index; if (Int32.TryParse(args[1], out index)) { string title; if (search.Play(index, out title)) { peer.Send(String.Format("[ {0} is watching {1} ] ", peer.Name, title)); return(true); } return(false); } else { Console.WriteLine("Invalid index"); return(false); } } else { Console.WriteLine("Unknown Command"); return(false); } }); }