Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Overall Architecture
On the client computers: A running Web browser with the Silverlight game
The server computer has more pieces: 1. The game HTML file served up by a running web server 2. The running game server program 3. A bunch of marked-up images in the right directory 4. The running socket policy server program
Multiple Players
Multiplayer games are ideal for sockets: Anyone can click at any time The server can update anyone at any time Http style request/response doesn t work well for these types of problems. It s the server that has most multiplayer code; each client of course only handles itself
Connect
Closing
Connecting
var s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); var e = new SocketAsyncEventArgs(); e.RemoteEndPoint = new DnsEndPoint(serverName, 4529); e.Completed += OnConnected; s.ConnectAsync(e);
Writing
Picture Hunt makes a list of ArraySegments, and then writes them out with SendAsync . The WriteCompleted doesn t actually do anything for this application var senddata = new List<ArraySegment<byte>>(); senddata.Add(new ArraySegment<byte>(header)); if (data != null) senddata.Add(new ArraySegment<byte>(data)); var writeEventArgs = new SocketAsyncEventArgs(); writeEventArgs.Completed += WriteCompleted; writeEventArgs.BufferList = senddata; socket.SendAsync(writeEventArgs);
Different people have different styles for serializing data. Picture Hunt has hand-made code for serialization. Each Command that has extra header fields has to implement InitHeader() to write them out into a BinaryWriter. Data is read in with the CommandReader class (which also handles message boundaries). CommandReader has to know about every class and every opcode; this is written by hand htonl converts host to native format (this lets my program work on any system)
Closing
The Discrete style is often a good choice. Other choices are Per-task dialog: show multiple activities related to a task (e.g., downloading) User-selected dialog: similar, but is shown only when user asks. Is appropriate when networking isn t a primary part of the user s work UI replacement: when offline, switch interface. Is appropriate when the app has no off-line capabilities (which is rare)
Started
Goes to OK if a connection can be made Goes to FailNl if a connection cannot be made
Ok
Goes to Fail if a socket read fails to read
Fail
Sets a timer to switch to Retry
Retry
The program is told to StartNeworkConnection() Goes to Ok if the connection works and Fail if it does not
FailNl
Sets a timer to switch into RetryNl
RetryNl
The program is told to StartNetworkConnection() Goes to Ok if the connection works and FailNl if it does not
People move their laptop computers a lot more than they used to. Your program has to handle Network Transitions smoothly: Always retry on failures Use the network status (and not an emergency dialog) Transitions are common: the user doesn t need a big reminder
The resulting single Area is now either a good candidate to return, or is null. Reading a LINQ book really helped me get started.