void sendMessage() { while (connected) { lock (sendMsg) { while (sendMsg.Count > 0) { try { byte[] MsgToBeSent = sendMsg.Dequeue(); Console.WriteLine("Sending : " + Encoding.ASCII.GetString(MsgToBeSent)); sendDel printSendMsg = new sendDel(() => { peerUI.textBox4.Text = Encoding.ASCII.GetString(MsgToBeSent); }); peerUI.Invoke(printSendMsg); int byteSent = sender.Send(MsgToBeSent); // kirim ke tracker //Receive response from tracker byte[] bytes = new byte[1024]; int byteRec = sender.Receive(bytes); recvDel printRecvMsg = new recvDel(() => { peerUI.textBox5.Text = Encoding.ASCII.GetString(bytes); }); peerUI.Invoke(printRecvMsg); Message mSent = new Message(); mSent.parseMe(MsgToBeSent); Message m = new Message(); m.parseMe(bytes); if (m.msgCode == Message.START) { //start game here playStatus = 1; sendDel printCurrentRoom = new sendDel(() => { peerUI.Close(); }); peerUI.Invoke(printCurrentRoom); } else if (m.msgCode == Message.SUCCESS) { if (mSent.msgCode == Message.START) { //start game here playStatus = 2; sendDel printCurrentRoom = new sendDel(() => { peerUI.Close(); }); peerUI.Invoke(printCurrentRoom); } else if (mSent.msgCode == Message.CREATE_ROOM) { //create room success Console.WriteLine("Create Room Success"); //masukin ke myRoom myRoom = mSent.Rooms[0]; inRoom = true; //masuk ke room yang di create canQuit = false; Console.WriteLine("my Room : " + myRoom.getRoomID()); peerList.Add(this.PeerID); sendDel printCurrentRoom = new sendDel(() => { peerUI.textBox6.Text = myRoom.getRoomID(); }); peerUI.Invoke(printCurrentRoom); } else if (mSent.msgCode == Message.JOIN) { //join success inRoom = true; sendDel printCurrentRoom = new sendDel(() => { peerUI.textBox6.Text = joininRoom; }); peerUI.Invoke(printCurrentRoom); //Print Room Member del printRoomMember = new del(() => { for (int i = 0; i < peerList.Count; i++) { peerUI.richTextBox1.Text += peerList[i]; peerUI.richTextBox1.Text += "\n"; } }); peerUI.Invoke(printRoomMember); Console.WriteLine("Join Room Success"); //TO DO : koneksi dengan GameConnection } else if (mSent.msgCode == Message.KEEP_ALIVE) { //keep alive success Console.WriteLine("Keep Alive Success"); } else if (mSent.msgCode == Message.QUIT) { //quit success inRoom = false; sendDel printCurrentRoom = new sendDel(() => { peerUI.textBox6.Text = "-"; }); peerUI.Invoke(printCurrentRoom); Console.WriteLine("Quit Success"); } } else if (m.msgCode == Message.ROOM) { //get list room roomList.Clear(); roomList.AddRange(m.Rooms); Console.WriteLine("Room List : "); List <string> roomString = new List <string>(); del printRoomList = new del(() => { Console.WriteLine("Room List : "); for (int i = 0; i < roomList.Count; i++) { Console.WriteLine(roomList[i].getRoomID()); roomString.Add(roomList[i].getRoomID()); } peerUI.listBox1.DataSource = roomString; }); peerUI.Invoke(printRoomList); } else if (m.msgCode == Message.FAILED) { if (mSent.msgCode == Message.CREATE_ROOM) { //create room success Console.WriteLine("Create Room FAILED"); } else if (mSent.msgCode == Message.JOIN) { //join success Console.WriteLine("Join Room FAILED"); } else if (mSent.msgCode == Message.KEEP_ALIVE) { //keep alive success Console.WriteLine("Keep Alive FAILED"); } else if (mSent.msgCode == Message.QUIT) { //quit success Console.WriteLine("Quit FAILED"); } } else if (m.msgCode == 100) //Check if myRoom masih muat { Console.WriteLine("jumlah peer : " + peerList.Count()); Console.WriteLine("max player : " + myRoom.getMaxPlayer()); if (peerList.Count() < myRoom.getMaxPlayer()) { Console.WriteLine("Masih bisa join"); IPTable.Add(m.msgPeerID, m.IP); byte[] msg = Encoding.ASCII.GetBytes("GunbondGame00000000"); List <byte> byteList = new List <byte>(); byteList.AddRange(msg); byteList.Add(Message.SUCCESS); msg = byteList.ToArray(); int byteSent_ = sender.Send(msg); //Receive response from tracker byte[] join_msg = new byte[1024]; int byteRecs = sender.Receive(join_msg); Message m_ = new Message(); m_.parseMe(join_msg); if (m_.msgCode == Message.SUCCESS) { peerList.Add(m.msgPeerID); //Print Room Member del printRoomMember = new del(() => { Console.WriteLine("RoomMember : "); for (int i = 0; i < peerList.Count; i++) { peerUI.richTextBox1.Text += peerList[i]; peerUI.richTextBox1.Text += "\n"; Console.WriteLine(peerList[i]); } }); peerUI.Invoke(printRoomMember); } else { Console.WriteLine("Sudah join room lain"); } } else { Console.WriteLine("Room already full"); byte[] msg = Encoding.ASCII.GetBytes("GunbondGame00000000"); List <byte> byteList = new List <byte>(); byteList.AddRange(msg); byteList.Add(Message.FAILED); msg = byteList.ToArray(); int byteSent_ = sender.Send(msg); } } else if (m.msgCode == Message.QUIT) { peerList.Remove(m.msgPeerID); Console.WriteLine("Peer " + m.msgPeerID + " quit from your room. Boo!"); IPTable.Remove(m.msgPeerID); byte[] msg = Encoding.ASCII.GetBytes("GunbondGame00000000"); List <byte> byteList = new List <byte>(); byteList.AddRange(msg); byteList.Add(Message.SUCCESS); msg = byteList.ToArray(); int byteSent_ = sender.Send(msg); //Print Room Member del printRoomMember = new del(() => { Console.WriteLine("RoomMember : "); peerUI.richTextBox1.Text = ""; for (int i = 0; i < peerList.Count; i++) { peerUI.richTextBox1.Text += peerList[i]; peerUI.richTextBox1.Text += "\n"; Console.WriteLine(peerList[i]); } }); peerUI.Invoke(printRoomMember); } } catch (SocketException se) { Console.WriteLine(se.ToString()); if (se.SocketErrorCode == SocketError.ConnectionAborted) { connected = false; } } catch (Exception e) { Console.WriteLine(e.ToString()); } } } } }
void sendMessage() { while (connected) { lock (sendMsg) { while (sendMsg.Count > 0) { try { byte[] MsgToBeSent = sendMsg.Dequeue(); Console.WriteLine("Sending : " + Encoding.ASCII.GetString(MsgToBeSent)); sendDel printSendMsg = new sendDel(() => { peerUI.textBox4.Text = Encoding.ASCII.GetString(MsgToBeSent); }); peerUI.Invoke(printSendMsg); int byteSent = sender.Send(MsgToBeSent); // kirim ke tracker //Receive response from tracker byte[] bytes = new byte[1024]; int byteRec = sender.Receive(bytes); recvDel printRecvMsg = new recvDel(() => { peerUI.textBox5.Text = Encoding.ASCII.GetString(bytes); }); peerUI.Invoke(printRecvMsg); Message mSent = new Message(); mSent.parseMe(MsgToBeSent); Message m = new Message(); m.parseMe(bytes); if (m.msgCode == Message.START) { //start game here playStatus = 1; sendDel printCurrentRoom = new sendDel(() => { peerUI.Close(); }); peerUI.Invoke(printCurrentRoom); } else if (m.msgCode == Message.SUCCESS) { if (mSent.msgCode == Message.START) { //start game here playStatus = 2; sendDel printCurrentRoom = new sendDel(() => { peerUI.Close(); }); peerUI.Invoke(printCurrentRoom); } else if (mSent.msgCode == Message.CREATE_ROOM) { //create room success Console.WriteLine("Create Room Success"); //masukin ke myRoom myRoom = mSent.Rooms[0]; inRoom = true; //masuk ke room yang di create canQuit = false; Console.WriteLine("my Room : " + myRoom.getRoomID()); peerList.Add(this.PeerID); sendDel printCurrentRoom = new sendDel(() => { peerUI.textBox6.Text = myRoom.getRoomID(); }); peerUI.Invoke(printCurrentRoom); } else if (mSent.msgCode == Message.JOIN) { //join success inRoom = true; sendDel printCurrentRoom = new sendDel(() => { peerUI.textBox6.Text = joininRoom; }); peerUI.Invoke(printCurrentRoom); //Print Room Member del printRoomMember = new del(() => { for (int i = 0; i < peerList.Count; i++) { peerUI.richTextBox1.Text += peerList[i]; peerUI.richTextBox1.Text += "\n"; } }); peerUI.Invoke(printRoomMember); Console.WriteLine("Join Room Success"); //TO DO : koneksi dengan GameConnection } else if (mSent.msgCode == Message.KEEP_ALIVE) { //keep alive success Console.WriteLine("Keep Alive Success"); } else if (mSent.msgCode == Message.QUIT) { //quit success inRoom = false; sendDel printCurrentRoom = new sendDel(() => { peerUI.textBox6.Text = "-"; }); peerUI.Invoke(printCurrentRoom); Console.WriteLine("Quit Success"); } } else if (m.msgCode == Message.ROOM) { //get list room roomList.Clear(); roomList.AddRange(m.Rooms); Console.WriteLine("Room List : "); List<string> roomString = new List<string>(); del printRoomList = new del(() => { Console.WriteLine("Room List : "); for (int i = 0; i < roomList.Count; i++) { Console.WriteLine(roomList[i].getRoomID()); roomString.Add(roomList[i].getRoomID()); } peerUI.listBox1.DataSource = roomString; }); peerUI.Invoke(printRoomList); } else if (m.msgCode == Message.FAILED) { if (mSent.msgCode == Message.CREATE_ROOM) { //create room success Console.WriteLine("Create Room FAILED"); } else if (mSent.msgCode == Message.JOIN) { //join success Console.WriteLine("Join Room FAILED"); } else if (mSent.msgCode == Message.KEEP_ALIVE) { //keep alive success Console.WriteLine("Keep Alive FAILED"); } else if (mSent.msgCode == Message.QUIT) { //quit success Console.WriteLine("Quit FAILED"); } } else if (m.msgCode == 100) //Check if myRoom masih muat { Console.WriteLine("jumlah peer : " + peerList.Count()); Console.WriteLine("max player : " + myRoom.getMaxPlayer()); if (peerList.Count() < myRoom.getMaxPlayer()) { Console.WriteLine("Masih bisa join"); IPTable.Add(m.msgPeerID, m.IP); byte[] msg = Encoding.ASCII.GetBytes("GunbondGame00000000"); List<byte> byteList = new List<byte>(); byteList.AddRange(msg); byteList.Add(Message.SUCCESS); msg = byteList.ToArray(); int byteSent_ = sender.Send(msg); //Receive response from tracker byte[] join_msg = new byte[1024]; int byteRecs = sender.Receive(join_msg); Message m_ = new Message(); m_.parseMe(join_msg); if (m_.msgCode == Message.SUCCESS) { peerList.Add(m.msgPeerID); //Print Room Member del printRoomMember = new del(() => { Console.WriteLine("RoomMember : "); for (int i = 0; i < peerList.Count; i++) { peerUI.richTextBox1.Text += peerList[i]; peerUI.richTextBox1.Text += "\n"; Console.WriteLine(peerList[i]); } }); peerUI.Invoke(printRoomMember); } else { Console.WriteLine("Sudah join room lain"); } } else { Console.WriteLine("Room already full"); byte[] msg = Encoding.ASCII.GetBytes("GunbondGame00000000"); List<byte> byteList = new List<byte>(); byteList.AddRange(msg); byteList.Add(Message.FAILED); msg = byteList.ToArray(); int byteSent_ = sender.Send(msg); } } else if (m.msgCode == Message.QUIT) { peerList.Remove(m.msgPeerID); Console.WriteLine("Peer " + m.msgPeerID + " quit from your room. Boo!"); IPTable.Remove(m.msgPeerID); byte[] msg = Encoding.ASCII.GetBytes("GunbondGame00000000"); List<byte> byteList = new List<byte>(); byteList.AddRange(msg); byteList.Add(Message.SUCCESS); msg = byteList.ToArray(); int byteSent_ = sender.Send(msg); //Print Room Member del printRoomMember = new del(() => { Console.WriteLine("RoomMember : "); peerUI.richTextBox1.Text = ""; for (int i = 0; i < peerList.Count; i++) { peerUI.richTextBox1.Text += peerList[i]; peerUI.richTextBox1.Text += "\n"; Console.WriteLine(peerList[i]); } }); peerUI.Invoke(printRoomMember); } } catch (SocketException se) { Console.WriteLine(se.ToString()); if (se.SocketErrorCode == SocketError.ConnectionAborted) { connected = false; } } catch (Exception e) { Console.WriteLine(e.ToString()); } } } } }