Using the coprocessor in Unity to verify the data of the service side

Source: Internet
Author: User

Recent projects in the personal center of some things, user avatar upload, download, local cache, level two cache, compression, these have to do, perfectly formed Ah, is also written in the muddle,

When we upload the user picture, to the server to send upload avatar request, generally do a verification, to the server to obtain token verification information, to ensure illegal upload, if not do this then there will be illegal users upload illegal images, so that your server

Bring about an unknown catastrophe.

And the logic of validation is very well written, and there is no difficulty, such as:

Server.sendmessage ("Get token");

Client.receive (String token)

{

Print (token);

if (token! = Local token)

{
Server.sendmessage ("Get token"); re-acquire Token

}

}

It seems very simple, but the general client in the receiving service side of the message back, there will be a phase of the protocol distribution, and then your receive token code will be placed in the agreement distributed there, or in the protocol distribution where to call, if serious failure,

Go again this process, has been to serious use, a bit like recursion, is not feeling a little silly way?

Indeed, this is not elegant, if not a good comment, it will let people can not touch the clue, the following is a very good way to achieve, that is to use the co-process

<summary>
Iterator object used to get token
</summary>
public class Tokenverificationoperation:ienumerator {
public bool MoveNext () {
Debug.logger.Log (Isdown);
Return! Isdown;
}
Public Tokenverificationoperation () {
Zoneserverrequeset.sendportraittokenreq ();
}
public void Reset () {

}
public void Ontoken (string token) {
Isdown = true;
token = token;
}
public bool Isdown {get; set;}
public string Token {get; set;}
Public object Current {get {return null;}}
}

Use time

Public IEnumerator Tokenjy ()

{

Tokenoperation = new Tokenverificationoperation ();
Yield return MB. Startcoroutine (tokenoperation);

if (tokenoperation. Token) {

Startcoroutine (TOKENJY);

}

}

is not feeling this way, very good, at least not everywhere to write code everywhere to find, directly a function inside, in fact, the principle is very simple, unity in the process will actively invoke the implementation of IEnumerator (iterator) MoveNext function, we go to implement this interface

And then in the MoveNext function to determine whether the service side of the message returned, so that through yeild cooperation, so that the process is simplified. Also very easy to understand, simple and clear.

Another hint: The iterator loop to determine the service-side callback method, not recommended for large-scale use, unity, if the association is not used properly, every launch will have a certain GC garbage, ingenious, just fine

Using the coprocessor in Unity to verify the data of the service side

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.