private void button_unlock_Click(object sender, EventArgs e) { this.savePptNum = 0; this.savepageNum = ppt[0].ActiveWindow.Selection.SlideRange.SlideNumber; //save와lock다른경우 if ((this.savepageNum != this.lockPageNum) || (this.savePptNum != this.lockPptNum)) { MessageBox.Show(lockPageNum + "의 slide를 수정완료먼저해주세요"); } else { string _Path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); presentation[this.savePptNum].Save(); PowerPoint.Application tempPpt = new PowerPoint.Application(); PowerPoint.Presentation tempPresentation = tempPpt.Presentations.Add(MsoTriState.msoFalse); PowerPoint.Slides tempSlides = tempPresentation.Slides; tempSlides.InsertFromFile(ButtonPPT[this.savePptNum].Tag.ToString(), 0, this.savepageNum, this.savepageNum); saveFileName = _Path + @"\" + "slide.pptx"; FileInfo fileInfo = new FileInfo(saveFileName); if (fileInfo.Exists == true) { File.Delete(this.saveFileName); } tempPresentation.SaveAs(_Path + @"\" + "slide"); this.askSave = true; } }
private void savePage(int pptNum) { savePptNum = pptNum; savePageNum = ppt[pptNum].ActiveWindow.Selection.SlideRange.SlideNumber; //save와lock다른경우 if ((savePageNum != lockPageNum) || (savePptNum != lockPptNum)) { MessageBox.Show(lockPageNum + "의 slide를 수정완료먼저해주세요"); return; } try { //save slide복사해서 새로운피피티 생성 presentation[pptNum].Save(); PowerPoint.Application tempPpt = new PowerPoint.Application(); PowerPoint.Presentation tempPresentation = tempPpt.Presentations.Add(MsoTriState.msoFalse); PowerPoint.Slides tempSlides = tempPresentation.Slides; tempSlides.InsertFromFile(ButtonPPT[pptNum].Tag.ToString(), 0, savePageNum, savePageNum); tempPresentation.SaveAs(_namePath + @"\" + "slide"); FileInfo file = new FileInfo(_namePath + @"\" + "slide.pptx"); //savePacket Console.WriteLine("client : save"); byte[] buffer = new byte[1024 * 4]; savePacket = new SavePacket(); savePacket.type = (int)PacketType.SAVE; savePacket.pptNum = savePptNum; savePacket.pageNum = savePageNum; savePacket.isSave = true; savePacket.fileSize = file.Length; if (presentation[pptNum].Slides.Count == slideCnt[pptNum]) //추가된슬라이드인가 { savePacket.isAdd = false; } else { savePacket.isAdd = true; } slideCnt[pptNum] = presentation[pptNum].Slides.Count; Packet.Serialize(savePacket).CopyTo(buffer, 0); stream.Write(buffer, 0, buffer.Length); //새로운피피티 server로보냄 FileStream fs = file.OpenRead(); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); stream.Write(bytes, 0, bytes.Length); tempPresentation.Close(); fs.Close(); stream.Flush(); File.Delete(_namePath + @"\" + "slide.pptx"); } catch (Exception e) { Console.WriteLine("savePage() 에러 : " + e.Message); } }
private void Socket_C() { Console.WriteLine("**** Socket_C ****"); while (true) { string filename; if (stream.CanRead && stream.CanWrite) { ///서버로부터 전송된 data 읽음 byte[] ReadByte; ReadByte = new byte[client.ReceiveBufferSize]; int BytesRead = stream.Read(ReadByte, 0, (int)ReadByte.Length); string str = Encoding.UTF8.GetString(ReadByte); filename = Encoding.GetEncoding("utf-8").GetString(ReadByte, 0, BytesRead); ///server와 연결되있는client의 name들 리스트뷰에 등록 if (str.Substring(0, 5) == "NAME#") { Console.WriteLine("add servername " + str); string[] names = str.Split('/'); for (int i = 1; i < names.Length - 1; i++) { ListViewItem li = new ListViewItem(); li.Text = names[i]; li.SubItems.Add(""); li.SubItems.Add(""); Invoke((MethodInvoker) delegate { listView1.Items.Add(li); }); } } ///추가로 연결되는 client의 name 리스트뷰에 등록 else if (str.Substring(0, 5) == "NAME*") { ListViewItem li = new ListViewItem(); li.Text = str.Substring(5, str.Length - 5); li.SubItems.Add(""); li.SubItems.Add(""); Invoke((MethodInvoker) delegate { listView1.Items.Add(li); }); } ///다른 클라이언트의 lock값 변경 else if (str.Substring(0, 7) == "CHANGE@") { string[] info = str.Split('/'); string name = info[1]; string pptnum = info[2]; string pagenum = info[3]; Invoke((MethodInvoker) delegate { for (int i = 0; i < listView1.Items.Count; i++) { ListViewItem item = listView1.Items[i]; bool isContains = item.SubItems[0].Text.Contains(name); if (isContains) { item.SubItems[1].Text = pptnum; item.SubItems[2].Text = pagenum; break; } } }); } ///lock실패했을경우 else if (str.Substring(0, 5) == "FAIL@") { MessageBox.Show("다른사용자가 편집중인 슬라이드입니다"); } ///다른 사용자가 피피티를 편집했을 경우 else if (str.Substring(0, 5) == "EDIT@") { string[] info = str.Split('/'); int fileSize = Convert.ToInt32(info[1]); int pptnum = Convert.ToInt32(info[2]); int pagenum = Convert.ToInt32(info[3]); bool isadd; if (info[4].CompareTo("True") == 0) { isadd = true; } else { isadd = false; } //변경할슬라이드가있는 피피티파일을 읽어와 'edit.pptx'생성후 저장 byte[] myReadBuffer = new byte[fileSize]; int numberOfBytesRead = 0; string path = _namePath + @"\" + "edit.pptx"; FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length); fs.Write(myReadBuffer, 0, numberOfBytesRead); fs.Close(); PowerPoint.Slides tempSlides = presentation[pptnum].Slides; if (isadd) { tempSlides.InsertFromFile(path, pagenum - 1, 1, 1); } else { tempSlides.InsertFromFile(path, pagenum, 1, 1); tempSlides[pagenum].Delete(); } File.Delete(path); } else if (str.Substring(0, 7) == "UPLOAD@") { if (filename != "") { ///upload시작한다고 server에게 전달 ///packetType = upload byte[] buffer = new byte[1024 * 4]; uploadPacket = new UploadPacket(); uploadPacket.type = (int)PacketType.UPLOAD; uploadPacket.isup = true; Packet.Serialize(uploadPacket).CopyTo(buffer, 0); stream.Write(buffer, 0, buffer.Length); Console.WriteLine("uploadpacket"); _namePath = _Path + @"\" + textBox_name.Text; filename = filename.Substring(7, filename.Length - 7); Byte[] sendBytes = Encoding.GetEncoding("utf-8").GetBytes(_namePath + @"\" + filename); stream.Write(sendBytes, 0, sendBytes.Length); int ByteSize = 0; Byte[] FileSizeBytes = new byte[client.ReceiveBufferSize]; ByteSize = stream.Read(FileSizeBytes, 0, FileSizeBytes.Length); int MaxFileLength = Convert.ToInt32(Encoding.UTF8.GetString(FileSizeBytes, 0, ByteSize)); ///전송준비작업을 완료했다고 서버에 전해줌 byte[] ReadyTransBytes = new byte[client.ReceiveBufferSize]; ReadyTransBytes = Encoding.UTF8.GetBytes("READY"); stream.Write(ReadyTransBytes, 0, ReadyTransBytes.Length); System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(_namePath); if (di.Exists == false) { di.Create(); } FileStream fs = new FileStream(_namePath + @"\" + filename, FileMode.Create, FileAccess.ReadWrite, FileShare.None); if (filename != string.Empty) { byte[] myReadBuffer = new byte[1024]; int numberOfBytesRead = 0; do { numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length); fs.Write(myReadBuffer, 0, numberOfBytesRead); }while (fs.Length < MaxFileLength); //while (stream.DataAvailable); } fs.Flush(); fs.Close(); stream.Flush(); Invoke((MethodInvoker) delegate { ButtonPPT[IdxPPT].Visible = true; ButtonPPT[IdxPPT].Enabled = true; ButtonPPT[IdxPPT].Tag = _namePath + @"\" + filename; LabelPPT[IdxPPT].Visible = true; LabelPPT[IdxPPT].Text = Path.GetFileNameWithoutExtension(filename); IdxPPT++; }); } } } } }
////각 ppt의 locking 제어/// private void checkLock() { while (true) { for (int i = 0; i < nClient; i++) { string name, pptN; int pptNum, pageNum; ///lock을 요청한 클라이언트 if (clientList[i].askLock == true) { Console.WriteLine(i + ": " + clientList[i].lockPageNum); name = clientList[i].name; pptN = LabelPPT[clientList[i].lockPptNum].Text; pptNum = clientList[i].lockPptNum; pageNum = clientList[i].lockPageNum; if (pptLockInfo[pptNum][pageNum] == -1)//해당 ppt의 page가 unlock일 경우 { pptLockInfo[pptNum][pageNum] = i; clientList[i].askLock = false; this.ChangeList(name, pptN, pageNum); for (int j = 0; j < nClient; j++) { if (clientList[j].isConnect) { clientList[j].ChangeList(name, pptN, pageNum.ToString()); } } } else //해당 ppt의 page가 lock일 경우 초기화 { clientList[i].askLock = false; clientList[i].lockPageNum = -1; clientList[i].lockPptNum = -1; clientList[i].LockFail(); } } if (this.askLock == true) //서버자신이 lock을 요청했을 경우 { Console.WriteLine(this.name + ": " + this.lockPageNum); name = this.name; pptN = LabelPPT[this.lockPptNum].Text; pptNum = this.lockPptNum; pageNum = this.lockPageNum; if (pptLockInfo[pptNum][pageNum] == -1)//해당 ppt의 page가 unlock일 경우 { pptLockInfo[pptNum][pageNum] = i; this.askLock = false; this.ChangeList(name, pptN, pageNum); for (int j = 0; j < nClient; j++) { if (clientList[j].isConnect) { clientList[j].ChangeList(name, pptN, pageNum.ToString()); } } } else //해당 ppt의 page가 lock일 경우 초기화 { this.askLock = false; this.lockPageNum = -1; this.lockPptNum = -1; MessageBox.Show("다른사용자가 편집중인 슬라이드입니다"); } } ///save요청한클라이언트 if (clientList[i].askSave == true) { int pptnum = clientList[i].savePptNum; int pagenum = clientList[i].savePageNum; Console.WriteLine("client -> server : askSave"); //int curSlideIdx = ppt[savePptNum].ActiveWindow.Selection.SlideRange.SlideNumber; PowerPoint.Slides tempSlides = presentation[pptnum].Slides; if (clientList[i].isAddSlide) { tempSlides.InsertFromFile(clientList[i].saveFileName, pagenum - 1, 1, 1); } else { tempSlides.InsertFromFile(clientList[i].saveFileName, pagenum, 1, 1); tempSlides[pagenum].Delete(); } ChangeList(clientList[i].name, "", -1); clientList[i].ChangeList(clientList[i].name, "", ""); //다른클라이언트들에게변경된파일을보냄 for (int j = 0; j < nClient; j++) { if (i == j) { ; } else { if (clientList[j].isConnect) { clientList[j].SendSaveFile(clientList[i].saveFileName, pptnum, pagenum, clientList[i].isAddSlide); clientList[j].ChangeList(clientList[i].name, "", ""); } } } clientList[i].lockPageNum = -1; clientList[i].lockPptNum = -1; pptLockInfo[pptnum][pagenum] = -1; //save후 lock info배열에서 lock해제 clientList[i].askSave = false; } if (this.askSave) //서버가 save요청했을 경우 { int pptnum = this.savePptNum; int pagenum = this.savepageNum; PowerPoint.Slides tempSlides = presentation[pptnum].Slides; if (this.isAddSlide) { if (pagenum == 1) { tempSlides.InsertFromFile(clientList[i].saveFileName, pagenum, 1, 1); } else { tempSlides.InsertFromFile(clientList[i].saveFileName, pagenum - 1, 1, 1); } } else { tempSlides.InsertFromFile(this.saveFileName, pagenum, 1, 1); tempSlides[pagenum].Delete(); } ChangeList(this.name, "", -1); //다른클라이언트들에게 변경된파일을보냄 for (int j = 0; j < nClient; j++) { if (clientList[j].isConnect) { clientList[j].ChangeList(this.name, "", ""); clientList[j].SendSaveFile(this.saveFileName, pptnum, pagenum, this.isAddSlide); } } this.lockPageNum = -1; this.lockPptNum = -1; pptLockInfo[pptnum][pagenum] = -1; //save후 lock info배열에서 lock해제 this.askSave = false; } } } }