def analysis_message_register(sock, cmd):
    """
    Register message
    @param sock:
    @param cmd:
    @return:
    """
    send_cmd = Command(Command.CMD_REGISTER)
    if db.check_user_exits(cmd.get_string(Argument.ARG_PLAYER_USERNAME)):
        send_cmd.add_int(Argument.ARG_CODE, 0)
    else:
        db.add_user(cmd.get_string(Argument.ARG_PLAYER_USERNAME), cmd.get_string(Argument.ARG_PLAYER_PASSWORD))
        send_cmd.add_int(Argument.ARG_CODE, 1)
    sock.sendall(send_cmd.get_bytes())
    pass
def analysis_message_add_friend(sock, cmd):
    """
    Add friend message
    @param sock:
    @param cmd:
    @return:
    """
    send_cmd = Command(Command.CMD_ADD_FRIEND)
    if db.invite_friend(cmd.get_string(Argument.ARG_PLAYER_USERNAME), cmd.get_string(Argument.ARG_PLAYER_USERNAME)):
        send_cmd.add_int(Argument.ARG_CODE, 1)
        send_cmd.add_string(Argument.ARG_MESSAGE, "Send invite friend successful!")
        #TODO send to friend invite message
    else:
        send_cmd.add_int(Argument.ARG_CODE, 0)
        send_cmd.add_string(Argument.ARG_MESSAGE, "Send invite friend failure! Please try again!")
    sock.sendall(send_cmd.get_bytes())
def analysis_message_chat(sock, cmd):
    """
    Chat message
    @param sock:
    @param cmd:
    @return:
    """
    from_user = sock_name_map[sock]
    to_user = cmd.get_string(Argument.ARG_PLAYER_USERNAME, "no name")
    message = cmd.get_string(Argument.ARG_MESSAGE, "no content")
    send_cmd = Command(Command.CMD_PLAYER_CHAT)
    send_cmd.add_string(Argument.ARG_PLAYER_USERNAME, from_user)
    send_cmd.add_string(Argument.ARG_MESSAGE, message)
    if check_player_online(to_user):
        name_sock_map[to_user].sendall(send_cmd.get_bytes())
    pass
Ejemplo n.º 4
0
        elif arg_type == Argument.LONG:
            long_val = long(unpack("<L", data[read_count:read_count + 8])[0])
            read_count += 8
            cmd.add_long(arg_code, long_val)
    print cmd.get_log()
    pass

HOST, PORT = "localhost", 9090
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
# cmd = Command(Command.CMD_ADD_FRIEND)
# cmd.add_string(Argument.ARG_PLAYER_USERNAME, "giapnh")

cmd = Command(Command.CMD_LOGIN)
username = raw_input("Username:"******"Password:"******"You want chat with?")
    message = raw_input("Message:")
    cmd = Command(Command.CMD_PLAYER_CHAT)
    cmd.add_string(Argument.ARG_FRIEND_USERNAME, chat_with)
    cmd.add_string(Argument.ARG_MESSAGE, message)
    sock.sendall(cmd.get_bytes())
    print cmd.get_log()
    data = sock.recv(1024)
    read(data)

Ejemplo n.º 5
0
# -*- coding: utf-8 -*-
__author__ = 'Nguyen Huu Giap'
import socket
from command import Command
from argument import Argument

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('localhost', 9999))
client.settimeout(10)
while True:
    msg = raw_input("Enter chat text: ")
    cmd = Command(Command.CMD_PLAYER_CHAT)
    cmd.add_string(Argument.ARG_MESSAGE, msg)
    client.send(cmd.get_bytes())
    data = client.recv(1000)
    print len(bytearray(data))