This class represents a connection to a remote host. It is possible to notify this host with messages that extends upon the "AbstractMessage" class. HOW TO USE: * Initialize the class with needed parameters (see constructor) * Call "Start()" method to start listening for incoming messages and start to write messages * Whenever a message is received from the server, the OnMessageReceived callback is called. * Whenever a message is received but in a corrupted format, the OnMessageFailed is called. * If the connection to the server fails, the OnConnectionFailed is called. The underlying connection will automatically be closed, so you do not need to call the stop method if this delegate is fired. * Writing to the server is non-blocking. All it does is to place the message into a buffer that is then handled by another thread.
 public RespondToInitRequestMessage(Dispatcher disp, TaskHandler handler, ClientConnectionList list, ClientConnection client, InitRequestMessage msg)
     : base(disp, handler)
 {
     ClientConnectionList = list;
     ClientConnection = client;
     InitRequestMessage = msg;
 }
 public RemoveClientFromUserListTask(Dispatcher disp, TaskHandler handler, ClientConnectionList list, ClientConnection client, ObservableCollection<UserLocation> locs)
     : base(disp, handler)
 {
     ClientConnectionList = list;
     ClientConnection = client;
     UserLocations = locs;
 }
 public RespondToGeoPointMessageTask(Dispatcher disp, TaskHandler handler, ClientConnectionList list, ClientConnection client, ObservableCollection<UserLocation> locs, GeoPointMessage msg)
     : base(disp, handler)
 {
     ClientConnectionList = list;
     ClientConnection = client;
     GeoPointMessage = msg;
     UserLocations = locs;
 }
Beispiel #4
0
        public void OnConnectionFailed(Object sender, ClientConnection.ConnectionFailedEventArgs args)
        {
            RemoveClientFromUserListTask task = new RemoveClientFromUserListTask(
                Dispatcher,
                TaskHandler.DispatcherAsync,
                UserList,
                sender as ClientConnection,
                UserLocations);

            task.RunWorkerCompleted += TaskCompleted;
            task.BeginExecute();
        }
 /// <summary>
 /// Constructor for MessageWriter.
 /// </summary>
 /// <param name="inner">This is the ClientConnection that this class is inside.</param>
 /// <param name="connection">This is the TCPConnection that the ClientConnection is connected to.</param>
 /// <param name="MessageFailed">A delegate representing the method to be called whenever this MessageWriter
 /// fails to write a message into it's underlying TCPConnection.</param>
 public MessageWriter(ClientConnection inner, TCPConnection connection)
 {
     this.inner = inner;
     this.connection = connection;
 }
 public AddClientToUserListTask(Dispatcher disp, TaskHandler handler, ClientConnectionList list, ClientConnection client)
     : base(disp, handler)
 {
     ClientConnectionList = list;
     ClientConnection = client;
 }
        /// <summary>
        /// Method listening for incoming connections.
        /// </summary>
        private void Go()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        TCPConnection con = listener.waitForConnection();
                        ClientConnection client = new ClientConnection(con);
                        client.MessageFailed += MessageFailed;
                        client.MessageReceived += MessageReceived;
                        client.ConnectionFailed += ConnectionFailed;

                        if (InitialConnectionSuccess != null)
                        {
                            InitialConnectionSuccessEventArgs args = new InitialConnectionSuccessEventArgs();
                            args.Client = client;
                            InitialConnectionSuccess(this, args);
                        }
                    }
                    catch (SocketException e)
                    {
                        if (InitialConnectionFailed != null)
                        {
                            InitialConnectionFailedEventArgs args = new InitialConnectionFailedEventArgs();
                            args.Cause = e;
                            InitialConnectionFailed(this, args);
                        }
                    }
                    catch (InvalidOperationException e)
                    {
                        if (InitialConnectionFailed != null)
                        {
                            InitialConnectionFailedEventArgs args = new InitialConnectionFailedEventArgs();
                            args.Cause = e;
                            InitialConnectionFailed(this, args);
                        }
                    }
                }
            }
            catch (ThreadAbortException)
            {
                // do something ?
            }
        }
Beispiel #8
0
        public void OnMessageReceived(Object sender, ClientConnection.MessageReceivedEventArgs args)
        {
            ClientConnection client = sender as ClientConnection;
            if (args.Message is InitRequestMessage)
            {
                RespondToInitRequestMessage task = new RespondToInitRequestMessage(
                    Dispatcher,
                    TaskHandler.DispatcherAsync,
                    UserList,
                    client,
                    args.Message as InitRequestMessage);

                task.RunWorkerCompleted += TaskCompleted;
                task.BeginExecute();
            }
            else if (args.Message is GeoPointMessage)
            {
                RespondToGeoPointMessageTask task = new RespondToGeoPointMessageTask(
                    Dispatcher,
                    TaskHandler.DispatcherAsync,
                    UserList,
                    client,
                    UserLocations,
                    args.Message as GeoPointMessage);

                task.RunWorkerCompleted += TaskCompleted;
                task.BeginExecute();
            }
            else if (args.Message is MultipleGeoPointRequestMessage)
            {
                MultipleGeoPointResponseMessage msg = null;
                lock (UserLocations)
                {
                    msg = new MultipleGeoPointResponseMessage(UserLocations.ToList());
                }
                client.Notify(msg);
            }
        }
Beispiel #9
0
 public void OnMessageFailed(Object sender, ClientConnection.MessageFailedEventArgs args)
 {
 }