コード例 #1
0
        private void DdelHandle(Socket MessageSocket, ServerControl Control, byte[] Message, int MessageCount, IPAddress Address)
        {
            string path  = ByteEncoding(Message, MessageCount);
            string tpath = path;

            path = Config.filepath + @"\" + Dict[Address].filepath + @"\" + path;
            if (tpath.Replace(".", String.Empty) == "")
            {
                ErrorMessage(MessageSocket, "Directory does not Exist.");
            }
            else
            {
                try {
                    if (!Directory.Exists(path))
                    {
                        ErrorMessage(MessageSocket, "Directory does not Exist.");
                    }
                    else
                    {
                        try {
                            Directory.Delete(path);
                            TextMessage(MessageSocket, ServerControl.TEXT, "Directory Deleted.");
                        }
                        catch {
                            ErrorMessage(MessageSocket, "Can't delete <directory>. Please make sure there is no file under this directory.");
                        }
                    }
                }
                catch (Exception e) {
                    CloFunc.Errorinfo("UnExpected DDEL Fail:\n" + e.ToString());
                }
            }
        }
コード例 #2
0
        private void FdelHandle(Socket MessageSocket, ServerControl Control, byte[] Message, int MessageCount, IPAddress Address)
        {
            string path  = ByteEncoding(Message, MessageCount);
            string tpath = path;

            path = Config.filepath + @"\" + Dict[Address].filepath + @"\" + path;
            if (tpath.Split('\\').ToArray().Length > 1)
            {
                ErrorMessage(MessageSocket, "Do not contain directory in <file>.");
            }
            else
            {
                try {
                    if (!File.Exists(path))
                    {
                        ErrorMessage(MessageSocket, "File does not Exist.");
                    }
                    else
                    {
                        try {
                            File.Delete(path);
                            TextMessage(MessageSocket, ServerControl.TEXT, "File Deleted.");
                        }
                        catch {
                            ErrorMessage(MessageSocket, "Can't delete <File>. Please make sure it's not occupied.");
                        }
                    }
                }
                catch (Exception e) {
                    CloFunc.Errorinfo("UnExpected FDEL Fail:\n" + e.ToString());
                }
            }
        }
コード例 #3
0
        private void CdirHandle(Socket MessageSocket, ServerControl Control, byte[] Message, int MessageCount, IPAddress Address)
        {
            string path  = ByteEncoding(Message, MessageCount);
            string tpath = path;

            path = Config.filepath + @"\" + Dict[Address].filepath + @"\" + path;
            if (tpath.Replace(".", String.Empty) == "")
            {
                ErrorMessage(MessageSocket, "Illegal <directory>");
            }
            else
            {
                try {
                    if (Directory.Exists(path))
                    {
                        ErrorMessage(MessageSocket, "Directory Alread Exists.");
                    }
                    else
                    {
                        Directory.CreateDirectory(path);
                        TextMessage(MessageSocket, ServerControl.TEXT, "Directory Created.");
                    }
                }
                catch (Exception e) {
                    CloFunc.Errorinfo("UnExpected CDIR Fail: " + e.ToString());
                }
            }
        }
コード例 #4
0
 // Update is called once per frame
 void Update()
 {
     if (isShow)
     {
         text.text = CloFunc.Round(ping).ToString();
     }
 }
コード例 #5
0
 private void RpasHandle(Socket MessageSocket, ServerControl Control, byte[] Message, int MessageCount, IPAddress Address)
 {
     try {
         string       ThisMessage = ByteEncoding(Message, MessageCount);
         StreamReader logincp     = new StreamReader(Config.userpath);//login check process
         int          tempindex   = 0;
         string       stemp       = logincp.ReadLine();
         string[]     temp        = stemp.Split(' ');//split string with whitespace.
         stemp = logincp.ReadLine();
         while (stemp != null)
         {
             ++tempindex;
             temp = stemp.Split(' ');
             if (temp[0] == Dict[Address].username)
             {
                 break;
             }
             stemp = logincp.ReadLine();
         }
         logincp.Close();
         string[] Alllines = File.ReadAllLines(Config.userpath);
         Alllines[tempindex] = Dict[Address].username + ' ' + ThisMessage;
         File.WriteAllLines(Config.userpath, Alllines);
         TextMessage(MessageSocket, ServerControl.TEXT, "ResetPassword Success.");
     }
     catch {
         //
         CloFunc.Errorinfo("Unexpected RPAS Fail.");
     }
 }
コード例 #6
0
        private void DlflHandle(Socket MessageSocket, ServerControl Control, byte[] Message, int MessageCount, IPAddress Address)
        {
            string path  = ByteEncoding(Message, MessageCount);
            string tpath = path;

            path = Config.filepath + @"\" + Dict[Address].filepath + @"\" + path;
            if (tpath.Split('\\').ToArray().Length > 1)
            {
                ErrorMessage(MessageSocket, "Do not contain directory in <file>.");
            }
            else
            {
                try {
                    if (!File.Exists(path))
                    {
                        ErrorMessage(MessageSocket, "File does not Exist.");
                    }
                    else
                    {
                        try {
                            TextMessage(MessageSocket, ServerControl.DLFL, tpath);
                            Dict[Address].ThreadState.Download = true;
                        }
                        catch {
                            ErrorMessage(MessageSocket, "Unexpected Error.");
                        }
                    }
                }
                catch (Exception e) {
                    CloFunc.Errorinfo("UnExpected DLFL Fail:\n" + e.ToString());
                }
            }
        }
コード例 #7
0
 // void OnGUI () {
 //  if(isShow) GUI.TextArea(PingRect,((int)ping).ToString(),0,GUIStyle.none);
 // }
 private void SendPingMessage(object state)
 {
     if (NetWorkScript.isConnected && isShow)
     {
         Timetemp = CloFunc.GetTimeNow();
         ClQueue.ReadyForSend(EnCoder.PingMessage());
     }
 }
コード例 #8
0
        private void FsttHandle(Socket MessageSocket, ServerControl Control, byte[] Message, int MessageCount, IPAddress Address)
        {
            string path  = ByteEncoding(Message, MessageCount);
            string tpath = path;

            path = Config.filepath + @"\" + Dict[Address].filepath + @"\" + path;
            if (tpath.Split('\\').ToArray().Length > 1)
            {
                ErrorMessage(MessageSocket, "Do not contain directory in <file>.");
            }
            else
            {
                try {
                    if (!File.Exists(path))
                    {
                        ErrorMessage(MessageSocket, "File does not Exist.");
                    }
                    else
                    {
                        try {
                            FileStream fp          = new FileStream(path, FileMode.Open, FileAccess.Read);//, FileShare.Read);
                            byte[]     messagebuff = new byte[2047];
                            TextMessage(MessageSocket, ServerControl.FSTT, fp.Length.ToString());
                            long   count        = (fp.Length + 2046) / 2047;
                            byte[] finalmessage = new byte[(int)(fp.Length % 2047)];
                            for (long i = 0; i < count; ++i)
                            {
                                if (i + 1 != count)
                                {
                                    fp.Read(messagebuff, 0, 2047);
                                    ByteMessage(MessageSocket, ServerControl.FILE, messagebuff);
                                }
                                else
                                {
                                    fp.Read(finalmessage, 0, finalmessage.Length);
                                    ByteMessage(MessageSocket, ServerControl.FEND, finalmessage);
                                }
                            }
                            /////////ByteMessage & TextMessage may be cat by Socket or FTP///////
                            /////////cause an unrepairable bug
                            /////////so add an [meaningless] operation here to avoid bug
                            log(Address + " < " + path + " Download Over.");
                            ////////////////////////////////////////
                            //TextMessage(MessageSocket, ServerControl.FEND, "DL END");
                            Dict[Address].ThreadState.Download = false;
                            fp.Close();
                        }
                        catch {
                            ErrorMessage(MessageSocket, "Unexpected Error.");
                        }
                    }
                }
                catch (Exception e) {
                    CloFunc.Errorinfo("UnExpected FSTT Fail:\n" + e.ToString());
                }
            }
        }
コード例 #9
0
 private void FileHandle(Socket MessageSocket, ServerControl Control, byte[] Message, int MessageCount, IPAddress Address)
 {
     try {
         Dict[Address].fp.Write(Message, 1, MessageCount);
     }
     catch {
         CloFunc.Errorinfo("Unexpected FILE Fail.");
     }
 }
コード例 #10
0
        private void checkuserpath()
        {
            //these code is based on function=>checklogpath()
            //check if userlog file exists.
            if (!File.Exists(Config.userpath))
            {
                //if userlog file does not exist, create one.
                //!!! Exists bool may crash.
                Console.WriteLine("Userlog Unexists.");
                string[] pathtemp = Config.userpath.Split('\\');
                string   ptemp    = "";
                for (int i = 0; i < pathtemp.Length - 1; i++)
                {
                    ptemp = ptemp + pathtemp[i];
                }
                if (!File.Exists(ptemp))
                {
                    try
                    {
                        Directory.CreateDirectory(ptemp);
                    }
                    catch
                    {
                        CloFunc.Errorinfo("Path Create Crash.");
                    }
                }
                try
                {
                    File.Create(Config.userpath).Close();
                    Console.WriteLine("Userlog path created.");
                }
                catch
                {
                    CloFunc.Errorinfo("Userlog File Create Crash.");
                }

                //int Root user
                try {
                    StreamWriter fp = new StreamWriter(Config.userpath);
                    fp.WriteLine("#Username #password");
                    fp.WriteLine("Root " + "root".GetHashCode());
                    fp.Close();
                }
                catch {
                    CloFunc.Errorinfo("Userlog int fail.");
                }
            }
            else
            {
                //log
                Console.WriteLine("UserLog Exists...OK.");
            }
        }
コード例 #11
0
 private void FendHandle(Socket MessageSocket, ServerControl Control, byte[] Message, int MessageCount, IPAddress Address)
 {
     try {
         Dict[Address].fp.Write(Message, 1, MessageCount);
         log(Address + " > " + Dict[Address].fp.Name + " Upload Over.");
         Dict[Address].fp.Close();
         Dict[Address].ThreadState.Upload = false;
     }
     catch {
         CloFunc.Errorinfo("Unexpected FILE Fail.");
     }
 }
コード例 #12
0
 private void DisconnectProcess()
 {
     if (!state.Connected)
     {
         CloFunc.Errorinfo("YOU SHOULD CONNECT TO SERVER FIRST.");
     }
     else
     {
         HandSocket.Close();
         HandSocket      = NewSocket();
         state.Connected = false;
         state.login     = false;
         state.user      = false;
         state.pass      = false;
     }
 }
コード例 #13
0
 private void ByteHandle(Socket MessageSocket, ServerControl Control, byte[] Message, int MessageCount, IPAddress Address)
 {
     try {
         if (Dict[Address].ThreadState.transmission)
         {
             Dict[Address].ByteBuff = CloFunc.Bytecat(Dict[Address].ByteBuff, Message.Skip(1).ToArray());
         }
         else
         {
             Dict[Address].ThreadState.transmission = true;
             Dict[Address].ByteBuff = Message.Skip(1).ToArray();
         }
     }
     catch {
         //CloFunc.Errorinfo("Unexpected Byte Fail.");
     }
 }
コード例 #14
0
 // private void AsynServerListen(IAsyncResult ar){
 //     ListenSocket = (Socket) ar.AsyncState;
 //     FileSocket = ListenSocket.EndAccept(ar);
 // }
 private void ServerListen()
 {
     //State.Listen = true;
     Console.WriteLine("Waiting...");
     while (!Config.SHUTDOWN)
     {
         try {
             //ListenSocket.BeginAccept(new AsyncCallback(AsynServerListen),ListenSocket);
             FileSocket = ListenSocket.Accept();
             //////////////!!!!!!!!!!!!!!!
             //Config.SHUTDOWN = true;
             Debug.Log("Server Accept");
             //////////////!!!!!!!!!!!!!!!
         }
         catch (Exception e) {
             if (Config.SHUTDOWN)
             {
                 break;
             }
             else
             {
                 Console.WriteLine(e.ToString());
             }
         }
         try
         {
             ThreadStruct AS      = new ThreadStruct();
             IPAddress    Address = getAddress(FileSocket);
             Dict.Add(((IPEndPoint)FileSocket.RemoteEndPoint).Address, AS);
             //Dict.Add(((IPEndPoint)FileSocket.RemoteEndPoint).Address,FileSocket);
             Dict[Address].ThreadSocket       = FileSocket;
             Dict[Address].ThreadState.Listen = true;
             Dict[Address].usergroup          = UserGroup.Stranger;
             Dict[Address].filepath           = "";
             log("Client" + " " + FileSocket.RemoteEndPoint + " " + "connected.");
             MessageThread = new Thread(new ParameterizedThreadStart(MessageTransport));
             Dict[Address].MessageThread = MessageThread;
             MessageThread.Start(FileSocket);//FileSocket
         }
         catch (Exception e)
         {
             CloFunc.Errorinfo("Listening Fail :\n" + e.ToString());
             //same ip adress try to connect twice is illegal
         }
     }
 }
コード例 #15
0
        private void SHUTHandle()
        {
            Config.SHUTDOWN = true;
            Console.WriteLine("Try Exiting...");

            foreach (var item in Dict)
            {
                item.Value.ThreadState.exit = true;
            }
            try {
                FileSocket.Close();
                ListenSocket.Close();
            }
            catch (Exception e) {
                CloFunc.Errorinfo(e.ToString());
            }
        }
コード例 #16
0
 private void checkfilepath()
 {
     if (!Directory.Exists(Config.filepath))
     {
         try
         {
             Directory.CreateDirectory(Config.filepath);
             Console.WriteLine("filepath created.");
         }
         catch
         {
             CloFunc.Errorinfo("Filepath Create Crash.");
         }
     }
     else
     {
         Console.WriteLine("Filepath Exists...OK.");
     }
 }
コード例 #17
0
 private void checklogpath()
 {
     //check if log file exists.
     if (!File.Exists(Config.logpath))
     {
         //if log file does not exist, create one.
         //!!! Exists bool may crash.
         Console.WriteLine("Log Unexists.");
         string[] pathtemp = Config.logpath.Split('\\');
         string   ptemp    = "";
         for (int i = 0; i < pathtemp.Length - 1; i++)
         {
             ptemp = ptemp + pathtemp[i];
         }
         if (!File.Exists(ptemp))
         {
             try
             {
                 Directory.CreateDirectory(ptemp);
             }
             catch
             {
                 CloFunc.Errorinfo("Path Create Crash.");
             }
         }
         try
         {
             File.Create(Config.logpath).Close();
             Console.WriteLine("log path created.");
         }
         catch
         {
             CloFunc.Errorinfo("log File Create Crash.");
         }
     }
     else
     {
         //log
         Console.WriteLine("Log Exists...OK.");
     }
 }
コード例 #18
0
 private void BendHandle(Socket MessageSocket, ServerControl Control, byte[] Message, int MessageCount, IPAddress Address)
 {
     try {
         if (Dict[Address].ThreadState.transmission)
         {
             Dict[Address].ByteBuff = CloFunc.Bytecat(Dict[Address].ByteBuff, Message.Skip(1).ToArray());
             Dict[Address].ThreadState.transmission = false;
             ThrowByteBuff(Address);
         }
         else
         {
             //single byte[]
             //Debug.Log("?//?");
             Dict[Address].ByteBuff = Message.Skip(1).ToArray();
             ThrowByteBuff(Address);
         }
     }
     catch {
         //CloFunc.Errorinfo("Unexpected Byte Fail.");
     }
 }
コード例 #19
0
 private void SignHandle(Socket MessageSocket, ServerControl Control, byte[] Message, int MessageCount, IPAddress Address)
 {
     try {
         string   ThisMessage = ByteEncoding(Message, MessageCount);
         string[] MessageArr  = ThisMessage.Split(' ').ToArray();
         if (IsUserExists(MessageArr[0]))
         {
             ErrorMessage(MessageSocket, "USER EXISTS.");
         }
         else
         {
             FileStream fp = File.Open(Config.userpath, FileMode.Append, FileAccess.Write);
             fp.Write(Message, 1, MessageCount);
             fp.Close();
             TextMessage(MessageSocket, ServerControl.TEXT, "Sign Up Success.");
         }
     }
     catch {
         //
         CloFunc.Errorinfo("Unexpected SIGN Fail.");
     }
 }
コード例 #20
0
        private void ReceiveMessage()
        {
            byte[] buffer = new byte[2048];
            //HandSocket.Receive(buffer);
            int MessageCount;

            NFTServer.ServerControl Control;
            MessageCount = HandSocket.Receive(buffer);
            if (MessageCount == 0)
            {
                //Thread exit
            }
            else if (!Enum.IsDefined(typeof(NFTServer.ServerControl), (int)buffer[0]))
            {
                CloFunc.Errorinfo("Error: Undefined Control Head.");
            }
            else
            {
                Control = (NFTServer.ServerControl)buffer[0];
                //Message = Encoding.Default.GetString(buffer, 1, MessageCount - 1);
                MessageHandle(Control, buffer, MessageCount - 1);
            }
        }
コード例 #21
0
        private void MessageTransport(object obj)
        {
            Socket     MessageSocket = obj as Socket;
            IPAddress  Address       = getAddress(MessageSocket);
            IPEndPoint IPinfo        = (IPEndPoint)MessageSocket.RemoteEndPoint;

            byte[]        buffer = new byte[2048];
            int           MessageCount;
            ServerControl Control;

            ////////////
            //MessageSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
            ////////////
            //string Message;
            while (!Dict[Address].ThreadState.exit)
            {
                //////
                //Console.WriteLine("Waiting for message.");
                //////
                try {
                    MessageCount = MessageSocket.Receive(buffer);
                    //Debug.Log("Server Receive");
                }
                catch {
                    ////////
                    break;
                }
                if (MessageCount == 0)
                {
                    //Thread exit
                    break;
                }
                if (!Enum.IsDefined(typeof(ServerControl), (int)buffer[0]))
                {
                    log("Error: " + MessageSocket.RemoteEndPoint + " Undefined Control Head.");
                }
                else
                {
                    Control = (ServerControl)buffer[0];
                    //Message = Encoding.Default.GetString(buffer, 1, MessageCount - 1);
                    if (AuthorityCheck(Control, Dict[getAddress(MessageSocket)].usergroup))
                    {
                        /////////////
                        //Console.WriteLine("messagesocket start");
                        /////////////
                        try {
                            MessageHandle(MessageSocket, Control, buffer, MessageCount - 1);
                        }
                        catch (Exception e) {
                            CloFunc.Errorinfo("MessageHandle Error :\n" + e.ToString());
                        }
                    }
                    else
                    {
                        ErrorMessage(MessageSocket, "Request Denied.");
                    }
                }
            }
            log("Client" + " " + IPinfo + " " + "disconnected.");
            ////////////////////////////
            Dict[Address].ThreadSocket.Close();
            Dict.Remove(Address);
            ////////////////////////////
        }
コード例 #22
0
        private void CommandInput(string[] args)
        {
            if (args.Length == 0)
            {
                CloFunc.Errorinfo("Can't run without any arguments.");
                ArgumentHelp();
            }
            else
            {
                try
                {
                    switch (args[0])
                    {
                    case "-c":
                    case "connect":
                    {
                        if (args.Length == 1)
                        {
                            Console.WriteLine("<adress> expected.");
                        }
                        else if (state.Connected)
                        {
                            CloFunc.Errorinfo("Server Already Connected.");
                        }
                        else
                        {
                            try
                            {
                                ConnectToIp(args[1]);
                                state.Connected = true;
                                state.IPReady   = true;
                                Console.WriteLine("Connect Sucess.");
                            }
                            catch (Exception e)
                            {
                                CloFunc.Errorinfo("Connect Fail." + '\n' + e.ToString());
                            }
                            if (args.Length > 2)
                            {
                                CommandInput(CloFunc.slice(args, 2, args.Length - 1));
                            }
                        }
                        break;
                    }

                    case "-p":
                    case "port":
                    {
                        if (args.Length == 1)
                        {
                            Console.WriteLine("<port> expected.");
                        }
                        else if (state.Connected)
                        {
                            CloFunc.Errorinfo("Can't change port while connecting.");
                        }
                        else
                        {
                            try
                            {
                                Config.Port     = int.Parse(args[1]);
                                state.PortReady = true;
                            }
                            catch
                            {
                                CloFunc.Errorinfo("ERROR:Port should be int number.");
                            }
                        }
                        if (args.Length > 2)
                        {
                            CommandInput(CloFunc.slice(args, 2, args.Length - 1));
                        }
                        break;
                    }

                    case "-h":
                    case "help":
                    case "?": {
                        ArgumentHelp();
                        break;
                    }

                    case "-v":
                    case "version": {
                        Console.WriteLine("NetFileTransportG <version 0.1>");
                        break;
                    }

                    case "--debug": {
                        DebugMission();
                        break;
                    }

                    case "user": {
                        if (!state.Connected)
                        {
                            CloFunc.Errorinfo("Please Connect to Server First");
                        }
                        else if (args.Length == 1)
                        {
                            Console.WriteLine("<username> expected.");
                        }
                        else if (state.login || state.user)
                        {
                            CloFunc.Errorinfo("Already login or input user.");
                        }
                        else
                        {
                            try
                            {
                                TextMessage(HandSocket, NFTServer.ServerControl.USER, args[1]);
                                ReceiveMessage();
                                state.user = true;//label
                            }
                            catch
                            {
                                CloFunc.Errorinfo("ERROR: Command User Fail.");
                            }
                        }
                        if (args.Length > 2)
                        {
                            CommandInput(CloFunc.slice(args, 2, args.Length - 1));
                        }
                        break;
                    }

                    case "pass": {
                        if (args.Length == 1)
                        {
                            Console.WriteLine("<password> expected.");
                        }
                        else if (state.login || !state.user)
                        {
                            CloFunc.Errorinfo("Already login or need username.");
                        }
                        else
                        {
                            try
                            {
                                //Console.WriteLine(args[1].GetHashCode());
                                TextMessage(HandSocket, NFTServer.ServerControl.PASS, args[1].GetHashCode().GetHashCode().ToString());
                                state.pass = true;
                                ReceiveMessage();
                                if (state.pass)
                                {
                                    state.login = true;
                                }
                            }
                            catch
                            {
                                CloFunc.Errorinfo("ERROR: Command Pass Fail.");
                            }
                        }
                        if (args.Length > 2)
                        {
                            CommandInput(CloFunc.slice(args, 2, args.Length - 1));
                        }
                        break;
                    }

                    case "ls": {
                        TextMessage(HandSocket, NFTServer.ServerControl.LIST, "Show me list.");
                        state.list  = true;
                        MessageTemp = "";
                        while (state.list)
                        {
                            ReceiveMessage();
                        }
                        break;
                    }

                    case "dir": {
                        if (args.Length == 1)
                        {
                            Console.WriteLine("<directory> expected.");
                        }
                        else if (!state.Connected)
                        {
                            CloFunc.Errorinfo("None Server Connected.");
                        }
                        else
                        {
                            try
                            {
                                TextMessage(HandSocket, NFTServer.ServerControl.CDIR, args[1]);
                                ReceiveMessage();
                            }
                            catch
                            {
                                CloFunc.Errorinfo("Create Directory Fail.");
                            }
                        }
                        if (args.Length > 2)
                        {
                            CommandInput(CloFunc.slice(args, 2, args.Length - 1));
                        }
                        break;
                    }

                    case "cd": {
                        if (args.Length == 1)
                        {
                            Console.WriteLine("<directory> expected.");
                        }
                        else if (!state.Connected)
                        {
                            CloFunc.Errorinfo("None Server Connected.");
                        }
                        else
                        {
                            try
                            {
                                TextMessage(HandSocket, NFTServer.ServerControl.CDCD, args[1]);
                                ReceiveMessage();
                            }
                            catch
                            {
                                CloFunc.Errorinfo("GetIn Directory Fail.");
                            }
                        }
                        if (args.Length > 2)
                        {
                            CommandInput(CloFunc.slice(args, 2, args.Length - 1));
                        }
                        break;
                    }

                    case "del":
                    case "delete": {
                        if (args.Length == 1)
                        {
                            Console.WriteLine("<directory> expected.");
                        }
                        else if (!state.Connected)
                        {
                            CloFunc.Errorinfo("None Server Connected.");
                        }
                        else
                        {
                            try
                            {
                                TextMessage(HandSocket, NFTServer.ServerControl.DDEL, args[1]);
                                //state.deldir = true;
                                ReceiveMessage();
                            }
                            catch
                            {
                                CloFunc.Errorinfo("Delete Directory Fail.");
                            }
                        }
                        if (args.Length > 2)
                        {
                            CommandInput(CloFunc.slice(args, 2, args.Length - 1));
                        }
                        break;
                    }

                    case "-r":
                    case "remove": {
                        if (args.Length == 1)
                        {
                            Console.WriteLine("<file> expected.");
                        }
                        else if (!state.Connected)
                        {
                            CloFunc.Errorinfo("None Server Connected.");
                        }
                        else
                        {
                            try
                            {
                                TextMessage(HandSocket, NFTServer.ServerControl.FDEL, args[1]);
                                //state.deldir = true;
                                ReceiveMessage();
                            }
                            catch
                            {
                                CloFunc.Errorinfo("Delete Directory Fail.");
                            }
                        }
                        if (args.Length > 2)
                        {
                            CommandInput(CloFunc.slice(args, 2, args.Length - 1));
                        }
                        break;
                    }

                    case "-d":
                    case "download": {
                        if (args.Length == 1)
                        {
                            Console.WriteLine("<file> expected.");
                        }
                        else if (!state.Connected)
                        {
                            CloFunc.Errorinfo("None Server Connected.");
                        }
                        else
                        {
                            try
                            {
                                DownloadProcess(args[1]);
                            }
                            catch
                            {
                                CloFunc.Errorinfo("DownLoad File Fail.");
                            }
                        }
                        if (args.Length > 2)
                        {
                            CommandInput(CloFunc.slice(args, 2, args.Length - 1));
                        }
                        break;
                    }

                    case "-u":
                    case "upload": {
                        if (args.Length == 1)
                        {
                            Console.WriteLine("<file> expected.");
                        }
                        else if (!state.Connected)
                        {
                            CloFunc.Errorinfo("None Server Connected.");
                        }
                        else
                        {
                            try
                            {
                                UploadProcess(args[1]);
                            }
                            catch
                            {
                                CloFunc.Errorinfo("UpLoad File Fail.");
                            }
                        }
                        if (args.Length > 2)
                        {
                            CommandInput(CloFunc.slice(args, 2, args.Length - 1));
                        }
                        break;
                    }

                    case "sign": {
                        if (!state.Connected)
                        {
                            CloFunc.Errorinfo("None Server Connected.");
                        }
                        else if (args.Length < 3)
                        {
                            CloFunc.Errorinfo("<username> or <password> required");
                        }
                        else
                        {
                            string username    = args[1];
                            string password    = args[2].GetHashCode().ToString();
                            string tempmessage = username + ' ' + password;
                            TextMessage(HandSocket, NFTServer.ServerControl.SIGN, tempmessage);
                            ReceiveMessage();
                        }
                        if (args.Length > 3)
                        {
                            CommandInput(CloFunc.slice(args, 2, args.Length - 1));
                        }
                        break;
                    }

                    case "rspass": {
                        if (!state.Connected)
                        {
                            CloFunc.Errorinfo("None Server Connected.");
                        }
                        else if (args.Length == 1)
                        {
                            CloFunc.Errorinfo("<password> required");
                        }
                        else
                        {
                            string password = args[1].GetHashCode().ToString();
                            TextMessage(HandSocket, NFTServer.ServerControl.RPAS, password);
                            ReceiveMessage();
                        }
                        if (args.Length > 2)
                        {
                            CommandInput(CloFunc.slice(args, 2, args.Length - 1));
                        }
                        break;
                    }

                    case "shutdown": {
                        if (!state.Connected)
                        {
                            CloFunc.Errorinfo("None Server Connected.");
                        }
                        else if (args.Length == 1)
                        {
                            TextMessage(HandSocket, NFTServer.ServerControl.SHUT, "SHUTDOWN");
                            state.Connected = false;
                        }
                        else
                        {
                            CommandInput(CloFunc.slice(args, 1, args.Length - 1));
                            TextMessage(HandSocket, NFTServer.ServerControl.SHUT, "SHUTDOWN");
                            state.Connected = false;
                        }
                        break;
                    }

                    case "-e":
                    case "exit": {
                        if (args.Length == 1)
                        {
                            ExitProcess();
                        }
                        else
                        {
                            CommandInput(CloFunc.slice(args, 1, args.Length - 1));
                            ExitProcess();
                        }
                        break;
                    }

                    case "disconnect": {
                        if (!state.Connected)
                        {
                            CloFunc.Errorinfo("None Servers Connected.");
                        }
                        else
                        {
                            DisconnectProcess();
                            if (args.Length > 2)
                            {
                                CommandInput(CloFunc.slice(args, 1, args.Length - 1));
                            }
                        }
                        break;
                    }

                    case "Supload": {
                        if (!state.Connected)
                        {
                            CloFunc.Errorinfo("None Server Connected.");
                        }
                        else
                        {
                            foreach (string filename in args.Skip(1).ToArray())
                            {
                                try
                                {
                                    UploadProcess(filename);
                                }
                                catch
                                {
                                    CloFunc.Errorinfo("UpLoad File Fail.");
                                }
                            }
                        }
                        break;
                    }

                    default: {
                        CloFunc.Errorinfo("Unexpected arguments");
                        ConsoleMessage("Use \"help\" to get arguments help.");
                        break;
                    }
                    }
                }
                catch (Exception e)
                {
                    CloFunc.Errorinfo("Unexpected arguments' error.");
                    Console.WriteLine(e.Message);
                }
            }
        }
コード例 #23
0
 public static void CalculatePing()
 {
     ping = (CloFunc.GetTimeNow() - Timetemp).TotalMilliseconds;
 }
コード例 #24
0
        private void CdHandle(Socket MessageSocket, ServerControl Control, byte[] Message, int MessageCount, IPAddress Address)
        {
            string path = ByteEncoding(Message, MessageCount);
            string dopathex;

            if (path.Replace(".", String.Empty) == "")
            {
                if (path == "..")
                {
                    if (Dict[Address].filepath == "" || Dict[Address].filepath == null)
                    {
                        ErrorMessage(MessageSocket, "Already at root menu.");
                    }
                    else
                    {
                        string[] pathlist = Dict[Address].filepath.Split('\\');
                        string   pathtemp = "";
                        for (int i = 0; i < pathlist.Length - 1; ++i)
                        {
                            if (i != pathlist.Length - 2)
                            {
                                pathtemp += pathlist[i] + @"\";
                            }
                            else
                            {
                                pathtemp += pathlist[i];
                            }
                        }
                        Dict[Address].filepath = pathtemp;
                        TextMessage(MessageSocket, ServerControl.CDCD, Dict[Address].filepath);
                    }
                }
                else
                {
                    /////////////////////////////
                    //Directory like "." mean itself
                    //".." mean parent directory
                    //And "...", "....", ".....", and so on All MEAN parent directory
                    //SO ALL THOSE THINGS IS UNSAFE!!!!!!!
                    /////////////////////////////
                    ErrorMessage(MessageSocket, "Directory does not Exist.");
                }
            }
            else
            {
                if (Dict[Address].filepath == "")
                {
                    dopathex = Config.filepath + @"\" + path;
                }
                else
                {
                    dopathex = Config.filepath + @"\" + Dict[Address].filepath + @"\" + path;
                }
                try {
                    if (!Directory.Exists(dopathex))
                    {
                        ErrorMessage(MessageSocket, "Directory does not Exist.");
                    }
                    else
                    {
                        if (Dict[Address].filepath == "" || path == "")
                        {
                            Dict[Address].filepath += path;
                        }
                        else
                        {
                            Dict[Address].filepath += @"\" + path;
                        }
                        TextMessage(MessageSocket, ServerControl.CDCD, Dict[Address].filepath);
                    }
                }
                catch (Exception e) {
                    CloFunc.Errorinfo("UnExpected CDIR Fail: " + e.ToString());
                }
            }
        }
コード例 #25
0
 private IPAddress getAddress(Socket s)
 {
     return(CloFunc.getAddress(s));
 }
コード例 #26
0
        private void MessageHandle(NFTServer.ServerControl Control, byte[] Message, int MessageCount)
        {
            switch (Control)
            {
            case NFTServer.ServerControl.DBUG: {
                Console.WriteLine(Encoding.Default.GetString(Message, 1, MessageCount));
                break;
            }

            case NFTServer.ServerControl.ERRO: {
                ErrorHandle();
                CloFunc.Errorinfo(ByteEncoding(Message, MessageCount));

                break;
            }

            case NFTServer.ServerControl.TEXT: {
                ConsoleMessage(ByteEncoding(Message, MessageCount));
                break;
            }

            case NFTServer.ServerControl.LIST: {
                MessageTemp += ByteEncoding(Message, MessageCount);
                break;
            }

            case NFTServer.ServerControl.CDCD: {
                Config.serverpath = ByteEncoding(Message, MessageCount);
                break;
            }

            case NFTServer.ServerControl.LEND: {
                state.list = false;
                ConsoleMessage(MessageTemp);
                MessageTemp = "";
                break;
            }

            case NFTServer.ServerControl.DDEL: {
                //state.deldir = false;
                ConsoleMessage(ByteEncoding(Message, MessageCount));
                break;
            }

            case NFTServer.ServerControl.DLFL: {
                if (state.download)
                {
                    string filename = ByteEncoding(Message, MessageCount);
                    TextMessage(HandSocket, NFTServer.ServerControl.FSTT, filename);
                }
                break;
            }

            case NFTServer.ServerControl.ULFL: {
                if (state.upload)
                {
                    string filename = ByteEncoding(Message, MessageCount);
                    Config.FileLength = fp.Length;
                    tempLength        = 0;
                    TextMessage(HandSocket, NFTServer.ServerControl.FSTT, fp.Length.ToString());
                    ////////////////////////////////////////
                    long   count        = (Config.FileLength + 2046) / 2047;
                    byte[] messagebuff  = new byte[2047];
                    byte[] finalmessage = new byte[(int)(fp.Length % 2047)];
                    Console.Write("Process:{0} / {1}", tempLength, Config.FileLength);
                    //Console.CursorVisible = false;
                    for (long i = 0; i < count; ++i)
                    {
                        if (i != count - 1)
                        {
                            tempLength += 2047;
                            fp.Read(messagebuff, 0, 2047);
                            ByteMessage(NFTServer.ServerControl.FILE, messagebuff);
                        }
                        else
                        {
                            tempLength += finalmessage.Length;
                            fp.Read(finalmessage, 0, finalmessage.Length);
                            ByteMessage(NFTServer.ServerControl.FEND, finalmessage);
                        }
                        //Console.SetCursorPosition(0, Console.CursorTop);
                        Console.Write("Process:{0} / {1}", tempLength, Config.FileLength);
                    }
                    //Console.SetCursorPosition(0, Console.CursorTop);
                    Console.WriteLine("Process:{0} / {1}", tempLength, Config.FileLength);
                    //Console.CursorVisible = true;
                    //TextMessage(HandSocket, NFTServer.ServerControl.FEND, "FL END");
                    state.upload = false;
                    fp.Close();
                }
                break;
            }

            case NFTServer.ServerControl.FSTT: {
                if (state.download)
                {
                    Config.FileLength = long.Parse(ByteEncoding(Message, MessageCount));
                    //Console.CursorVisible = false;
                    tempLength = 0;
                    Console.Write("Process:{0} / {1}", tempLength, Config.FileLength);
                }
                else if (state.upload)
                {
                    ///////
                }
                ///////////
                break;
            }

            case NFTServer.ServerControl.FILE: {
                if (state.download)
                {
                    fp.Write(Message, 1, MessageCount);
                    //Console.SetCursorPosition(0, Console.CursorTop);
                    tempLength += MessageCount;
                    //////////////////////////////////////
                    if (tempLength == Config.FileLength)
                    {
                        state.download = false;
                    }
                    //////////////////////////////////////
                    Console.Write("Process:{0} / {1}", tempLength, Config.FileLength);
                }
                break;
            }

            case NFTServer.ServerControl.FEND: {
                if (state.download)
                {
                    fp.Write(Message, 1, MessageCount);
                    tempLength += MessageCount;
                    fp.Close();
                    state.download = false;
                    //Console.SetCursorPosition(0, Console.CursorTop);
                    //tempLength += MessageCount;
                    Console.WriteLine("Process:{0} / {1}", tempLength, Config.FileLength);
                    //Console.CursorVisible = true;
                }
                break;
            }

            default: {
                CloFunc.Errorinfo("Unknown Control Head");
                break;
            }
            }
        }
コード例 #27
0
 private void log(string loginfo)
 {
     loginfo = TimeCode(loginfo);
     Console.WriteLine(loginfo);
     CloFunc.Filelog(Config.logpath, loginfo);
 }