1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
#! /usr/bin/env python
# __________
import os, sys
if __name__ == '__main__':
LOCALDIR = sys.argv[0]
else:
LOCALDIR = __file__
try:
LOCALDIR = os.readlink(LOCALDIR)
except:
pass
LOCALDIR = os.path.dirname(os.path.abspath(LOCALDIR))
# ----------
sys.path.insert(0, os.path.dirname(LOCALDIR))
sys.path.insert(0, LOCALDIR)
import common
import pclient
import modes
UdpLookForServer = [
'127.0.0.1',
'<broadcast>',
]
def parse_cmdline(argv):
# parse command-line
def usage():
print >> sys.stderr, 'usage:'
print >> sys.stderr, ' python Client.py [-d#] [-s#] [extra options] [host[:port]]'
print >> sys.stderr
print >> sys.stderr, 'options:'
print >> sys.stderr, ' host search for a game on the given machine'
print >> sys.stderr, ' host:port connect to the given game server'
print >> sys.stderr, ' (default search for any local server)'
print >> sys.stderr, ' -d# --display=# graphic driver (see below)'
print >> sys.stderr, ' -s# --sound=# sound driver (see below)'
print >> sys.stderr, ' --music=no disable background music'
print >> sys.stderr, ' -h --help display this text'
print >> sys.stderr, ' -m --metaserver connect with the help of the metaserver'
print >> sys.stderr, ' (list servers with Client.py -m)'
print >> sys.stderr, ' -t --tcp for slow or proxy connections'
print >> sys.stderr, ' -u --udp for fast direct connections'
print >> sys.stderr, ' (default is to autodetect tcp or udp)'
print >> sys.stderr, ' --port UDP=# or #:# fixed inbound udp port or host:port'
print >> sys.stderr, ' --port TCP=# fixed inbound tcp port (-m only)'
print >> sys.stderr
print >> sys.stderr, 'graphic drivers:'
for info in modes.graphicmodeslist():
info.printline(sys.stderr)
print >> sys.stderr
print >> sys.stderr, 'sound drivers:'
for info in modes.soundmodeslist():
info.printline(sys.stderr)
print >> sys.stderr
sys.exit(2)
shortopts = 'd:s:htum'
longopts = ['display=', 'sound=', 'music=', 'help', 'tcp', 'udp',
'cfg=', 'metaserver', 'port=']
for info in modes.graphicmodeslist() + modes.soundmodeslist():
short, long = info.getformaloptions()
shortopts += short
longopts += long
try:
from getopt import gnu_getopt as getopt
except ImportError:
from getopt import getopt
from getopt import error
try:
opts, args = getopt(argv, shortopts, longopts)
except error, e:
print >> sys.stderr, 'Client.py: %s' % str(e)
print >> sys.stderr
usage()
metaserver = 0
driver = sound = None
extraopts = {}
for key, value in opts:
if key in ('-d', '--display'):
driver = value
elif key in ('-s', '--sound'):
sound = value
elif key in ('-t', '--tcp'):
extraopts['udp_over_tcp'] = 1
elif key in ('-u', '--udp'):
extraopts['udp_over_tcp'] = 0
elif key in ('-m', '--metaserver'):
metaserver = 1
elif key == '--port':
import common.msgstruct
try:
portname, value = value.split('=')
if portname == 'UDP':
portname = 'CLIENT'
elif portname == 'TCP':
portname = 'BACK'
except ValueError:
portname = 'CLIENT'
if portname == 'CLIENT' and ':' in value:
udphostname, value = value.split(':')
common.msgstruct.PORTS['sendudpto'] = udphostname
common.msgstruct.PORTS[portname] = int(value)
elif key == '--cfg':
extraopts['cfgfile'] = value
elif key in ('-h', '--help'):
usage()
else:
extraopts[key] = value
mode = driver, sound, extraopts
if metaserver:
if len(args) == 0:
metalist()
sys.exit(0)
elif len(args) != 1 or ':' not in args[0]:
usage()
return metaconnect(args[0]), mode
if args:
if len(args) > 1:
usage()
hosts = args[0].split(':')
if len(hosts) == 1:
host, = hosts
from common import hostchooser
server = hostchooser.pick([host] * 5)
elif len(hosts) == 2:
host, port = hosts
try:
port = int(port)
except ValueError:
usage()
server = host, port
else:
usage()
return directconnect(server), mode
from common import hostchooser
server = hostchooser.pick(UdpLookForServer * 3)
return directconnect(server), mode
def directconnect(sockaddr):
print "connecting to %s:%d..." % sockaddr
from socket import socket, AF_INET, SOCK_STREAM
s = socket(AF_INET, SOCK_STREAM)
s.connect(sockaddr)
return s, sockaddr
def metaconnect(metaaddr):
from metaserver import metaclient
import common.msgstruct
port = common.msgstruct.PORTS.get('BACK')
s = metaclient.meta_connect(metaaddr, port)
sockaddr = s.getpeername()
return s, sockaddr
def metalist():
from metaserver import metaclient
metaclient.print_server_list()
def main():
(s, sockaddr), mode = parse_cmdline(sys.argv[1:])
pclient.run(s, sockaddr, mode)
if __name__ == '__main__':
main()
|