public Peer(PeerUI peerUI) { this.peerUI = peerUI; playStatus = 0; sendMsg = new Queue<byte[]>(); receivedMsg = new Queue<byte[]>(); roomList = new List<Room>(); peerList = new List<String>(); myRoom = new Room(); keepAliveThread = new Thread(KeepAlive); keepAliveThread.Name = "Keep Alive Thread"; senderThread = new Thread(sendMessage); senderThread.Name = "Sender Thread"; //processThread = new Thread(processMessage); IPTable = new Dictionary<string, string>(); }
/// <summary> /// Parsing byte[] to Message Type /// </summary> /// <param name="iMsg">byte[] message</param> public void parseMe(byte[] iMsg) { if (validMsg(iMsg)) { msgCode = iMsg[19]; Console.WriteLine(msgCode.ToString()); if (msgCode != 127 || msgCode != 128 || msgCode != 200) { // bukan message sukses, gagal, atau room if ((msgCode == 135 && iMsg.Length == 24) || msgCode == 182 || msgCode == 254 || msgCode == 235) // Handshake response, keep alive, list, quit { msgPeerID = Encoding.ASCII.GetString(SubBytes(iMsg, 20, 4)); } else if (msgCode == 253 || msgCode == 252) { // message join dan start msgPeerID = Encoding.ASCII.GetString(SubBytes(iMsg, 20, 4)); dummyCreateRoom = new Room(); dummyCreateRoom.setRoomID(SubBytes(iMsg,24, 50)); Rooms.Clear(); Rooms.Add(dummyCreateRoom); } else if (msgCode == 255) { // message create room msgPeerID = Encoding.ASCII.GetString(SubBytes(iMsg, 20, 4)); dummyCreateRoom = new Room(); dummyCreateRoom.setPeerID(SubBytes(iMsg, 20, 4)); dummyCreateRoom.setMaxPlayer(iMsg[24]); dummyCreateRoom.setRoomID(SubBytes(iMsg, 25, 50)); Rooms.Clear(); Rooms.Add(dummyCreateRoom); } else if (msgCode == 200) { // message room iterate = iMsg[20]; Rooms.Clear(); for (int i = 0; i < iterate ; i++) { dummyCreateRoom = new Room(); dummyCreateRoom.setPeerID(SubBytes(iMsg, (21 + (i * 55)), 4)); dummyCreateRoom.setMaxPlayer(iMsg[(25 + (i * 55))]); dummyCreateRoom.setRoomID(SubBytes(iMsg, (26 + (i * 55)), 50)); Console.WriteLine("Msg : " + Encoding.ASCII.GetString(SubBytes(iMsg, (26 + (i * 55)), 50))); Rooms.Add(dummyCreateRoom); } } else if (msgCode == 100) { msgPeerID = Encoding.ASCII.GetString(SubBytes(iMsg, 20, 4)); Console.WriteLine("message Length : " + iMsg.Count()); IP = Encoding.ASCII.GetString(SubBytes(iMsg,24,iMsg.Count()-24)); IP = IP.Trim(); IP = IP.Substring(0, IP.IndexOf('\0')); Console.WriteLine(IP); } } } else { Console.WriteLine("Message Tidak Valid"); } }
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()); } } } } }