#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import socket
import sys
import time
import sched
import threading

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sch = sched.scheduler(time.time, time.sleep)
def send_stuff():
    msg = "000000090000006d0000014f9e15bd3300".decode('hex')
    while True:
        s.send(msg)
        answer = s.recv(1024)
        print answer
        time.sleep(0.1)

def send_msg (string):
    mes = "0000000f0000008c000%s%s00" % (hex(len(string)).split('x')[1], string.encode('hex'))
    #mes = "000000070000008c00046869393900".decode('hex')
    print mes
    s.send(mes.decode('hex'))
    answer = s.recv(1024)
    return

def connect_rw (ip, port, ver):
    s.connect((ip, int(port)))
    msg = '00000020000000a00016636f6d2e636f72726f64696e6767616d65732e7274730000000000000027'.decode('hex')
    s.send(msg)
    answer = s.recv(1024)
    print answer
#    msg = '�ncom.corrodinggames.rts,,Namedcom.corrodinggames.rts@33C47298EB429F53607281AC7443971124D4E5C6F4F47ABD67CE504F830C2FDC*�'
#    msg= '0000008a0000006e0016636f6d2e636f72726f64696e6767616d65732e727473000000030000002c0000002c00054e616d6564000016636f6d2e636f72726f64696e6767616d65732e72747300406333376331623331353831353461333338666632353065623938323964626566'.decode('hex')
    msg = "0000008a0000006e0016636f6d2e636f72726f64696e6767616d65732e727473000000030000002c0000002c00054e616d6564000016636f6d2e636f72726f64696e6767616d65732e7274730040333343343732393845423432394635333630373238314143373434333937313132344434453543364634463437414244363743453530344638333043324644432ab38eb6".decode('hex')
    print msg
#    msg = '0000008b0000006e0016636f6d2e636f72726f64696e6767616d65732e727473000000020000002700000027000a556e6e616d6564313134000016636f6d2e636f72726f64696e6767616d65732e727473004041364637354330354432393436304138393244323744353332413842423945374645303136443942303139463930453138374638313036304432463546433041'.decode('hex')
    #msg = "0000008b0000006e0016636f6d2e636f72726f64696e6767616d65732e727473000000030000002a0000002a00054e616d6564000016636f6d2e636f72726f64696e6767616d65732e7274730040323344433732393845423432394635333630373238314143373434333937313132344434453543364634463437414244363743453530344638333043324644432ab38eb6".decode('hex')
    s.send(msg)
    answer = s.recv(1024)
    print answer

    t1 = threading.Thread(target=send_stuff)
    t1.start()
    #t1.join()
    #sch.enter(1, 1, send_stuff, ())
    #sch.run()
    print "test"
    #send_stuff()

    #msg = "000000070000008c00046869393900".decode('hex')
    #s.send(msg)
    #answer = s.recv(1024)
    #print answer
    return

connect_rw(sys.argv[1], sys.argv[2], 1)
send_msg(sys.argv[3])
