Пример #1
0
        public void FromHighLayerToHere(DataContent dataContent)
        {
            SmallFileDataObject dataObject = (SmallFileDataObject)dataContent.Data;

            // Convert an object to a byte array
            dataContent.Data = Util.ObjectByteConverter.ObjectToByteArray(dataObject);
            NextLowLayerEvent?.Invoke(dataContent);
        }
Пример #2
0
        public object Clone()
        {
            SmallFileDataObject dataObject = new SmallFileDataObject();

            dataObject.Filename = this.Filename;
            dataObject.BinData  = (byte[])this.BinData.Clone();
            return(dataObject);
        }
Пример #3
0
        static void SendSmallFileConsole(SockMgr sockMgr)
        {
            Console.WriteLine("Enter filepath to send");
            Console.Write("> ");
            string filepath = Console.ReadLine();

            Protocol.SmallFileDataObject dataObject = new Protocol.SmallFileDataObject();
            dataObject.Filename = Path.GetFileName(filepath);
            Console.WriteLine("Reading File");
            try
            {
                dataObject.BinData = File.ReadAllBytes(filepath);
            }
            catch (Exception)
            {
                Console.WriteLine("[Error] File Not Found");
                return;
            }
            Console.WriteLine("Sending File");
            sockMgr.SendSmallFile(dataObject);
            Console.WriteLine($"[File] File \"{dataObject.Filename}\" Sent");
            Console.Write("> ");
        }
Пример #4
0
        // respond to event at the top of the protocol stack
        private void OnNextHighLayerEvent(Protocol.DataContent dataContent)
        {
            switch (dataContent.Type)
            {
            case Protocol.DataProtocolType.Text:
                // print:
                // [Message] remote -> local | time
                // data
                // [MessageEnd]
                Console.WriteLine();
                Console.WriteLine(string.Format("[Message] {0} -> {1} | {2}",
                                                _sockMgr.GetSockBase().GetSocket().RemoteEndPoint.ToString(),
                                                _sockMgr.GetSockBase().GetSocket().LocalEndPoint.ToString(),
                                                DateTime.Now.ToString()));
                Console.WriteLine((string)dataContent.Data);
                Console.WriteLine(string.Format("[MessageEnd]"));
                Console.Write("> ");
                break;

            case Protocol.DataProtocolType.SmallFile:
                string dirPath = "./recvFiles";
                Console.WriteLine(string.Format("[File] {0} -> {1} | {2}",
                                                _sockMgr.GetSockBase().GetSocket().RemoteEndPoint.ToString(),
                                                _sockMgr.GetSockBase().GetSocket().LocalEndPoint.ToString(),
                                                DateTime.Now.ToString()));
                Protocol.SmallFileDataObject dataObject = (Protocol.SmallFileDataObject)dataContent.Data;
                Console.WriteLine($"Saving File \"{dataObject.Filename}\" to \"{dirPath}\" ...");
                Directory.CreateDirectory(dirPath);
                string filepath = Util.SaveFile.WriteFile(Path.Combine(dirPath, dataObject.Filename), dataObject.BinData);
                Console.WriteLine($"File \"{filepath}\" saved");
                Console.WriteLine(string.Format("[FileEnd]"));
                Console.Write("> ");
                break;

            case Protocol.DataProtocolType.Management:
                TransportState state = dataContent.TransportState;
                if (state.PendingLength == 0)
                {
                    // this branch should not be reached
                    Console.WriteLine("[Transport] Done");
                    Console.Write("> ");
                }
                else
                {
                    // don't print if less than 10 KB
                    if (state.PendingLength < 1024 * 10)
                    {
                        break;
                    }
                    double remainingSec;
                    remainingSec = (state.PendingLength - state.ReceivedLength) / 1024 / state.Speed;
                    Console.WriteLine(string.Format("[Transport] {0} -> {1} | {2}",
                                                    _sockMgr.GetSockBase().GetSocket().RemoteEndPoint.ToString(),
                                                    _sockMgr.GetSockBase().GetSocket().LocalEndPoint.ToString(),
                                                    DateTime.Now.ToString()));
                    Console.WriteLine($"[Transport] Speed {state.Speed.ToString("0.0")} KB/s | Pending {Util.FormatConverter.ByteSizeToHumanReadable(state.PendingLength)} | Received {Util.FormatConverter.ByteSizeToHumanReadable(state.ReceivedLength)} | ETA {Util.FormatConverter.SecondToHumanReadable(remainingSec)}");
                    Console.Write("> ");
                }
                break;
            }
            _sockMgr.RaiseSockMgrProtocolTopEvent(dataContent);
        }