3 Replies - 423 Views - Last Post: 25 July 2013 - 10:16 PM Rate Topic: -----

#1 theref  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-June 13

Client does not wait for method invocations by server

Posted 25 July 2013 - 03:00 PM

Hmm, might be a bit messy. After read the tutorial here http://www.codeproje...ication-and-RMI I'm trying to create my own new project for experimenting. So, inside my solutions, there are 3 projects
  • ChatClient
  • ChatCommonLib
  • ChatServer


Basically the client will send stuff to server (UserData("Marko", 17, "Male")) then the server will send something back to Client and trigger the method inside it to write something on Client's console

_clients[client.ClientId].ClientProxy.OnMessageToRoom("Richard", "Hello");


The client can access method of server fine, works like charm. But on reverse, I can't get my server to access the method of my client, it says

Quote

An exception of type 'Hik.Communication.ScsServices.Communication.Messages.ScsRemoteException' occurred in mscorlib.dll but was not handled in user code

Additional information: Client does not wait for method invocations by server.

If there is a handler for this exception, the program may be safely continued.


Here is my current code :
ChatClient
Client.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ChatCommonLib;

namespace ChatClient
{
    internal class Client : iClient
    {
        public Client()
        {

        }
        public void OnMessageToRoom(string name, string message)
        {
            Console.WriteLine(name + ": " + message);
        }
    }
}


Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hik.Communication.Scs.Communication.EndPoints.Tcp;
using Hik.Communication.ScsServices.Client;
using ChatCommonLib;

namespace ChatClient
{
    class Program
    {
        static void Main(string[] args)
        {
            var client = ScsServiceClientBuilder.CreateClient<iServer>(
              new ScsTcpEndPoint("127.0.0.1", 10048));
            var client2 = ScsServiceClientBuilder.CreateClient<iClient>(
              new ScsTcpEndPoint("127.0.0.1", 10048));
            Console.WriteLine("Press enter to connect...");
            Console.ReadLine();

            //Connect to the server
            client.Connect();
            client.ServiceProxy.test();
            client.ServiceProxy.Login(new UserData("Marko", 17, "Male"));     
            Console.ReadLine();
        }
    }
}



ChatCommonLib
iClient.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hik.Communication.ScsServices.Service;

namespace ChatCommonLib
{
    [ScsService]
    public interface iClient
    {
        void OnMessageToRoom(string name, string message);
    }
}


iServer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hik.Communication.ScsServices.Service;

namespace ChatCommonLib
{
    [ScsService]
    public interface iServer
    {
        void Login(UserData userData);

        void SendMessage(string message);

        void Logout();

        void test();
    }
}



ChatServer
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hik.Communication.Scs.Communication.EndPoints.Tcp;
using Hik.Communication.ScsServices.Service;
using ChatCommonLib;

namespace ChatServer
{
    class Program
    {
        
        static void Main(string[] args)
        {
             IScsServiceApplication _serviceApplication;
             Server _chatService;
            _serviceApplication = ScsServiceBuilder.CreateService(new ScsTcpEndPoint(10048));
            _chatService = new Server();
            _serviceApplication.AddService<iServer, Server>(_chatService);
           
            _serviceApplication.Start();
            Console.WriteLine("Server Started...");
            Console.ReadLine();

        }
    }
}


Server.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ChatCommonLib;
using Hik.Collections;
using Hik.Communication.ScsServices.Service;

namespace ChatServer
{
    internal class Server : ScsService, iServer
    {
        public List<UserData> userList
        {
            get
            {
                return (from client in _clients.GetAllItems()
                        select client.User).ToList();
            }
        }

        private readonly ThreadSafeSortedList<long, ChatClient> _clients;

        public Server()
        {
            _clients = new ThreadSafeSortedList<long, ChatClient>();
        }

        public void Login(UserData userInfo)
        {
            var client = CurrentClient;
            var clientProxy = client.GetClientProxy<iClient>();
            var chatClient = new ChatClient(client, clientProxy, userInfo);
            _clients[client.ClientId] = chatClient;
            _clients[client.ClientId].ClientProxy.OnMessageToRoom("Richard", "Hello");
            client.Disconnected += Client_Disconnected;
        }

        private void Client_Disconnected(object sender, EventArgs e)
        {
            //Get client object
            var client = (IScsServiceClient)sender;

            //Perform logout (so, if client did not call Logout method before close,
            //we do logout automatically.
            ClientLogout(client.ClientId);
        }

        public void Logout()
        {
            ClientLogout(CurrentClient.ClientId);
        }
        private void ClientLogout(long clientId)
        {
            //Get client from client list, if not in list do not continue
            var client = _clients[clientId];
            if (client == null)
            {
                return;
            }

            //Remove client from online clients list
            _clients.Remove(client.Client.ClientId);

            //Unregister to Disconnected event (not needed really)
            client.Client.Disconnected -= Client_Disconnected;
        }

        public void SendMessage(string message)
        {
            //Get ChatClient object
            var senderClient = _clients[CurrentClient.ClientId];
            if (senderClient == null)
            {
                throw new ApplicationException("Can not send message before login.");
            }

            //Send message to all online users
            Task.Factory.StartNew(
                () =>
                {
                    foreach (var chatClient in _clients.GetAllItems())
                    {
                        try
                        {
                            chatClient.ClientProxy.OnMessageToRoom(senderClient.User.name, message);
                        }
                        catch
                        {

                        }
                    }
                });
        }

        public void test()
        {
            Console.WriteLine("Test OK");
        }

        public void OnMessageToRoom(string name, string message)
        {
            Console.WriteLine(name + ": " + message);
        }

        private sealed class ChatClient
        {
            public IScsServiceClient Client { get; private set; }
            public iClient ClientProxy { get; private set; }
            public UserData User { get; private set; }

            public ChatClient(IScsServiceClient client, iClient clientProxy, UserData userInfo)
            {
                Client = client;
                ClientProxy = clientProxy;
                User = userInfo;
            }
        }
    }
}



I've also put the source code at attachment. Been dissecting the codes for hours, could'nt find the cause of the error.

Really apreciate any helps!

Attached File(s)


This post has been edited by theref: 25 July 2013 - 03:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Client does not wait for method invocations by server

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3481
  • View blog
  • Posts: 10,733
  • Joined: 05-May 12

Re: Client does not wait for method invocations by server

Posted 25 July 2013 - 05:36 PM

You may have more luck contacting the author of the library to determine what is going on.

Early in the .NET Framework days, it was thought that .NET Remoting (on which that library seems to be based on) was going to be the ultimate solution for client-server type applications, but it's fallen to the wayside and the expertise in it is pretty narrow.
Was This Post Helpful? 0
  • +
  • -

#3 theref  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 03-June 13

Re: Client does not wait for method invocations by server

Posted 25 July 2013 - 09:39 PM

Yeah I left a message on the article still yet to hear any response from the author. There's also another guy who had same problem like me, but it's been 2 months and no response up until today :butbut:
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3481
  • View blog
  • Posts: 10,733
  • Joined: 05-May 12

Re: Client does not wait for method invocations by server

Posted 25 July 2013 - 10:16 PM

Well, I need to correct myself. Apparently that implementation doesn't use the built in .NET remoting. He actually implemented his own remoting based on the follow up article here: http://www.codeproje...RMI#ScsRMILayer

Anyway if you scan through that second article, that exact exception message text you are getting shows up in the source code that he presents. I'm pretty brain fried right now, but I think it should be relatively easy to figure out what set of conditions causes that exception to be thrown.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1