def chatListener(): global chatSocket templog = [] while True: packet = tcp_packet_pb2.TcpPacket() data = chatSocket.recv(1024) data1 = data packet.ParseFromString(data1) if packet.type == 0: packet = tcp_packet_pb2.TcpPacket.DisconnectPacket() packet.ParseFromString(data) if packet.update == 0: print(packet.player.name, " has disconnected!") templog.append(packet.player.name + " has disconnected!") elif packet.update == 1: print(packet.player.name, " has lost connection!") templog.append(packet.player.name + " has lost connection!") elif packet.type == 1: packet = tcp_packet_pb2.TcpPacket.ConnectPacket() packet.ParseFromString(data) print(packet.player.name, " successfully connected to lobby ", packet.lobby_id) templog.append(packet.player.name + " successfully connected to lobby " + packet.lobby_id) elif packet.type == 2: packet = tcp_packet_pb2.TcpPacket.CreateLobbyPacket() packet.ParseFromString(data) print("Lobby ", packet.lobby_id, " successfully created!") templog.append("Lobby " + packet.lobby_id + " successfully created!") elif packet.type == 3: packet = tcp_packet_pb2.TcpPacket.ChatPacket() packet.ParseFromString(data) print(packet.player.name, ": ", packet.message) templog.append(packet.player.name + ": " + packet.message) elif packet.type == 4: packet = tcp_packet_pb2.TcpPacket.PlayerListPacket() packet.ParseFromString(data) print(packet.player_list) templog.append(packet.player_list) elif packet.type == 5: packet = tcp_packet_pb2.TcpPacket.ErrLdnePacket() packet.ParseFromString(data) print("Error: ", packet.err_message) templog.append("Error: " + packet.err_message) elif packet.type == 6: packet = tcp_packet_pb2.TcpPacket.ErrLfullPacket() packet.ParseFromString(data) print("Error: ", packet.err_message) templog.append("Error: " + packet.err_message) elif packet.type == 7: packet = tcp_packet_pb2.TcpPacket.ErrPacket() packet.ParseFromString(data) print("Error: ", packet.err_message) templog.append("Error: " + packet.err_message) updateChatLog(templog)
def receivePackets(sock, player): global objectToDraw, turn, winner, nextPort, scores, addrList tcpPacket = tcp.TcpPacket() chatPacket = tcp.TcpPacket.ChatPacket() connectPacket = tcp.TcpPacket.ConnectPacket() disconnectPacket = tcp.TcpPacket.DisconnectPacket() while True: data = sock.recv(1024) errCheck(data) tcpPacket.ParseFromString(data) if tcpPacket.type == tcp.TcpPacket.CHAT: chatPacket.ParseFromString(data) if chatPacket.player == player: chatarea.configure(state='normal') chatarea.insert(tk.END, 'You: ' + chatPacket.message + '\n') chatarea.configure(state='disabled') else: chatarea.configure(state='normal') chatarea.insert( tk.END, chatPacket.player.name + ' : ' + chatPacket.message + '\n') chatarea.configure(state='disabled') if (chatPacket.message and objectToDraw and chatPacket.message.lower() == objectToDraw.lower() and not (turn == chatPacket.player) and not winner): winner = chatPacket.player elif tcpPacket.type == tcp.TcpPacket.CONNECT: connectPacket.ParseFromString(data) playerList.append(connectPacket.player) chatarea.configure(state='normal') chatarea.insert( tk.END, connectPacket.player.name + ' joined the lobby! \n') chatarea.configure(state='disabled') scores[connectPacket.player.id] = { 'name': connectPacket.player.name, 'score': 0 } currentPlayers(sock) elif tcpPacket.type == tcp.TcpPacket.DISCONNECT: disconnectPacket.ParseFromString(data) playerList.remove(disconnectPacket.player) if disconnectPacket.update == tcp.TcpPacket.DisconnectPacket.NORMAL: chatarea.configure(state='normal') chatarea.insert( tk.END, disconnectPacket.player.name + ' disconnected from the lobby! \n') chatarea.configure(state='disabled') currentPlayers(sock) else: chatarea.configure(state='normal') chatarea.insert( tk.END, disconnectPacket.player.name + ' lost connection to the lobby! \n') chatarea.configure(state='disabled') currentPlayers(sock)
def receive_msg(): while True: chat_data = client_socket.recv(1024) tcp_packet = tcp_packet_pb2.TcpPacket() tcp_packet.ParseFromString(chat_data) if tcp_packet.type == tcp_packet_pb2.TcpPacket.CHAT: chat_packet = tcp_packet_pb2.TcpPacket.ChatPacket() chat_packet.ParseFromString(chat_data) player_name = chat_packet.player.name msg = chat_packet.message print(player_name,":", msg)
def errCheck(data): tcpPacket = tcp.TcpPacket() tcpPacket.ParseFromString(data) if tcpPacket.type == tcp.TcpPacket.ERR_LDNE: errLdnePacket = tcp.TcpPacket.ErrLdnePacket() errLdnePacket.ParseFromString(data) print('Error:', errLdnePacket.err_message) exit() elif tcpPacket.type == tcp.TcpPacket.ERR_LFULL: errLfullPacket = tcp.TcpPacket.ErrLfullPacket() errLfullPacket.ParseFromString(data) print('Error:', errLfullPacket.err_message) exit() elif tcpPacket.type == tcp.TcpPacket.ERR: errPacket = tcp.TcpPacket.ErrPacket() errPacket.ParseFromString(data) print('Error:', errPacket.err_message) exit()
def chatListenerGUI(): global chat_log while True: packet = tcp_packet_pb2.TcpPacket() data = chatSocket.recv(1024) data1 = data packet.ParseFromString(data1) if packet.type == 0: packet = tcp_packet_pb2.TcpPacket.DisconnectPacket() packet.ParseFromString(data) if packet.update == 0: chat_log = chat_log + '\n' + packet.player.name + " has disconnected!" elif packet.update == 1: chat_log = chat_log + '\n' + packet.player.name + " has lost connection!" elif packet.type == 1: packet = tcp_packet_pb2.TcpPacket.ConnectPacket() packet.ParseFromString(data) chat_log = chat_log + '\n' + packet.player.name + " successfully connected to lobby " + packet.lobby_id elif packet.type == 2: packet = tcp_packet_pb2.TcpPacket.CreateLobbyPacket() packet.ParseFromString(data) chat_log = chat_log + '\n' + "Lobby " + packet.lobby_id + " successfully created!" elif packet.type == 3: packet = tcp_packet_pb2.TcpPacket.ChatPacket() packet.ParseFromString(data) chat_log = chat_log + '\n' + packet.player.name + ": " + packet.message elif packet.type == 4: packet = tcp_packet_pb2.TcpPacket.PlayerListPacket() packet.ParseFromString(data) chat_log = chat_log + '\n' + packet.player_list elif packet.type == 5: packet = tcp_packet_pb2.TcpPacket.ErrLdnePacket() packet.ParseFromString(data) chat_log = chat_log + '\n' + "Error: " + packet.err_msg elif packet.type == 6: packet = tcp_packet_pb2.TcpPacket.ErrLfullPacket() packet.ParseFromString(data) chat_log = chat_log + '\n' + "Error: " + packet.err_msg elif packet.type == 7: packet = tcp_packet_pb2.TcpPacket.ErrPacket() packet.ParseFromString(data) chat_log = chat_log + '\n' + "Error: " + packet.err_msg
def receivePackets(sock, player): tcpPacket = tcp.TcpPacket() chatPacket = tcp.TcpPacket.ChatPacket() connectPacket = tcp.TcpPacket.ConnectPacket() disconnectPacket = tcp.TcpPacket.DisconnectPacket() while True: data = sock.recv(1024) errCheck(data) tcpPacket.ParseFromString(data) if tcpPacket.type == tcp.TcpPacket.CHAT: chatPacket.ParseFromString(data) if chatPacket.player == player: chatarea.configure(state = 'normal') chatarea.insert(tk.END, 'You: ' + chatPacket.message + '\n') chatarea.configure(state = 'disabled') else: chatarea.configure(state = 'normal') chatarea.insert(tk.END, chatPacket.player.name + ': ' + chatPacket.message + '\n') chatarea.configure(state = 'disabled') elif tcpPacket.type == tcp.TcpPacket.CONNECT: connectPacket.ParseFromString(data) playerList.append(connectPacket.player) chatarea.configure(state = 'normal') chatarea.insert(tk.END, connectPacket.player.name + ' joined the lobby! \n') chatarea.configure(state = 'disabled') currentPlayers(sock) elif tcpPacket.type == tcp.TcpPacket.DISCONNECT: disconnectPacket.ParseFromString(data) playerList.remove(disconnectPacket.player) if disconnectPacket.update == tcp.TcpPacket.DisconnectPacket.NORMAL: chatarea.configure(state = 'normal') chatarea.insert(tk.END, disconnectPacket.player.name + ' disconnected from the lobby! \n') chatarea.configure(state = 'disabled') currentPlayers(sock) else: chatarea.configure(state = 'normal') chatarea.insert(tk.END, disconnectPacket.player.name + ' lost connection to the lobby! \n') chatarea.configure(state = 'disabled') currentPlayers(sock)
def RecvPacket(sock, player): packet = tcp_packet.TcpPacket() while True: data = sock.recv(1024) packet.ParseFromString(data) #USER DISCONNECTED FROM THE CHAT if packet.type == 0: ret = tcp_packet.TcpPacket.DisconnectPacket() ret.ParseFromString(data) if ret.player.name == "": exit() chatarea.configure(state='normal') chatarea.insert( tk.END, ret.player.name + " has disconnected [" + str(ret.update) + "]\n") chatarea.configure(state='disabled') #A NEW USER ENTERED THE LOBBY elif packet.type == 1: ret = tcp_packet.TcpPacket.ConnectPacket() ret.ParseFromString(data) chatarea.configure(state='normal') chatarea.insert(tk.END, ret.player.name + ' has entered the lobby. \n') chatarea.configure(state='disabled') lobby_id = ret.lobby_id #HOST CREATED LOBBY PACKET elif packet.type == 2: ret = tcp_packet.TcpPacket.CreateLobbyPacket() ret.ParseFromString(data) chatarea.configure(state='normal') chatarea.insert(tk.END, 'Created lobby:' + ret.lobby_id + "\n") chatarea.configure(state='disabled') #USER'S MESSAGE SENT elif packet.type == 3: ret = tcp_packet.TcpPacket.ChatPacket() ret.ParseFromString(data) chatarea.configure(state='normal') chatarea.insert(tk.END, ret.player.name + ':\t' + ret.message + "\n") chatarea.configure(state='disabled') #SHOW PLAYER LIST elif packet.type == 4: ret = tcp_packet.TcpPacket.PlayerListPacket() ret.ParseFromString(data) chatarea.configure(state='normal') chatarea.insert(tk.END, "Player list:\n") chatarea.configure(state='disabled') for p in ret.player_list: chatarea.configure(state='normal') chatarea.insert(tk.END, "\t> " + p.name + "; id:" + p.id + "\n") chatarea.configure(state='disabled') chatarea.configure(state='normal') chatarea.insert( tk.END, "Num of Players in lobby" + str(len(ret.player_list)) + "\n") chatarea.configure(state='disabled') #ERROR MESSAGES elif packet.type == 5: ret = tcp_packet.TcpPacket.ErrLdnePacket() ret.ParseFromString(data) chatarea.configure(state='normal') chatarea.insert(tk.END, "!!! " + ret.err_message + " !!!\n") chatarea.configure(state='disabled') elif packet.type == 6: ret = tcp_packet.TcpPacket.ErrLfullPacket() ret.ParseFromString(data) chatarea.configure(state='normal') chatarea.insert(tk.END, "!!! " + ret.err_message + " !!!\n") chatarea.configure(state='disabled') elif packet.type == 7: ret = tcp_packet.TcpPacket.ErrPacket() ret.ParseFromString(data) chatarea.configure(state='normal') chatarea.insert(tk.END, "!!! " + ret.err_message + " !!!\n") chatarea.configure(state='disabled')
import tcp_packet_pb2 from threading import Thread import tkinter from tkinter import * from tkinter import simpledialog HOST = '202.92.144.45' PORT = 80 #create socket and connect to server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST,PORT)) sentm = "" #create tcp packet tcp = tcp_packet_pb2.TcpPacket() def raise_frame(frame,choice): frame.tkraise() if choice==1: createLobby_Action() elif choice==2: connect_Action() def Menu(): print("[1] - Create Lobby") print("[2] - Connect") print("[3] - Exit") x = input("choice: ") return int(x)