static void HandleCommands(bool connected, string msg) { switch (msg) { case "time": var getTime = _client.GetRemoteFunction <string>("GetTime"); //Call a function that requires authentication checking. getTime.CallWait(); //If call failed, return will be the default value for the type if (getTime.LastStatus == FunctionResponseStatus.Success) { //No issues Console.WriteLine(getTime.LastValue); //Write last returned value to console } else { Console.WriteLine("Call failed. reason: {0}. Try the \"toggletime\" command", getTime.LastStatus); } break; case "say": Console.WriteLine("Enter the string to send to the server:"); var input = Console.ReadLine(); if (string.IsNullOrEmpty(input)) { Console.WriteLine("Input empty."); break; } var say = _client.GetRemoteFunction <string>("say"); var sayCmd = say.CallWait(input); Console.WriteLine($"Server said: {sayCmd}"); break; case "toggletime": var toggleTime = _client.GetRemoteFunction <string>("toggletime"); Console.WriteLine(toggleTime.CallWait()); //If call fails, nothing (null) will be printed because it is strings default value. break; default: if (connected) { _client.Send(new ChatMessage(msg)); } break; } }
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); }
public SecureFunction <T> GetFunction <T>(string name) { if (!client.Connected) { return(null); } lock (SyncLock) { var realName = string.Format("{0}.{1}", AppID, name); if (FuncList.ContainsKey(realName)) { return(FuncList[realName] as SecureFunction <T>); } else { var newFunc = new SecureFunction <T>(client.GetRemoteFunction <T>(realName)); FuncList.Add(realName, newFunc); Console.WriteLine(realName); return(newFunc); } } }
private static void Client() { //When sending custom objects, you must create a new packer and specify them. var packer = new Packager(new Type[] { typeof(SetName), typeof(ChatMessage) }); //Using ipv4 and the packer that has the custom types. var client = new SyncIOClient(TransportProtocal.IPv4, packer); //The diffrent types of handlers: (all optional) //The type handler. //This handler handles a specific object type. client.SetHandler <ChatMessage>((SyncIOClient sender, ChatMessage messagePacket) => { //All ChatMessage packages will be passed to this callback Console.WriteLine(messagePacket.Message); }); //This handler handles any IPacket that does not have a handler. client.SetHandler((SyncIOClient sender, IPacket p) => { //Any packets without a set handler will be passed here }); //This handler handles anything that is not a SINGLE IPacket object client.SetHandler((SyncIOClient sender, object[] data) => { //Any object array sent will be passed here, even if the array contains //A packet with a handler (e.g. ChatMessage) }); if (!client.Connect("127.0.0.1", new Random().Next(9996, 10000)))//Connect to any of the open ports. { ConsoleExtentions.ErrorAndClose("Failed to connect to server."); } //Connecting and handshake are not the same. //Connecting = Establishing a connection with a socket //Handskake = Establishing a connection with a SyncIOServer and getting an ID. Console.WriteLine("Connected on port {0}. Waiting for handshake.", client.EndPoint.Port); bool success = client.WaitForHandshake(); /* * The asynchronous way to get handshake would be subscribing * to the client.OnHandshake event. */ if (!success) { ConsoleExtentions.ErrorAndClose("Handshake failed."); } Console.WriteLine("Handshake success. Got ID: {0}", client.ID); var name = ConsoleExtentions.GetNonEmptyString("Enter a name: "); client.Send(new SetName(name)); Console.WriteLine("Name set. You can now send messages."); bool connected = true; client.OnDisconnect += (s, err) => { connected = false; }; var GetTime = client.GetRemoteFunction <string>("GetTime"); var toggletime = client.GetRemoteFunction <string>("toggletime"); while (connected) { var msg = ConsoleExtentions.GetNonEmptyString(""); if (msg == "time") { //Call a function that requires authentication checking. GetTime.CallWait(); //If call failed, return will be the default value for the type if (GetTime.LastStatus == FunctionResponceStatus.Success) //No issues { Console.WriteLine(GetTime.LastValue); //Write last returned value to console } else { Console.WriteLine("Call failed. reason: {0}. Try the \"toggletime\" command", GetTime.LastStatus); } } else if (msg == "toggletime") { Console.WriteLine(toggletime.CallWait()); //If call fails, nothing (null) will be printed because it is strings default value. } else { if (connected) { client.Send(new ChatMessage(msg)); } } } ConsoleExtentions.ErrorAndClose("Lost connection to server"); }