public bool connect(string host, int port) { // Store server info this.server = new IPEndPoint(IPAddress.Parse(host), port); // Client may now try to connect this.curState = cState.TRYCONNECT; //Spawn the client reader/writer threads this.nw = new NetworkWorker(server); // Inform the client thread that the server info is ready ready.Release(); // Send the handshake request to the server this.handshake(); // Wait for the connection to be established while (this.curState == cState.TRYCONNECT) ; return true; }
public void setState(cState newState) { currentState = newState; }
// Use this for initialization void Start() { currentState = cState.WindowSelection; }
private void cm_initNewLetter(cState state) { foreach (ucBottomLetterControl _con in state.cf_Dic.Values) { _con.IsEnabled = true; } }
private void cm_showGeneratedLetter(cState state) { w_topPanel.Children.Add(new ucTopLetterControl() { cp_Letter = state.cf_OkChars[0] }); }
private void cm_showCurrentLetter(cState state) { uc_mainLetter.cp_Letter = state.cf_CurrentChar; if (state.cf_CurrentState) { uc_mainLetter.IsEnabled = true; } else { // :( uc_mainLetter.IsEnabled = false; state.cf_Dic[state.cf_CurrentChar].IsEnabled = false; } }
//Main routine, this does all the processing private void ClientstartFunc() { // Hold here until the server information has been provided ready.WaitOne(); // Event Loop // Pull packets out of the network layer and handle them while (this.go) { Packet newPacket = nw.getNext(); // This is a blocking call! // Handle timeout if (newPacket == null) { Console.WriteLine("Timeout on receive"); switch (curState) { case cState.TRYCONNECT: // Did not receive the expected HANDSHAKE message // Restart the handshake this.handshake(); break; case cState.CONNECTED: // The server may have died, ping the server to find out //this.pingServer(); //TODO Should probably not silently ignore this.... lock (this) { if (this.sw_ping.IsRunning) { //this.curState = cState.DISCONNECTED; this.sw_ping.Stop(); this.lastPing = 501; } } break; case cState.DISCONNECTED: default: // This should not happen, die screaming! Environment.Exit(1); break; } } else { //Console.Write("Received packet of type: "); //Console.WriteLine(newPacket.ptype); // Handle the new packet switch (newPacket.ptype) { case Packet.PacketType.CMD: Console.WriteLine("Should not be getting CMD packets from the server..."); Environment.Exit(1); break; case Packet.PacketType.HANDSHAKE: Console.WriteLine("Handshake received from the server"); switch (curState) { case cState.TRYCONNECT: // The connection has succeeded! this.startPing(); this.curState = cState.CONNECTED; break; case cState.CONNECTED: // Repeat? This can be ignored ( I hope...) break; case cState.DISCONNECTED: default: // This should not happen, die screaming! Environment.Exit(1); break; } break; case Packet.PacketType.STC: //Console.WriteLine("STC received from the server"); switch (curState) { case cState.TRYCONNECT: break; case cState.CONNECTED: // Marshall the state change packet into an object StateChange newSTC = new StateChange(newPacket.data); // Add the state change object to the buffer for the UI lock (this.buffer) { buffer.Enqueue(newSTC); } break; case cState.DISCONNECTED: default: // This should not happen, die screaming! Environment.Exit(1); break; } break; case Packet.PacketType.MSC: //Console.WriteLine("MSC received from the server"); switch (curState) { case cState.TRYCONNECT: break; case cState.CONNECTED: // Marshall the state change packet into an object MenuState newMSC = new MenuState(newPacket.data); // Add the state change object to the buffer for the UI lock (this.menuBuffer) { menuBuffer.Enqueue(newMSC); } break; case cState.DISCONNECTED: default: // This should not happen, die screaming! Environment.Exit(1); break; } break; case Packet.PacketType.SYNC: //Console.WriteLine("SYNC received from the server"); switch (curState) { case cState.TRYCONNECT: break; case cState.CONNECTED: syncServer(); break; case cState.DISCONNECTED: default: // This should not happen, die screaming! Environment.Exit(1); break; } break; case Packet.PacketType.PING: //Console.WriteLine("PING received from the server"); switch (curState) { case cState.TRYCONNECT: break; case cState.CONNECTED: lock (this) { sw_ping.Stop(); this.lastPing = sw_ping.ElapsedMilliseconds; } //Console.WriteLine("Ping"+lastPing); break; case cState.DISCONNECTED: default: // This should not happen, die screaming! Environment.Exit(1); break; } break; default: Console.WriteLine("Unknown packet type from the server..."); Environment.Exit(1); break; } } } }