private void SetupRpcs() { var sayHi = _server.RegisterRemoteFunction("say", new Func <string, string>((string msg) => { WriteLog(LogType.Debug, $"Say function called: {msg}"); return(msg); })); var getTime = _server.RegisterRemoteFunction("GetTime", new Func <string>(() => { WriteLog(LogType.Debug, "Time function called"); return(string.Format("It is {0}.", DateTime.Now.ToShortTimeString())); })); getTime.SetAuthFunc((c, f) => { return(_clients[c.ID].CanUseTimeCommand); }); var toggleTime = _server.RegisterRemoteFunction("toggletime", new Func <string>(() => "\"time\" command has been toggled.")); toggleTime.SetAuthFunc((c, f) => { return(_clients[c.ID].CanUseTimeCommand = !_clients[c.ID].CanUseTimeCommand); }); }
public void TestRemoteCalls() { var client = new SyncIOClient(); var server = new SyncIOServer(); var Test1 = server.RegisterRemoteFunction("Test1", new Func <string, int>((string string1) => string1.Length)); server.RegisterRemoteFunction("Test2", new Func <string, string>((string string1) => string.Concat(string1.Reverse()))); server.RegisterRemoteFunction("Test3", new Func <string, char>((string string1) => string1.FirstOrDefault())); Test1.SetAuthFunc((SyncIOConnectedClient requester, RemoteFunctionBind callingFunc) => { //Example of authenticating call to Test1 return(true); //Always allow call }); var listenSock = server.ListenTCP(6000); Assert.IsNotNull(listenSock); client.OnDisconnect += (sCl, err) => { throw new AssertFailedException(err.Message, err); }; Assert.IsTrue(client.Connect("127.0.0.1", 6000)); Assert.IsTrue(client.WaitForHandshake()); var testParam = "Hello World"; var func1 = client.GetRemoteFunction <int>("Test1"); var func2 = client.GetRemoteFunction <string>("Test2"); var func3 = client.GetRemoteFunction <char>("Test3"); Assert.AreEqual(testParam.Length, func1.CallWait(testParam)); Assert.AreEqual(string.Concat(testParam.Reverse()), func2.CallWait(testParam)); Assert.AreEqual(testParam.FirstOrDefault(), func3.CallWait(testParam)); func1.CallWait(1, 2, 3, 4, 5); Assert.AreEqual(func1.LastStatus, FunctionResponceStatus.InvalidParameters); }
private void LoadFunctions() { var dirInfo = new DirectoryInfo("Remote"); if (!dirInfo.Exists) { dirInfo.Create(); } foreach (var file in dirInfo.GetFiles("*.dll")) { try { var asm = Assembly.LoadFile(file.FullName); foreach (var t in asm.GetTypes()) { var typeAtt = t.GetCustomAttribute <RemoteAppAttribute>(); if (typeAtt == null) { continue; } foreach (var m in t.GetMethods(BindingFlags.Public | BindingFlags.Static)) { var methodAtt = m.GetCustomAttribute <RemoteFunctionAttribute>(); if (methodAtt == null) { continue; } var del = System.Linq.Expressions.Expression.GetDelegateType(m.GetParameters() .Select(x => x.ParameterType) .Concat(new[] { m.ReturnType }) .ToArray()); var profile = new RemoteFunctionProfile(typeAtt.AppID, methodAtt.Name, methodAtt.Description, m); var funcName = string.Format("{0}.{1}", profile.ApplicationID, profile.FunctionName); var newRemoteFunc = server.RegisterRemoteFunction(funcName, m.CreateDelegate(del)); newRemoteFunc.Tag = new FunctionInfo(typeAtt.AppID, typeAtt.ApiToken); lvFunctions.Items.Add(profile); } } }catch { MessageBox.Show("Failed to load " + file.Name); } } }
private static void Server() { var packer = new Packager(new Type[] { typeof(SetName), typeof(ChatMessage) }); var server = new SyncIOServer(TransportProtocal.IPv4, packer); var clients = new Dictionary <Guid, ConnectedChatClient>(); var sendToAll = new Action <IPacket>((p) => { //Send to all clients who have set a name. foreach (var c in clients.Select(x => x.Value)) { c.Connection.Send(p); } }); var gettime = server.RegisterRemoteFunction("GetTime", new Func <string> (() => { Console.WriteLine("Time function called"); return(string.Format("It is {0}.", DateTime.Now.ToShortTimeString())); })); gettime.SetAuthFunc((c, f) => { return(clients[c.ID].CanUseTimeCommand); }); var toggletime = server.RegisterRemoteFunction("toggletime", new Func <string>(() => "\"time\" command has been toggled.")); toggletime.SetAuthFunc((c, f) => { clients[c.ID].CanUseTimeCommand = !clients[c.ID].CanUseTimeCommand; return(true); }); server.OnClientConnect += (SyncIOServer sender, SyncIOConnectedClient client) => { Console.WriteLine("{0}] New connection", client.ID); client.OnDisconnect += (c, ex) => { Console.WriteLine("[{0}] Disconnected: {1}", c.ID, ex.Message); }; }; server.SetHandler <SetName>((c, p) => { sendToAll(new ChatMessage($"{p.Name} connected. ({c.ID})")); clients.Add(c.ID, new ConnectedChatClient(c, p.Name)); }); server.SetHandler <ChatMessage>((c, p) => { var msg = $"<{clients[c.ID].Name}> {p.Message}"; sendToAll(new ChatMessage(msg)); Console.WriteLine(msg); }); Console.WriteLine("Closing socket examples:"); //Listen on all of the following ports: var firstSock = server.ListenTCP(9996); //Add it to a variable for closing example server.ListenTCP(9997); server.ListenTCP(9998); server.ListenTCP(9999); if (server.Count() < 1) { ConsoleExtentions.ErrorAndClose("Failed to listen on any ports."); } foreach (var sock in server) { Console.WriteLine("Listening on {0}", sock.EndPoint.Port); sock.OnClose += (sender, err) => { Console.WriteLine("{0}] Socket closed. {1}", sender.EndPoint.Port, err); }; } Console.WriteLine("Closing port {0} and 9998", firstSock.EndPoint.Port); firstSock.Dispose(); //Either close from var server[9997].Dispose(); //Or by server index. foreach (var sock in server) { Console.WriteLine("Listening on {0}", sock.EndPoint.Port); } Console.WriteLine("Reopening ports in 3 seconds"); Thread.Sleep(3000); Console.Clear(); //Reopen: server.ListenTCP(9996); server.ListenTCP(9997); foreach (var sock in server) { Console.WriteLine("Listening on {0}", sock.EndPoint.Port); } while (true) { Console.ReadLine(); } }