static void FloodMessage(NetworkStream[] streams, object streamLock, Message msg) { Message floodingMsg = null; switch (msg.header.MSGTYPE) { case CONSTANT.CHATTING: floodingMsg = makeFloodingMessage(msg); //chatting을 flooding하는 메세지를 만듦 break; case CONSTANT.FLOODING: floodingMsg = msg; //Do nothing. break; case CONSTANT.NOTICE: //아직은 NOTICE Message의 종류가 flooding 할 Message밖에 없음. 추후에 기능 추가 시 수정 요. floodingMsg = msg; //Do nothing. 특정 client에게 보낼 Notice의 경우는 다른 방식으로 처리. break; } lock (streamLock) { for (int i = 0; i < 8; i++) { if (streams[i] != null) { ChattingMessageUtil.Send(streams[i], floodingMsg); } } } }
async void recieveMessage(NetworkStream stream) { try { while (true) { msg = ChattingMessageUtil.Recieve(stream); if (msg == null) { continue; } if (msg.header.MSGTYPE == CONSTANT.FLOODING) { string chat = Encoding.Default.GetString(msg.body.GetBytes()); appendText(tbxChattingLog, chat); } else if (msg.header.MSGTYPE == CONSTANT.NOTICE) { string notice = Encoding.Default.GetString(msg.body.GetBytes()); appendText(tbxChattingLog, notice); } } } catch (SocketException) { stream.Close(); client.Close(); } catch (IOException) { stream.Close(); client.Close(); } }
static async void connectClient(TcpListener server, NetworkStream[] streams, object streamLock, int index) { while (true) { TcpClient client = server.AcceptTcpClient(); lock (streamLock) { streams[index] = client.GetStream(); } Message msg = default(Message); string nickname = null; try { //잘못된 접속 시도. client 프로그램에 문제가 있는 경우. if ((msg = ChattingMessageUtil.Recieve(streams[index])) == null || msg.header.MSGTYPE != CONSTANT.NOTICE) { streams[index].Close(); client.Close(); return; } //새로운 사용자가 입장했다는 Wellcome Notice Message flooding nickname = Encoding.Default.GetString(msg.body.GetBytes()); string wellcome = String.Format("[{0}:{1}:{2}]Server: {3}님이 입장하셨습니다.", DateTime.Now.Hour.ToString(), DateTime.Now.Minute.ToString(), DateTime.Now.Second.ToString(), nickname); FloodMessage(streams, streamLock, makeNoticeMessage(wellcome)); //Chatting Start while ((msg = ChattingMessageUtil.Recieve(streams[index])) != null) { if (msg.header.MSGTYPE == CONSTANT.CHANGE_NICKNAME) { FloodMessage(streams, streamLock, makeChangeNicknameNoticeMessage(msg, ref nickname)); } else { FloodMessage(streams, streamLock, msg); } } } catch (SocketException) { } catch (IOException) { } catch (ObjectDisposedException) { } finally { streams[index].Close(); client.Close(); streams[index] = null; string gooodbye = String.Format("[{0}:{1}:{2}]Server: {3}님이 퇴장하셨습니다.", DateTime.Now.Hour.ToString(), DateTime.Now.Minute.ToString(), DateTime.Now.Second.ToString(), nickname); FloodMessage(streams, streamLock, makeNoticeMessage(gooodbye)); } } }
void sendMessage(NetworkStream stream, ChattingMessage.Message msg) { try { ChattingMessageUtil.Send(stream, msg); } catch (SocketException) { stream.Close(); client.Close(); Application.Exit(); } catch (IOException) { stream.Close(); client.Close(); Application.Exit(); } catch (ObjectDisposedException) { appendText(tbxChattingLog, "[전송 실패: 서버와의 연결 종료됨]"); } }