Пример #1
0
        private async Task OnTransferAsync(TcpClient tcpClient, StateObject tcpState)
        {
            await Task.Yield();

            using (var networkStream = tcpClient.GetStream())
            {
                var buffer = new byte[8192];

                // Receiving and deserialized the serializable object
                var byteCount = await networkStream.ReadAsync(buffer, 0, buffer.Length);

                if (byteCount > 0)
                {
                    ServerSb.Append(Encoding.UTF8.GetString(buffer, 0, byteCount));
                    var StringMsg = ServerSb.ToString();
                    if (StringMsg.IndexOf("</TcpMsg>") > -1)
                    {
                        var xmlSerializer     = new XmlSerializer(typeof(ServerClientMsg));
                        var ReceivedMsgObject = new ServerClientMsg();
                        using (var textReader = new StringReader(StringMsg))
                        {
                            ReceivedMsgObject = (ServerClientMsg)xmlSerializer.Deserialize(textReader);
                        }

                        // Clear string builder since we are done with it
                        ServerSb.Clear();

                        var MsgObjectToReturn = new ServerClientMsg();

                        // If this thread is for the server
                        if (tcpState.ClientType == false)
                        {
                            MsgObjectToReturn = ServerCreateRly(ReceivedMsgObject);
                        }
                        // If this thread if for the client
                        else
                        {
                            // Now that we have the serializable object, we need to switch through all the commands
                            switch (ReceivedMsgObject.Command)
                            {
                            // Data request command
                            case (int)ServerClientMsg.Commands.DataRq:

                                byte[] file;
                                string hash = string.Empty;
                                //Console.WriteLine("Client Server: received data request for file: {0} segment: {1}", ReceivedMsgObject.NameOfFile,ReceivedMsgObject.SegmentOfFile);

                                string filepath = tcpState.TempFolderPath + @"/" + Path.GetFileNameWithoutExtension(ReceivedMsgObject.NameOfFile);
                                filepath += "_temp" + ReceivedMsgObject.SegmentOfFile + Path.GetExtension(ReceivedMsgObject.NameOfFile);
                                //Console.WriteLine("Client Server: Retreiving file: {0}", filepath);

                                // See if the client's server actually has the file or not
                                if (File.Exists(filepath))
                                {
                                    file = File.ReadAllBytes(filepath);
                                }
                                else
                                {
                                    //Console.WriteLine("Client Server could not find the file {0}", filepath);
                                    file = new byte[2];
                                }

                                // Retrieveing the hash to send back with the data segment
                                foreach (var pair in tcpState.FileDict)
                                {
                                    if (pair.Key == ReceivedMsgObject.NameOfFile)
                                    {
                                        ObjectForFiledict obj = pair.Value;
                                        hash = obj.Hash;
                                        //Console.WriteLine("Client Server: Hash = {0}", hash);
                                        break;
                                    }
                                }

                                MsgObjectToReturn.DataRly(hash, ReceivedMsgObject.SegmentOfFile, file);

                                break;

                            default:
                                Console.WriteLine("Invalid command to the client server");
                                break;
                            }
                        }

                        // At this point, the serializable object should be popululated with the proper command and info
                        StringBuilder SerializedSb = new StringBuilder();

                        using (var stringWriter = new StringWriter())
                        {
                            xmlSerializer.Serialize(stringWriter, MsgObjectToReturn);
                            SerializedSb.Append(stringWriter.ToString());
                        }

                        var serializedString = SerializedSb.ToString();
                        var MsgToSend        = Encoding.UTF8.GetBytes(serializedString);

                        // Sending the serializable response back
                        await networkStream.WriteAsync(MsgToSend, 0, MsgToSend.Length);

                        //Console.WriteLine("[Server] Response has been given");
                    }
                }
            }
        }
Пример #2
0
        private async Task OnTransferAsync(TcpClient tcpClient, StateObject tcpState)
        {
            await Task.Yield();

            using (var networkStream = tcpClient.GetStream())
            {
                var buffer = new byte[8192];


                var byteCount = await networkStream.ReadAsync(buffer, 0, buffer.Length);
                if (byteCount > 0)
                {
                    ServerSb.Append(Encoding.UTF8.GetString(buffer, 0, byteCount));
                    var StringMsg = ServerSb.ToString();
                    if (StringMsg.IndexOf("</TcpMsg>") > -1)
                    {
                        var xmlSerializer = new XmlSerializer(typeof(ServerClientMsg));
                        var ReceivedMsgObject = new ServerClientMsg();
                        using (var textReader = new StringReader(StringMsg))
                        {
                            ReceivedMsgObject = (ServerClientMsg)xmlSerializer.Deserialize(textReader);
                        }

                        // Clear string builder since we are done with it 
                        ServerSb.Clear();

                        var MsgObjectToReturn = new ServerClientMsg();

                        // If this thread if for the server
                        if (tcpState.ClientType == false)
                        {
                            MsgObjectToReturn = ServerCreateRly(ReceivedMsgObject);
                        }
                        // If this thread if for the client 
                        else
                        {
                            Console.WriteLine("client Server received {0} command", ReceivedMsgObject.Command);
                            switch (ReceivedMsgObject.Command)
                            {
                                case (int)ServerClientMsg.Commands.DataRq:

                                    string file = string.Empty;
                                    string hash = string.Empty;

                                    Console.WriteLine("ClientServer received the data request");

                                    foreach (var pair in tcpState.FileDict)
                                    {
                                        if (pair.Key == ReceivedMsgObject.NameOfFile)
                                        {
                                            ObjectForFiledict resultObj = pair.Value;
                                            file = resultObj.Segments[ReceivedMsgObject.SegmentOfFile];
                                            hash = resultObj.Hash;

                                            if (!file.Any() || String.IsNullOrEmpty(hash))
                                            {
                                                Console.WriteLine("string or hash is empty");
                                            }
                                            
                                        }
                                    }
                                    Console.WriteLine("ClientServer is sending string: {0} \nWith a hash of: {1}", file, hash);
                                    MsgObjectToReturn.DataRly(hash, ReceivedMsgObject.SegmentOfFile, file);

                                    break;
                                default:
                                    Console.WriteLine("Invalid command to the client server");
                                    break;
                            }
                        }


                        StringBuilder SerializedSb = new StringBuilder();

                        using (var stringWriter = new StringWriter())
                        {
                            xmlSerializer.Serialize(stringWriter, MsgObjectToReturn);
                            SerializedSb.Append(stringWriter.ToString());
                        }

                        var serializedString = SerializedSb.ToString();
                        var MsgToSend = Encoding.UTF8.GetBytes(serializedString);
                        await networkStream.WriteAsync(MsgToSend, 0, MsgToSend.Length);

                            //Console.WriteLine("[Server] Response has been given");
                        }
                    }
                }
            }
Пример #3
0
        private async Task OnTransferAsync(TcpClient tcpClient, StateObject tcpState)
        {
            await Task.Yield();

            using (var networkStream = tcpClient.GetStream())
            {
                var buffer = new byte[8192];


                var byteCount = await networkStream.ReadAsync(buffer, 0, buffer.Length);

                if (byteCount > 0)
                {
                    ServerSb.Append(Encoding.UTF8.GetString(buffer, 0, byteCount));
                    var StringMsg = ServerSb.ToString();
                    if (StringMsg.IndexOf("</TcpMsg>") > -1)
                    {
                        var xmlSerializer     = new XmlSerializer(typeof(ServerClientMsg));
                        var ReceivedMsgObject = new ServerClientMsg();
                        using (var textReader = new StringReader(StringMsg))
                        {
                            ReceivedMsgObject = (ServerClientMsg)xmlSerializer.Deserialize(textReader);
                        }

                        // Clear string builder since we are done with it
                        ServerSb.Clear();

                        var MsgObjectToReturn = new ServerClientMsg();

                        // If this thread if for the server
                        if (tcpState.ClientType == false)
                        {
                            MsgObjectToReturn = ServerCreateRly(ReceivedMsgObject);
                        }
                        // If this thread if for the client
                        else
                        {
                            Console.WriteLine("client Server received {0} command", ReceivedMsgObject.Command);
                            switch (ReceivedMsgObject.Command)
                            {
                            case (int)ServerClientMsg.Commands.DataRq:

                                string file = string.Empty;
                                string hash = string.Empty;

                                Console.WriteLine("ClientServer received the data request");

                                foreach (var pair in tcpState.FileDict)
                                {
                                    if (pair.Key == ReceivedMsgObject.NameOfFile)
                                    {
                                        ObjectForFiledict resultObj = pair.Value;
                                        file = resultObj.Segments[ReceivedMsgObject.SegmentOfFile];
                                        hash = resultObj.Hash;

                                        if (!file.Any() || String.IsNullOrEmpty(hash))
                                        {
                                            Console.WriteLine("string or hash is empty");
                                        }
                                    }
                                }
                                Console.WriteLine("ClientServer is sending string: {0} \nWith a hash of: {1}", file, hash);
                                MsgObjectToReturn.DataRly(hash, ReceivedMsgObject.SegmentOfFile, file);

                                break;

                            default:
                                Console.WriteLine("Invalid command to the client server");
                                break;
                            }
                        }


                        StringBuilder SerializedSb = new StringBuilder();

                        using (var stringWriter = new StringWriter())
                        {
                            xmlSerializer.Serialize(stringWriter, MsgObjectToReturn);
                            SerializedSb.Append(stringWriter.ToString());
                        }

                        var serializedString = SerializedSb.ToString();
                        var MsgToSend        = Encoding.UTF8.GetBytes(serializedString);
                        await networkStream.WriteAsync(MsgToSend, 0, MsgToSend.Length);

                        //Console.WriteLine("[Server] Response has been given");
                    }
                }
            }
        }
Пример #4
0
        private async Task OnTransferAsync(TcpClient tcpClient, StateObject tcpState)
        {
            await Task.Yield();

            using (var networkStream = tcpClient.GetStream())
            {
                var buffer = new byte[8192];

                // Receiving and deserialized the serializable object
                var byteCount = await networkStream.ReadAsync(buffer, 0, buffer.Length);
                if (byteCount > 0)
                {
                    ServerSb.Append(Encoding.UTF8.GetString(buffer, 0, byteCount));
                    var StringMsg = ServerSb.ToString();
                    if (StringMsg.IndexOf("</TcpMsg>") > -1)
                    {
                        var xmlSerializer = new XmlSerializer(typeof(ServerClientMsg));
                        var ReceivedMsgObject = new ServerClientMsg();
                        using (var textReader = new StringReader(StringMsg))
                        {
                            ReceivedMsgObject = (ServerClientMsg)xmlSerializer.Deserialize(textReader);
                        }

                        // Clear string builder since we are done with it 
                        ServerSb.Clear();

                        var MsgObjectToReturn = new ServerClientMsg();

                        // If this thread is for the server
                        if (tcpState.ClientType == false)
                        {
                            MsgObjectToReturn = ServerCreateRly(ReceivedMsgObject);
                        }
                        // If this thread if for the client 
                        else
                        {
                            
                            // Now that we have the serializable object, we need to switch through all the commands
                            switch (ReceivedMsgObject.Command)
                            {
                                // Data request command
                                case (int)ServerClientMsg.Commands.DataRq:

                                    byte[] file;
                                    string hash = string.Empty;
                                    //Console.WriteLine("Client Server: received data request for file: {0} segment: {1}", ReceivedMsgObject.NameOfFile,ReceivedMsgObject.SegmentOfFile);

                                    string filepath = tcpState.TempFolderPath + @"/" + Path.GetFileNameWithoutExtension(ReceivedMsgObject.NameOfFile);
                                    filepath += "_temp" + ReceivedMsgObject.SegmentOfFile + Path.GetExtension(ReceivedMsgObject.NameOfFile);
                                    //Console.WriteLine("Client Server: Retreiving file: {0}", filepath);

                                    // See if the client's server actually has the file or not
                                    if (File.Exists(filepath))
                                    {
                                        file = File.ReadAllBytes(filepath);
                                    }
                                    else
                                    {
                                        //Console.WriteLine("Client Server could not find the file {0}", filepath);
                                        file = new byte[2];
                                    }

                                    // Retrieveing the hash to send back with the data segment
                                    foreach (var pair in tcpState.FileDict)
                                    {
                                        if(pair.Key == ReceivedMsgObject.NameOfFile)
                                        {
                                            ObjectForFiledict obj = pair.Value;
                                            hash = obj.Hash;
                                            //Console.WriteLine("Client Server: Hash = {0}", hash);
                                            break;
                                        }
                                    }
                                    
                                    MsgObjectToReturn.DataRly(hash, ReceivedMsgObject.SegmentOfFile, file);

                                    break;
                                default:
                                    Console.WriteLine("Invalid command to the client server");
                                    break;
                            }
                        }

                        // At this point, the serializable object should be popululated with the proper command and info
                        StringBuilder SerializedSb = new StringBuilder();

                        using (var stringWriter = new StringWriter())
                        {
                            xmlSerializer.Serialize(stringWriter, MsgObjectToReturn);
                            SerializedSb.Append(stringWriter.ToString());
                        }

                        var serializedString = SerializedSb.ToString();
                        var MsgToSend = Encoding.UTF8.GetBytes(serializedString);

                        // Sending the serializable response back
                        await networkStream.WriteAsync(MsgToSend, 0, MsgToSend.Length);

                            //Console.WriteLine("[Server] Response has been given");
                        }
                    }
                }
            }