예제 #1
0
        private void _serverSocket_OnRequested(IUserToken userToken, string htmlStr)
        {
            while (true)
            {
                if (_queue.TryDequeue(out HttpContext httpContext))
                {
                    try
                    {
                        httpContext.Init(this, userToken, htmlStr);

                        _dic.TryAdd(userToken.ID, httpContext);

                        httpContext.InvokeAction();
                    }
                    catch (Exception ex)
                    {
                        _serverSocket.Disconnect(userToken, ex);
                    }
                    break;
                }
                else
                {
                    Thread.Sleep(1);
                }
            }
        }
예제 #2
0
        private void _serverSocket_OnRequested(IUserToken userToken, RequestDataReader requestDataReader)
        {
            try
            {
                using (var httpContext = new HttpContext())
                {
                    httpContext.Init(this, userToken, requestDataReader);

                    httpContext.HttpHandler();
                }
            }
            catch (Exception ex)
            {
                _serverSocket.Disconnect(userToken, ex);
            }
        }
예제 #3
0
 /// <summary>
 /// Dequeues the client
 /// </summary>
 public void Dequeue()
 {
     ClientSocket.Disconnect(false);
     ServerSocket.Disconnect(false);
     ClientSocket = null;
     ServerSocket = null;
 }
예제 #4
0
 private void Disconnect(object sender, NavElementEventArgs e)
 {
     serverSocket.Disconnect(sender, e);
     serverSocket.UpdateStatus("Shut down", Status, e);
     serverSocket.UpdateLog("Server shut down", log_box, e);
     isInit = false;
 }
예제 #5
0
        /// <summary>
        /// Disconnects the client and stops the state machine
        /// </summary>
        public void Disconnect()
        {
            StartConnection = false;
            CycleTimer.Change(0, Timeout.Infinite);

            if (ServerSocket != null && ServerSocket.Connected == true)
            {
                try
                {
                    ServerSocket.Disconnect(false);
                }
                catch { }
            }
        }
예제 #6
0
        private void NamedIpc_Disconnect(object sender, EventArgs e)
        {
            ISLogger.Write($"Service->Ipc requested disconnect");
            if (!cSocket.IsStateConnected())
            {
                if (namedIpc.Active)
                {
                    namedIpc.SendObject(NIpcBasicMessage.AlreadyDisconnected);
                }

                return;
            }


            cSocket.Disconnect();
        }
예제 #7
0
        private void Btn_cont_Click(object sender, RoutedEventArgs e)
        {
            if (btn_cont.IsChecked == true)
            {
                serverSocket = new ServerSocket(10001);

                serverSocket.AsyncDataReceive -= OnReceiveData;
                serverSocket.AsyncDataReceive += OnReceiveData;

                serverSocket.Connect();
                MSG("服务开启");
            }
            else
            {
                serverSocket.Disconnect();
                MSG("服务断开");
            }
        }
 public virtual void Dispose()
 {
     ClientSocket.Disconnect(false);
     ServerSocket.Disconnect(false);
     GC.SuppressFinalize(this);
 }
예제 #9
0
 //Remove Connection
 public void RemoveConnection(int key)
 {
     ServerSocket.Disconnect(clientCollection[key]);
     clientCollection[key] = null;
     numberOfConnections  -= 1;
 }
예제 #10
0
        void DoCommand(Socket clientCommand, char[] buffer)
        {
            string[] command = (new string(buffer)).Split(' ', '\r', '\n');
            string   path;

            command[0] = command[0].ToUpper();
            switch (command[0].ToUpper())
            {
            case "SYST":
                ServerSocket.SendString(("215 UNIX Type: L8\r\n").ToCharArray(), clientCommand);
                break;

            case "FEAT":
                ServerSocket.SendString(("211-Features:\n MDTM\n NLST\n MLST type *; size *; modify *;\n MLSD  UTF8\n REST STREAM\n SIZE\n211 End\r\n").ToCharArray(), clientCommand);
                break;

            case "TYPE":
                //if (command.Length == 1) ServerSocket.SendString(("TYPE I\r\n").ToCharArray(), clientCommand);
                if (command[1].Equals("A"))
                {
                    asciiType = true;
                    ServerSocket.SendString(("200 OK\r\n").ToCharArray(), clientCommand);
                }
                else if (command[1].Equals("I"))
                {
                    asciiType = false;
                    ServerSocket.SendString(("200 OK\r\n").ToCharArray(), clientCommand);
                }
                else
                {
                    ServerSocket.SendString(("504 Command not implemented for that parameter.").ToCharArray(), clientCommand);
                }
                break;

            case "PASV":
                if (clientTransfer != null)
                {
                    clientTransfer.Close();
                    clientTransfer = null;
                }
                ServerSocket transferSocket = new ServerSocket();
                string[]     endPointParts  = clientCommand.LocalEndPoint.ToString().Split('.', ':');
                transferSocket.Initialize(endPointParts[0] + '.' + endPointParts[1] + '.' + endPointParts[2] + '.' + endPointParts[3], port);
                ServerSocket.SendString(("227 Entering Passive Mode (" + endPointParts[0] + ',' + endPointParts[1] + ',' + endPointParts[2] + ',' + endPointParts[3] + ',' + (port / 256) + ',' + (port % 256) + ")\n").ToCharArray(), clientCommand);
                clientTransfer = transferSocket.Listen();
                break;

            case "PWD":
                ServerSocket.SendString(("257 \"" + workingDirectory + "\"\r\n").ToCharArray(), clientCommand);
                break;

            case "MLST":
                ServerSocket.SendString(("250-Listing " + workingDirectory + ":\n" + fileSystem.Find(item => item.name.Equals(command[1])).MachineList() + " " + fileSystem.Find(item => item.name.Equals(command[1])).name + "\n250 End.\r\n").ToCharArray(), clientCommand);
                break;

            case "SIZE":
                ServerSocket.SendString(("213 " + fileSystem.Find(item => item.name.Equals(command[1])).size + "\r\n").ToCharArray(), clientCommand);
                break;

            case "MDTM":
                ServerSocket.SendString(("213 " + fileSystem.Find(item => item.name.Equals(command[1])).modificationTime + "\r\n").ToCharArray(), clientCommand);
                break;

            case "CWD":
                if (!command[1].StartsWith("/"))
                {
                    path = workingDirectory + command[1];
                    if (!path.EndsWith("/"))
                    {
                        path += "/";
                    }
                }
                else
                {
                    if (command[1].LastIndexOf("/") != 0)
                    {
                        path = home + command[1];
                        if (command[1].EndsWith("/"))
                        {
                            path += "/";
                        }
                    }
                    else
                    {
                        path = home;
                    }
                }
                if (Directory.Exists(path))
                {
                    workingDirectory = path;
                    MakeFileSystem();
                    ServerSocket.SendString("250 Directory Successfully Changed\r\n".ToCharArray(), clientCommand);
                }
                else
                {
                    ServerSocket.SendString(("550 Can't change directory to \"" + path + "\"\r\n").ToCharArray(), clientCommand);
                }
                break;

            case "CDUP":
                if (!workingDirectory.Equals("/"))
                {
                    workingDirectory = workingDirectory.Substring(0, workingDirectory.Length - workingDirectory.IndexOf('/', 0, workingDirectory.Length - 1));
                }
                MakeFileSystem();
                ServerSocket.SendString(("250 Directory successfully changed\r\n").ToCharArray(), clientCommand);
                break;

            case "DELE":
                if ((File.Exists(workingDirectory + command[1])) && !(new FileInfo(workingDirectory + command[1])).IsReadOnly)
                {
                    File.Delete(workingDirectory + command[1]);
                    if (fileSystem.Remove(fileSystem.Find(item => item.name.Equals(command[1]))))
                    {
                        ServerSocket.SendString(("250 Deleted file \"" + command[1] + "\"\r\n").ToCharArray(), clientCommand);
                    }
                    else
                    {
                        ServerSocket.SendString(("550 Can't delete file \"" + command[1] + "\"\r\n").ToCharArray(), clientCommand);
                    }
                }
                else
                {
                    ServerSocket.SendString(("550 Can't delete file \"" + command[1] + "\"\r\n").ToCharArray(), clientCommand);
                }
                break;

            //case "RNFR":
            //    if ((File.Exists(workingDirectory + command[1])) && !(new FileInfo(workingDirectory + command[1])).IsReadOnly)
            //    {
            //        File.Delete(workingDirectory + command[1]);
            //        ServerSocket.SendString(("250 Deleted file \"" + command[1] + "\"\r\n").ToCharArray(), clientCommand);
            //    }
            //    break;
            //case "RNTO":
            //    break;
            case "MKD":
                path  = workingDirectory;
                path += command[1];
                if (!command[1].EndsWith("/"))
                {
                    path += "/";
                }
                try
                {
                    if (!Directory.Exists(path))
                    {
                        throw new IOException();
                    }
                    fileSystem.Add(new FileSystemItem(Directory.CreateDirectory(path)));
                    ServerSocket.SendString(("257 \"" + path + "\" directory created\r\n").ToCharArray(), clientCommand);
                }
                catch
                {
                    ServerSocket.SendString(("521 Can't create directory\r\n").ToCharArray(), clientCommand);
                }
                break;

            case "RMD":
                path  = workingDirectory;
                path += command[1];
                if (!command[1].EndsWith("/"))
                {
                    path += "/";
                }
                try
                {
                    Directory.Delete(path, true);
                    if (fileSystem.Remove(fileSystem.Find(item => item.name.Equals(command[1]))))
                    {
                        ServerSocket.SendString(("250 Directory removed\r\n").ToCharArray(), clientCommand);
                    }
                    else
                    {
                        ServerSocket.SendString(("550 Can't remove directory\r\n").ToCharArray(), clientCommand);
                    }
                }
                catch
                {
                    ServerSocket.SendString(("550 Can't remove directory\r\n").ToCharArray(), clientCommand);
                }
                break;

            case "LIST":
                if (clientTransfer != null)
                {
                    ServerSocket.SendString("150 Opening BINARY Mode for Data Connection for LIST\n".ToCharArray(), clientCommand);
                    ListFilesWithDetails(clientTransfer);
                    ServerSocket.SendString(("226 Transfer Complete\r\n").ToCharArray(), clientCommand);
                    ServerSocket.Disconnect(clientTransfer);
                    clientTransfer = null;
                    port          += maxConnections;
                }
                else
                {
                    ServerSocket.SendString(("426 Use PORT or PASV first\r\n").ToCharArray(), clientCommand);
                }
                break;

            case "MLSD":
                if (clientTransfer != null)
                {
                    ServerSocket.SendString("150 Opening BINARY Mode for Data Connection for MLSD\n".ToCharArray(), clientCommand);
                    ListFilesForMachines(clientTransfer);
                    ServerSocket.SendString(("226 Transfer Complete\r\n").ToCharArray(), clientCommand);
                    ServerSocket.Disconnect(clientTransfer);
                    clientTransfer = null;
                    port          += maxConnections;
                }
                else
                {
                    ServerSocket.SendString(("426 Use PORT or PASV first\r\n").ToCharArray(), clientCommand);
                }
                break;

            case "NLST":
                if (clientTransfer != null)
                {
                    ServerSocket.SendString("150 Opening BINARY Mode for Data Connection for NLST\n".ToCharArray(), clientCommand);
                    ListFiles(clientTransfer);
                    ServerSocket.SendString(("226 Transfer Complete\r\n").ToCharArray(), clientCommand);
                    ServerSocket.Disconnect(clientTransfer);
                    clientTransfer = null;
                    port          += maxConnections;
                }
                else
                {
                    ServerSocket.SendString(("426 Use PORT or PASV first\r\n").ToCharArray(), clientCommand);
                }
                break;

            case "STOR":
                if (clientTransfer != null)
                {
                    ServerSocket.Disconnect(clientTransfer);
                    clientTransfer = null;
                    port          += maxConnections;
                }
                else
                {
                    ServerSocket.SendString(("426 Use PORT or PASV first\r\n").ToCharArray(), clientCommand);
                }
                break;

            case "RETR":
                if (clientTransfer != null)
                {
                    ServerSocket.Disconnect(clientTransfer);
                    clientTransfer = null;
                    port          += maxConnections;
                }
                else
                {
                    ServerSocket.SendString(("426 Use PORT or PASV first\r\n").ToCharArray(), clientCommand);
                }
                break;

            case "OPTS":
                utf8 = true;
                ServerSocket.SendString(("200 Command Ok\r\n").ToCharArray(), clientCommand);
                break;

            case "NOOP":
                ServerSocket.SendString(("200 Ok\r\n").ToCharArray(), clientCommand);
                break;

            case "QUIT":
                break;

            default:
                ServerSocket.SendString(("502 Unknown ftp command\r\n").ToCharArray(), clientCommand);
                break;
            }
        }
예제 #11
0
 private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
 {
     mySeverSocket.Disconnect();
 }
예제 #12
0
 internal void Close(IUserToken userToken)
 {
     _serverSocket.Disconnect(userToken);
 }