private void SendDesktopImage(NetworkStream clientStream)
        {
            if (client.Connected && readyToSend)
            {
                //using (var mainStream = client.GetStream())
                //{
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                while (client.Connected)
                {
                    SerializableSharedObject sso = new SerializableSharedObject(connectionIdTest)
                    {
                        timeStamp    = DateTime.Now,
                        customerCode = "TEST",
                        desktopCode  = "DESK",
                        objectType   = (int)SerializableObjectType.SCREEN,
                        screen       = GrabDesktop()
                    };
                    byte[] ba          = ObjectToByteArray(sso);
                    byte[] userDataLen = BitConverter.GetBytes((Int32)ba.Length);
                    clientStream.Write(userDataLen, 0, 4);

                    bool lengthSent = false;
                    while (!lengthSent)
                    {
                        if (clientStream.DataAvailable)
                        {
                            byte[] sendResult = new byte[1];
                            if (clientStream.Read(sendResult, 0, 1) > 0)
                            {
                                if (sendResult[0] == (byte)1)
                                {
                                    lengthSent = true;
                                }
                            }
                        }
                    }

                    clientStream.Write(ba, 0, ba.Length);

                    bool contentSent = false;
                    while (!contentSent)
                    {
                        if (clientStream.DataAvailable)
                        {
                            byte[] sendResult = new byte[1];
                            if (clientStream.Read(sendResult, 0, 1) > 0)
                            {
                                if (sendResult[0] == (byte)1)
                                {
                                    contentSent = true;
                                }
                            }
                        }
                    }
                    //binaryFormatter.Serialize(mainStream, sso);
                    //}
                }
            }
        }
        private void registerClient(TcpClient client, NetworkStream mainStream)
        {
            BinaryFormatter          binaryFormatter = new BinaryFormatter();
            SerializableSharedObject sso             = new SerializableSharedObject(connectionIdTest)
            {
                timeStamp    = DateTime.Now,
                customerCode = "ADMIN",
                desktopCode  = "ADMIN_DESK",
                objectType   = (int)SerializableObjectType.REGISTER
            };

            binaryFormatter.Serialize(mainStream, sso);
        }
        private SerializableSharedObject ByteArrayToSerializableSharedObject(byte[] arrBytes)
        {
            SerializableSharedObject obj = null;

            using (MemoryStream memStream = new MemoryStream())
            {
                BinaryFormatter binForm = new BinaryFormatter();
                memStream.Write(arrBytes, 0, arrBytes.Length);
                memStream.Seek(0, SeekOrigin.Begin);
                obj = (SerializableSharedObject)binForm.Deserialize(memStream);
            }
            return(obj);
        }
        private void registerClient(TcpClient client)
        {
            BinaryFormatter          binaryFormatter = new BinaryFormatter();
            var                      mainStream      = client.GetStream();
            SerializableSharedObject sso             = new SerializableSharedObject(connectionIdTest)
            {
                timeStamp    = DateTime.Now,
                customerCode = "TEST",
                desktopCode  = "DESK",
                objectType   = (int)SerializableObjectType.REGISTER,
                screen       = null
            };

            binaryFormatter.Serialize(mainStream, sso);
        }
Ejemplo n.º 5
0
        private void ReceiveResendImage(TcpClientWithID clientAssociation)
        {
            if (clientAssociation != null)
            {
                clientAssociation.isListening = true;
            }
            else
            {
                return;
            }

            BinaryFormatter          binaryFormatter = new BinaryFormatter();
            SerializableSharedObject sso             = new SerializableSharedObject(1);

            if (clientAssociation.clientToSendDataTo.Connected && clientAssociation.clientToGetDataFrom.Connected)
            {
                // avviso chi invia i dati che siamo pronti per ricevere.
                clientAssociation.streamToGetDataFrom.Write(new byte[1] {
                    (byte)1
                }, 0, 1);

                while (clientAssociation.clientToSendDataTo.Connected && clientAssociation.clientToGetDataFrom.Connected)
                {
                    byte[] readMsgLen = new byte[4];
                    while (!clientAssociation.streamToGetDataFrom.DataAvailable)
                    {
                        // aspetto...
                    }
                    clientAssociation.streamToGetDataFrom.Read(readMsgLen, 0, 4);
                    int dataLen = BitConverter.ToInt32(readMsgLen, 0);

                    byte[] readMsgData = new byte[dataLen];

                    //invio al client un feedback di messaggio ricevuto.
                    clientAssociation.streamToGetDataFrom.Write(new byte[1] {
                        (byte)1
                    }, 0, 1);

                    while (!clientAssociation.streamToGetDataFrom.DataAvailable)
                    {
                        // aspetto...
                    }

                    clientAssociation.streamToGetDataFrom.Read(readMsgData, 0, dataLen);

                    //if (clientAssociation.streamToGetDataFrom.DataAvailable)
                    //{
                    //byte[] readMsgLen = new byte[4];
                    //clientAssociation.streamToGetDataFrom.Read(readMsgLen, 0, 4);

                    ////invio al client un feedback di messaggio ricevuto.
                    //clientAssociation.streamToGetDataFrom.Write(new byte[1] { (byte)1 }, 0, 1);

                    //int dataLen = BitConverter.ToInt32(readMsgLen, 0);
                    //byte[] readMsgData = new byte[dataLen];
                    //clientAssociation.streamToGetDataFrom.Read(readMsgData, 0, dataLen);


                    try
                    {
                        sso = ByteArrayToSerializableSharedObject <SerializableSharedObject>(readMsgData);
                        switch (sso.objectType)
                        {
                        case (int)SerializableObjectType.SCREEN:
                            //byte[] ba = ObjectToByteArray(sso);
                            //byte[] userDataLen = BitConverter.GetBytes((Int32)ba.Length);
                            //clientAssociation.streamToSendDataTo.Write(userDataLen, 0, 4);
                            //clientAssociation.streamToSendDataTo.Write(ba, 0, ba.Length);

                            ////rimango in attesa che il client mi dica di aver ricevuto la foto.
                            //bool messageSent = false;
                            //while (!messageSent)
                            //{
                            //    if (clientAssociation.streamToSendDataTo.DataAvailable)
                            //    {
                            //        byte[] sendResult = new byte[1];
                            //        if (clientAssociation.streamToSendDataTo.Read(sendResult, 0, 1) > 0)
                            //        {
                            //            if (sendResult[0] == (byte)1)
                            //                messageSent = true;
                            //        }
                            //    }
                            //}

                            break;

                        default:
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                    //invio al client un feedback di messaggio ricevuto.
                    clientAssociation.streamToGetDataFrom.Write(new byte[1] {
                        (byte)1
                    }, 0, 1);
                }
            }
            clientAssociation.isListening = false;
            clientAssociation.streamToSendDataTo.Dispose();
            clientAssociation.streamToGetDataFrom.Dispose();
            clientsAssociations.Remove(clientAssociation);
        }