Exemplo n.º 1
0
        private void sendErrorNotConnected()
        {
            commArgs comm = new commArgs();

            comm.Info = "Not connected !";
            launchWithInvokeCheck(InfoExchange, this, comm);
        }
Exemplo n.º 2
0
        private void sendDataCallback(IAsyncResult ar)
        {
            commSocket.EndSend(ar);
            commArgs comm = new commArgs();

            comm.Info = "The object data has been sent";
            launchWithInvokeCheck(InfoExchange, this, comm);
        }
Exemplo n.º 3
0
 private void newClient_Connection(object sender, commArgs e)
 {
     //if connection is lost, dispose that sender to the garbage collector
     if (!((Client)sender).isConnected)
     {
         sender      = null;
         isConnected = false;
     }
     this.launchWithInvokeCheck(Connection, sender, e);
 }
Exemplo n.º 4
0
        private void connectCallback(IAsyncResult ar)
        {
            try { commSocket.EndConnect(ar); } catch { isConnected = false; }
            commArgs connectSuccess = new commArgs();

            isConnected         = true;
            connectSuccess.Info = "Connected to Server";
            launchWithInvokeCheck(Connection, this, connectSuccess);
            //this.ReceiveMsg();
            // this.ReceiveData();
        }
Exemplo n.º 5
0
        private void acceptConnectionCallback(IAsyncResult ar)
        {
            //quand le server accepte la connection, il reçoit le socket de communication avec le client --> Il faut garder ce Socket!
            AddClient(listenSocket.EndAccept(ar));

            //notify the new connection
            commArgs connectionInfo = new commArgs();

            connectionInfo.Info = "Connected to Client";
            isConnected         = true;
            launchWithInvokeCheck(Connection, this, connectionInfo);

            acceptConnection();
        }
Exemplo n.º 6
0
        private void receiveDataCallback(IAsyncResult ar)
        {
            try
            {
                int          DataSize     = commSocket.EndReceive(ar);
                SerialObject receivedData = (SerialObject)ar.AsyncState;
                receivedData.AddDataToBeSerialized(DataSize);

                if (commSocket.Available > 0)
                {
                    commSocket.BeginReceive(receivedData.Serialized, 0, receivedData.Serialized.Length, SocketFlags.None, receiveDataCallback, receivedData);
                }
                else
                {
                    commArgs comm = new commArgs();
                    comm.Info      = "Object Received";
                    comm.Reception = true;
                    receivedData.Deserialize();
                    comm.ObjectData = receivedData.Deserialized;
                    launchWithInvokeCheck(InfoExchange, this, comm);
                    ReceiveData();
                }
            }
            catch (System.Net.Sockets.SocketException e)
            {
                Console.WriteLine(e);
                commArgs connexionLost = new commArgs();
                connexionLost.Info = "Connexion Lost with the ";
                if (isServer)
                {
                    connexionLost.Info += "Client.";
                    isConnected         = false;
                }
                else
                {
                    connexionLost.Info += "Server.";
                    isConnected         = false;
                }
                launchWithInvokeCheck(Connection, this, connexionLost);
            }
        }
Exemplo n.º 7
0
 //check method
 private void launchWithInvokeCheck(EventHandler <commArgs> ASyncMethod, object sender, commArgs e)
 {
     if (ASyncMethod == null)
     {
         Console.WriteLine("Delegate sync method launch by " + sender + " has not been defined");
     }
     else
     {
         if (ASyncMethod.Target is Control)
         {
             if (((Control)ASyncMethod.Target).InvokeRequired)
             {
                 ((Control)ASyncMethod.Target).Invoke(ASyncMethod, sender, e);
             }
             else
             {
                 ASyncMethod(sender, e);
             }
         }
     }
 }
Exemplo n.º 8
0
 private void newClient_InfoExchange(object sender, commArgs e)
 {
     this.launchWithInvokeCheck(InfoExchange, sender, e);
 }
Exemplo n.º 9
0
 //Making sure everything isn't messed up with asynchrone stuff
 private void launchWithInvokeCheck(EventHandler <commArgs> syncMethod, object sender, commArgs e)
 {
     if (syncMethod == null)
     {
         Console.WriteLine("Delegate sync method launch by " + sender + " has not been defined");
     }
     else
     {
         if (syncMethod.Target is Control)
         {
             if (((Control)syncMethod.Target).InvokeRequired)
             {
                 ((Control)syncMethod.Target).Invoke(syncMethod, sender, e);
             }
             else
             {
                 syncMethod(sender, e);
             }
         }
         else
         {
             Console.WriteLine("!! " + syncMethod.Target.GetType() + "is NOT Control (nor SERVER)");
         }
     }
 }