Esempio n. 1
0
        /// Synchronous submission api method.
        public SubmissionResponse SubmitScore(string level, Score score)
        {
            // Ensure arguments are valid
            if (level == null)
            {
                throw new ArgumentNullException();
            }
            score.Validate();

            Connect();

            // form a submission message and send it to the server
            string request = SubmissionRequest.ToJson(level, score);

            WriteLine(request);

            // get the response and convert back to an object
            string             response = ReadLine();
            SubmissionResponse position = SubmissionResponse.FromJson(response);

            Disconnect();

            // report back to user
            return(position);
        }
Esempio n. 2
0
        /// OnConnect callback, the bulk of the work is handled here.
        void SubmitScoreOnConnectCallback(IAsyncResult ar)
        {
            SubmitScoreState state = (SubmitScoreState)ar.AsyncState;

            // the arguments for the external callback
            SubmissionResponse position = new SubmissionResponse();
            ServerException    error    = null;

            // handle being unable to connect
            if (!client.Connected)
            {
                error = new ServerException("Unable to connect");
            }
            else
            {
                try {
                    // form a submission message and send it to the server
                    string request = SubmissionRequest.ToJson(state.level, state.score);
                    WriteLine(request);

                    // get a response from server and convert back to an object
                    string response = ReadLine();
                    position = SubmissionResponse.FromJson(response);
                } catch (ServerException e) {
                    error = e;
                }
            }

            // report back to user
            if (state.callback != null)
            {
                state.callback.Invoke(position, error);
            }

            // note: even if connection failed, clean up is still necessary
            DisconnectAsync(ar);
        }