예제 #1
0
        private void ConnectNewClientToGetDataFrom(Object clientToConnectO)
        {
            TcpClient                clientToConnect = (TcpClient)clientToConnectO;
            BinaryFormatter          binaryFormatter = new BinaryFormatter();
            SerializableSharedObject sso;

            bool clientRegistered = false;
            var  mainStream       = clientToConnect.GetStream();

            while (!clientRegistered && clientToConnect.Connected)
            {
                if (mainStream.DataAvailable)
                {
                    try
                    {
                        sso = (SerializableSharedObject)binaryFormatter.Deserialize(mainStream);
                        switch (sso.objectType)
                        {
                        case (int)SerializableObjectType.REGISTER:

                            TcpClientWithID tcpClientWithId = clientsAssociations.Where(x => x.connectionRequestId == sso.connectionRequestID).FirstOrDefault();
                            clientsAssociations.Remove(tcpClientWithId);
                            if (tcpClientWithId == null)
                            {
                                tcpClientWithId = new TcpClientWithID(sso.connectionRequestID);
                            }
                            tcpClientWithId.customerCodeToGetDataFrom = sso.customerCode;
                            tcpClientWithId.desktopCodeToGetDataFrom  = sso.desktopCode;
                            tcpClientWithId.clientToGetDataFrom       = clientToConnect;
                            tcpClientWithId.streamToGetDataFrom       = mainStream;
                            clientsAssociations.Add(tcpClientWithId);

                            if (tcpClientWithId.clientToGetDataFrom != null && tcpClientWithId.clientToSendDataTo != null)
                            {
                                ReceiveResendImage(tcpClientWithId);
                            }
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }
            }
        }
예제 #2
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);
        }