예제 #1
0
        private void trvDir_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            /* Send BeforeExpand to Server */
            BeforeExpand be = new BeforeExpand();

            be.Type = (int)PacketType.beforeExpand;
            be.path = e.Node.FullPath;

            Packet.Serialize(be).CopyTo(sendBuffer, 0);
            Send();

            /* Receive Dir Array And Dictionary from Server */
            m_stream.Read(readBuffer, 0, 1024 * 4);
            BeforeExpand             m_beforeExpandClass = (BeforeExpand)Packet.Desserialize(readBuffer);
            Dictionary <string, int> dirDic = m_beforeExpandClass.diAdd;

            /* Set TreeView */
            TreeNode node;

            e.Node.Nodes.Clear();
            foreach (DirectoryInfo dirs in m_beforeExpandClass.diArray)
            {
                node            = e.Node.Nodes.Add(dirs.Name);
                node.ImageIndex = 1;

                /* Set Plus */
                if (dirDic[dirs.Name] == 1)
                {
                    node.Nodes.Add("");
                }
            }
        }
예제 #2
0
 private void RaiseBeforeExpand()
 {
     BeforeExpand?.Invoke(this, EventArgs.Empty);
 }
예제 #3
0
        public void ServerStart()
        {
            try
            {
                string    ip     = txtIp.Text;
                int       port   = int.Parse(txtPort.Text);
                IPAddress ipAddr = IPAddress.Parse(ip);

                m_server = new TcpListener(ipAddr, port);
                m_server.Start();

                m_bStop = true;
                WriteLog("클라이언트 접속 대기중...");

                while (m_bStop)
                {
                    m_client = m_server.AcceptTcpClient();

                    if (m_client.Connected)
                    {
                        m_bConnect = true;
                        WriteLog("클라이언트 접속");
                        m_stream = m_client.GetStream();
                    }

                    while (m_bConnect)
                    {
                        try
                        {
                            m_stream.Read(readBuffer, 0, 1024 * 4);
                        }
                        catch
                        {
                            WriteLog("서버에서 데이터를 읽는데 에러가 발생해 서버를 종료합니다.");
                            ServerStop();
                            this.Invoke(new MethodInvoker(delegate()
                            {
                                btnServer.Text      = "서버켜기";
                                btnServer.ForeColor = Color.Black;
                            }));
                            return;
                        }

                        Packet packet = (Packet)Packet.Desserialize(readBuffer);

                        switch ((int)packet.Type)
                        {
                        case (int)PacketType.init:
                        {
                            m_initializeClass = (Initialize)Packet.Desserialize(readBuffer);
                            WriteLog("초기화 데이터 요청..");

                            /* Send Path to client */
                            byte[] bytePath = Encoding.UTF8.GetBytes(dirPath);
                            m_stream.Write(bytePath, 0, bytePath.Length);
                            break;
                        }

                        case (int)PacketType.beforeSelect:
                        {
                            m_beforeSelectClass = (BeforeSelect)Packet.Desserialize(readBuffer);
                            WriteLog("beforeSelect 데이터 요청..");
                            string path = m_beforeSelectClass.path;

                            /* Send Dir and File Array to client */
                            DirectoryInfo di;
                            BeforeSelect  bs = new BeforeSelect();
                            try
                            {
                                di         = new DirectoryInfo(path);
                                bs.Type    = (int)PacketType.beforeSelect;
                                bs.diArray = di.GetDirectories();
                                bs.fiArray = di.GetFiles();
                                Packet.Serialize(bs).CopyTo(sendBuffer, 0);
                                Send();
                            }
                            catch (Exception ex)
                            {
                                WriteLog("BeforeSelect error " + ex.Message);
                            }
                            break;
                        }

                        case (int)PacketType.beforeExpand:
                        {
                            m_beforeExpandClass = (BeforeExpand)Packet.Desserialize(readBuffer);
                            WriteLog("beforeExpand 데이터 요청..");
                            string path = m_beforeExpandClass.path;

                            /* Send Dir Array And Dictionary to client */
                            DirectoryInfo   di;
                            DirectoryInfo   diPlus;
                            DirectoryInfo[] diArrayPlus;
                            BeforeExpand    be = new BeforeExpand();
                            try
                            {
                                di         = new DirectoryInfo(path);
                                be.Type    = (int)PacketType.beforeExpand;
                                be.diArray = di.GetDirectories();

                                /* To Set Plus */
                                be.diAdd = new Dictionary <string, int>();
                                foreach (DirectoryInfo dir in be.diArray)
                                {
                                    diPlus      = new DirectoryInfo(dir.FullName);
                                    diArrayPlus = diPlus.GetDirectories();
                                    if (diArrayPlus.Length > 0)
                                    {
                                        be.diAdd.Add(dir.Name, 1);
                                    }
                                    else
                                    {
                                        be.diAdd.Add(dir.Name, 0);
                                    }
                                }
                                Packet.Serialize(be).CopyTo(sendBuffer, 0);
                                Send();
                            }
                            catch (Exception ex)
                            {
                                WriteLog("BeforeExpand error " + ex.Message);
                            }
                            break;
                        }

                        case (int)PacketType.fileTransfer:
                        {
                            m_fileTransferClass = (FileTransfer)Packet.Desserialize(readBuffer);
                            WriteLog("파일 전송 요청..");
                            string path           = m_fileTransferClass.path;
                            long   size           = m_fileTransferClass.size;
                            byte[] fileSendBuffer = new byte[1024];

                            /* Create File Stream */
                            FileStream   fStr    = new FileStream(path, FileMode.Open, FileAccess.Read);
                            BinaryReader bReader = new BinaryReader(fStr);
                            long         loopCnt = (long)(size / 1024 + 1);

                            try
                            {
                                /* Send File */
                                int reSize = 1024;
                                for (long i = 0; i < loopCnt; i++)
                                {
                                    if (i == loopCnt - 1)
                                    {
                                        reSize = (int)(size - (1024 * (loopCnt - 1)));
                                    }

                                    fileSendBuffer = bReader.ReadBytes(reSize);
                                    m_stream.Write(fileSendBuffer, 0, reSize);
                                    m_stream.Flush();

                                    /* Reset Array */
                                    for (int j = 0; j < reSize; j++)
                                    {
                                        fileSendBuffer[j] = 0;
                                    }
                                }

                                WriteLog(path + " 파일 전송 완료");
                            }
                            catch (Exception ex)
                            {
                                WriteLog("FileTransfer error " + ex.Message);
                            }
                            finally
                            {
                                fStr.Close();
                                bReader.Close();
                            }
                            break;
                        }

                        case (int)PacketType.exitConnection:
                        {
                            WriteLog("클라이언트 연결 해제");

                            /* Disconnection */
                            m_bConnect = false;
                            m_stream.Close();
                            break;
                        }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message + " 예외 발생으로 인해 서버를 종료합니다.");
                ServerStop();
                this.Invoke(new MethodInvoker(delegate()
                {
                    btnServer.Text      = "서버켜기";
                    btnServer.ForeColor = Color.Black;
                }));
                return;
            }
        }
예제 #4
0
 /// <summary>
 /// Raises the BeforeExpand event.
 /// </summary>
 public void OnBeforeExpand(EventArgs <TreeViewItem> e) => BeforeExpand?.Invoke(this, e);