public static void MessageReceived(HS_SocketDataWorker sdw, string message) { JsonDataMessage request = JsonDataMessage.Parse(message.Trim()); Log(message); if (request.Data1 == "login") { clients[sdw].userid = request.Data2; //TODO make this async Log("API call"); string stringResponse = NetUtil.PostSynchro(Settings.Current.InternalTokenLookupUrl, new Dictionary <string, string> { { "code", Settings.Current.InternalApiAccessCode }, { "userid", request.Data2 } }); Log("Response: " + stringResponse); ServerResponse response = ServerResponse.Parse(stringResponse); clients[sdw].symkey = response.Data1; Log("Sym token: " + response.Data1); sdw.Symkey = response.Data1; JsonDataMessage readyRequest = new JsonDataMessage("ready"); Log("User joined, sending ready message: " + readyRequest.Json); sdw.Send(readyRequest.Json); legacy2PlayerQueue.Enqueue(sdw); } CheckGameStart(); }
public void PlayerCommandReceived(HS_SocketDataWorker sdw, string message) { if (!gameon) { return; } HS_PlayerInstance callingPlayer = playerInstances[sdw]; JsonDataMessage request = JsonDataMessage.Parse(message.Trim()); string query = request.Data1; if (query == "login") { names[callingPlayer] = request.Data2; } int spaceindex = query.IndexOf(' '); string q = (spaceindex > 0) ? query.Substring(0, spaceindex) : query; string[] cs = query.Split(' '); string response = ""; switch (q) { case "hands": response = PrintHands(callingPlayer); break; case "fields": response = PrintFields(); break; //explain card case "play": response = Play(callingPlayer, cs); break; case "attack": response = Attack(callingPlayer, cs); break; case "a": response = Attack(callingPlayer, cs); break; case "allface": response = AllFace(callingPlayer, cs); break; //case "help": break; //case "clear": System.Console.Clear(); break; case "end": response = EndTurn(callingPlayer); break; case "hero": response = Hero(callingPlayer); break; case "whoturn": response = WhoTurn(); break; default: response = "Unknown command"; break; } sdw.Send(response); CheckDeadPlayers(); }
void GameConnectCallback(IAsyncResult ar) { try { Socket listener = (Socket)ar.AsyncState; listener.EndConnect(ar); HS_SocketDataWorker sdw = new HS_SocketDataWorker(listener); sdw.SetCallback(new HS_SocketDataWorker.HS_PlayerCommandCallback(GameMessageReceived)); JsonDataMessage r = new JsonDataMessage("login", userid); sdw.Send(r.Json); Debug.Log("Sending data: " + r.Json); } catch (Exception e) { Debug.Log(e.ToString()); } }
private static void StartClient() { // Connect to a remote device. try { // Establish the remote endpoint for the socket. // The name of the // remote device is "host.contoso.com". IPHostEntry ipHostInfo = Dns.Resolve(Settings.Current.ServerUrl); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress, gameport); // Create a TCP/IP socket. Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Connect to the remote endpoint. client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client); connectDone.WaitOne(); Receive(client); String command = ""; while (command != "disconnect") { command = Console.ReadLine(); if (command == "clear") { Console.Clear(); continue; } else if (command == "status" || command == "s" || command == "") { Console.Clear(); JsonDataMessage r = new JsonDataMessage(name, "hands"); JsonDataMessage r2 = new JsonDataMessage(name, "fields"); //HS_Request r3 = new HS_Request(name, "whoturn"); //Send(client, r3.Json); Send(client, r.Json); Send(client, r2.Json); continue; } JsonDataMessage request = new JsonDataMessage(name, command); // Send test data to the remote device. Send(client, request.Json); //sendDone.WaitOne(); // Receive the response from the remote device. //receiveDone.WaitOne(); } // Release the socket. Console.WriteLine("Shutting down connection..."); client.Shutdown(SocketShutdown.Both); client.Close(); } catch (Exception e) { Console.WriteLine(e.ToString()); } }