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
171
|
# __________
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('usage:', file=sys.stderr)
print(' python Client.py [-d#] [-s#] [extra options] [host[:port]]', file=sys.stderr)
print(file=sys.stderr)
print('options:', file=sys.stderr)
print(' host search for a game on the given machine', file=sys.stderr)
print(' host:port connect to the given game server', file=sys.stderr)
print(' (default search for any local server)', file=sys.stderr)
print(' -d# --display=# graphic driver (see below)', file=sys.stderr)
print(' -s# --sound=# sound driver (see below)', file=sys.stderr)
print(' --music=no disable background music', file=sys.stderr)
print(' -h --help display this text', file=sys.stderr)
print(' -m --metaserver connect with the help of the metaserver', file=sys.stderr)
print(' (list servers with Client.py -m)', file=sys.stderr)
print(' -t --tcp for slow or proxy connections', file=sys.stderr)
print(' -u --udp for fast direct connections', file=sys.stderr)
print(' (default is to autodetect tcp or udp)', file=sys.stderr)
print(' --port UDP=# or #:# fixed inbound udp port or host:port', file=sys.stderr)
print(' --port TCP=# fixed inbound tcp port (-m only)', file=sys.stderr)
print(file=sys.stderr)
print('graphic drivers:', file=sys.stderr)
for info in modes.graphicmodeslist():
info.printline(sys.stderr)
print(file=sys.stderr)
print('sound drivers:', file=sys.stderr)
for info in modes.soundmodeslist():
info.printline(sys.stderr)
print(file=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():
s, i = info.getformaloptions()
shortopts += s
longopts += i
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 as e:
print('Client.py: %s' % str(e), file=sys.stderr)
print(file=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:
#print(f"port = {port} int={int}",file=sys.stderr)
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()
|