/*2017-06-09추가 *한 획을 그을 때마다 저장한 그림을 서버에 전송해준다.*/ private void sendPictuer() { byte[] sendBuffer = new byte[1024 * 4]; ////////////////////////////// 그림 파일 정보 전송 ////////////////////////////// ClientDrawingInfo drawInfo = new ClientDrawingInfo(); drawInfo.Type = (int)PacketType.그림정보; drawInfo.clientNum = client_number; Packet.Serialize(drawInfo).CopyTo(sendBuffer, 0); stream.Write(sendBuffer, 0, sendBuffer.Length); stream.Flush(); ////////////////////////////// 그림 파일 전송 ////////////////////////////// ClientDrawing draw = new ClientDrawing(); FileInfo file = new FileInfo(Environment.CurrentDirectory + "\\UserImage\\" + client_number + "Turn.jpg"); FileStream fileStrm = new FileStream(file.FullName, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(fileStrm); int count = (int)(file.Length) / (1024) + 1; for (int i = 0; i < count; i++) { draw.buffer = reader.ReadBytes(1024); draw.count = count; draw.Type = (int)PacketType.그림; draw.clientNum = client_number; Packet.Serialize(draw).CopyTo(sendBuffer, 0); stream.Write(sendBuffer, 0, sendBuffer.Length); this.stream.Flush(); resetBuffer(sendBuffer); } fileStrm.Close(); ////////////////////////////////////////////////////////////////////////////////// }
/* 2017년 6월 9일에 추가 * 실시간 전송 구현중. * 그림을 그리고 있는 클라이언트에게 그림을 받아 모든 클라이언트에게 전송*/ public void SendDrawAll(int cli_num) { //모든 클라이언트 리스트를 탐색 string fileName = null; for (int i = 0; i < clients.Count; i++) { if (i != cli_num) { ClientDrawing draw = new ClientDrawing(); ClientDrawingInfo drawInfo = new ClientDrawingInfo(); byte[] sendBuffer = new byte[1024 * 4]; NetworkStream stream = clients[i].GetStream(); //그림 정보보내기 drawInfo.Type = (int)PacketType.그림정보; drawInfo.clientNum = cli_num; //누가 보낸 그림인지 Packet.Serialize(drawInfo).CopyTo(sendBuffer, 0); stream.Write(sendBuffer, 0, sendBuffer.Length); stream.Flush(); resetBuffer(sendBuffer); //그림 보내기 fileName = Environment.CurrentDirectory + "\\Server\\bin\\Debug\\" + cli_num + "turn.jpg"; draw.Type = (int)PacketType.그림; FileInfo file = new FileInfo(fileName); FileStream fileStrm = new FileStream(file.FullName, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(fileStrm); int count = (int)(file.Length) / (1024) + 1; for (int j = 0; j < count; j++) { draw.buffer = reader.ReadBytes(1024); draw.count = count; draw.clientNum = cli_num; Packet.Serialize(draw).CopyTo(sendBuffer, 0); stream.Write(sendBuffer, 0, sendBuffer.Length); stream.Flush(); resetBuffer(sendBuffer); } fileStrm.Close(); } } }
//////////////////////////////////////////////////// 서버와의 통신 //////////////////////////////////////////////////// private void GetRequest() //서버로부터 받은 요청을 처리하는 쓰레드 메소드 { while (true) { stream = clientSocket.GetStream(); //스트림 읽음 int bytes = stream.Read(this.readbuffer, 0, this.readbuffer.Length); Packet packet = (Packet)Packet.Deserialize(this.readbuffer); switch ((int)packet.Type) { case (int)PacketType.메세지: { ClientMessage clientMessage = new ClientMessage(); clientMessage = (ClientMessage)Packet.Deserialize(this.readbuffer); DisplayText(clientMessage.Message, Color.Black); //텍스트창에 띄움 break; } case (int)PacketType.그림정보: { ClientDrawingInfo drawInfo = new ClientDrawingInfo(); drawInfo = (ClientDrawingInfo)Packet.Deserialize(this.readbuffer); //로컬에서 테스트할때 이름 안 겹치게하려고.. fileStrm = new FileStream(filepath + "turnPic_" + client_number + ".jpg", FileMode.Create, FileAccess.Write); break; } case (int)PacketType.그림: { ClientDrawing draw = new ClientDrawing(); draw = (ClientDrawing)Packet.Deserialize(this.readbuffer); string fileName = null; //클라이언트로부터 받은 파일을 쓴다. if (fileStrm != null) { fileStrm.Write(draw.buffer, 0, draw.buffer.Length); countRead++; if (countRead == draw.count) { fileName = fileStrm.Name; fileStrm.Close(); fileStrm = null; countRead = 0; //picture박스에 그림표시해야함. FileStream fs = new FileStream(filepath + "turnPic_" + client_number + ".jpg", FileMode.Open, FileAccess.Read); gamePicture.Image = System.Drawing.Image.FromStream(fs); fs.Close(); } } break; } case (int)PacketType.프로필정보: { ClientProfileInfo profileInfo = new ClientProfileInfo(); profileInfo = (ClientProfileInfo)Packet.Deserialize(this.readbuffer); fileStrm = new FileStream(filepath + client_number.ToString() + "-" + profileInfo.clientNum.ToString() + ".jpg", FileMode.Create, FileAccess.Write); break; } case (int)PacketType.게임상태: { GameState statePacket = new GameState(); statePacket = (GameState)Packet.Deserialize(this.readbuffer); DisplayText(statePacket.message, Color.Red); gameStart = statePacket.gameStart; if (!gameStart) { settingTurnChange(-1); System.Threading.Thread.Sleep(2000); Next_Form(); } break; } case (int)PacketType.턴: { TurnChange turnPacket = new TurnChange(); turnPacket.turnCount = turnCount; turnPacket = (TurnChange)Packet.Deserialize(this.readbuffer); turnReceived(turnPacket.turn, turnPacket.Word, turnPacket.message, turnPacket.clientscore); break; } case (int)PacketType.프로필: { ClientProfile profile = new ClientProfile(); profile = (ClientProfile)Packet.Deserialize(this.readbuffer); string fileName = null; //클라이언트로부터 받은 파일을 쓴다. if (fileStrm != null) { fileStrm.Write(profile.buffer, 0, profile.buffer.Length); countRead++; if (countRead == profile.count) { fileName = fileStrm.Name; fileStrm.Close(); fileStrm = null; countRead = 0; if (profile.clientNum > client_number) { switch (profile.clientNum) { case 1: this.Invoke(new MethodInvoker(delegate() { nickNameList.Add(profile.nickName); })); break; case 2: this.Invoke(new MethodInvoker(delegate() { nickNameList.Add(profile.nickName); })); break; case 3: this.Invoke(new MethodInvoker(delegate() { nickNameList.Add(profile.nickName); })); break; } } setProfile(profile.clientNum); } } break; } case (int)PacketType.초기화: { Init init = new Init(); init = (Init)Packet.Deserialize(this.readbuffer); client_number = init.clinetNumber; //자신의 클라이언트 넘버 초기화 break; } case (int)PacketType.닉네임: //6.13 초기화랑 닉네임 바꿈 -> initReceive함수 옮김 { NickName nick_name = new NickName(); nick_name = (NickName)Packet.Deserialize(this.readbuffer); initReceived(nick_name.nickNameList, nick_name.client_sum); break; } } } }
private void OnRequest(int requestType, byte[] readBuffer, int cli_num) { //클라이언트의 리퀘스트 타입을 식별하여 타입에 알맞게 처리 switch (requestType) { case (int)PacketType.메세지: { ClientMessage clientMessage = new ClientMessage(); clientMessage = (ClientMessage)Packet.Deserialize(readBuffer); msgReceived(clientMessage.Message, clientMessage.nickName); break; } case (int)PacketType.프로필: { ClientProfile clientProfile = new ClientProfile(); clientProfile = (ClientProfile)Packet.Deserialize(readBuffer); profileReceived(clientProfile.nickName, clientProfile.buffer, clientProfile.count, cli_num); break; } case (int)PacketType.게임상태: { GameState statePacket = new GameState(); statePacket = (GameState)Packet.Deserialize(readBuffer); stateReceived(statePacket.gameStart); break; } case (int)PacketType.랭킹: { Ranking rank = new Ranking(); rank = (Ranking)Packet.Deserialize(readBuffer); SendScoreAll(); break; } case (int)PacketType.정답: { TurnChange turnPacket = new TurnChange(); turnPacket = (TurnChange)Packet.Deserialize(readBuffer); DisplayText("Turn Change" + turnPacket.turn.ToString()); answerReceived(turnPacket.turn, turnPacket.score, turnPacket.message, turnPacket.clientNum); break; } case (int)PacketType.그림정보: { ClientDrawingInfo drawinfo = new ClientDrawingInfo(); drawinfo = (ClientDrawingInfo)Packet.Deserialize(readBuffer); DisplayText(drawinfo.clientNum + "의 그림 도착"); fileStrm = new FileStream(Environment.CurrentDirectory + "\\Server\\bin\\Debug\\" + drawinfo.clientNum + "turn.jpg" , FileMode.Create, FileAccess.Write); break; } case (int)PacketType.그림: { ClientDrawing draw = new ClientDrawing(); draw = (ClientDrawing)Packet.Deserialize(readBuffer); drawReceived(draw.buffer, draw.count, draw.clientNum); break; } } }