publicclassPlayerStats : NetworkBehaviour { // 服务器修改,自动同步到所有客户端 public NetworkVariable<int> Health = new(100); public NetworkVariable<Vector3> Position = new(Vector3.zero);
var flags = (PlayerDirtyFlags)reader.ReadUInt32();
if (flags.HasFlag(PlayerDirtyFlags.Position)) { Position = new Vector3( reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle() ); } if (flags.HasFlag(PlayerDirtyFlags.Health)) Health = reader.ReadInt32(); if (flags.HasFlag(PlayerDirtyFlags.Mana)) Mana = reader.ReadInt32(); } }
The undelying network protocol for communication on the web. It defines methods like GET, POST, PUT, and DELETE. When you visit a website, your browser sends HTTP requests and the server sends back HTTP responses.
multipart/form-data is an encoding type (media or content type) used in HTTP requests to send data to a server, primarily for forms that include file uploads.
In HTML, you specify this encoding by setting the enctype attribute of the <form> tag to multipart/form-data when the method is POST. This is mandatory if your form includes an <input type=”file”> element.
When using modern web APIs like the Fetch API or XMLHttpRequest in JavaScript, the browser’s FormData object automatically handles the complex process of structuring the request in the multipart/form-data format.
WWWForm is a Unity class used to create a standard web form. It structures data into the multipart/form-data or x-www-form-urlencoded format, which is a standard format for submitting form data, including file uploads, to web servers.
REST API
A set of architectural constraints (like statelessness and uniform interface) that define how to build scalable and standardized web services, primarily using HTTP.
Guidelines for designing a server’s endpoints in a logical, resource-oriented way, specifically origanize resources in URIs like https://exmaple.com/api/v3/users
For example, a REST API would use GET /users to retrieve users, POST /users to create a new user, and leverage standard HTTP status codes.
Fetch API
A modern, promise-based JavaScript interface built into web browsers that allows developers to make HTTP requests programmatically.
Client-side tool used to consume a service that might be RESTful (or any other kind of HTTP API).
You use the Fetch API in JavaScript in your web browser or a server environment like Node.js to send the HTTP requests defined by the API’s design.
In summary, you use the Fetch API to send HTTP requests to a server that is structured as a REST API.
XHR
XML Http Request (XHR) is a JavaScript API to create HTTP requests. Its methods provide the ability to send network requests between the browser and a server. The Fetch API is the modern replacement for XMLHttpRequest
ASP.NET
Microsoft’s open-source framework for building web applications and and services using .NET and C#; it is fundamentally built on top of the HTTP protocol.
HTTP/2 & HTTP/3: Modern versions of ASP.NET Core support HTTP/2 and HTTP/3 for improved performance.
Status Codes: The framework provides built-in methods to return standard HTTP status codes, such as Ok() (200), CreatedAtAction() (201), or NotFound() (404).
Handling Incoming HTTP Requests (Server-Side)
HTTP Servers: ASP.NET Core uses Kestrel, a cross-platform HTTP server, as the default to listen for requests.
HttpContext: Every request is encapsulated in an HttpContext object, which provides access to the Request (headers, body, query strings) and the Response.
Routing and HTTP Methods: Controllers use attributes like [HttpGet], [HttpPost], and [HttpPut] to map specific HTTP verbs to C# methods.
Creating an HTTP Endpoint
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
var builder = WebApplication.CreateBuilder(args); var app = builder.Build();
// Simple GET endpoint returning a string app.MapGet("/", () => "Hello World!");
// GET endpoint with a route parameter app.MapGet("/products/{id}", (int id) => $"Returning product {id}");
// POST endpoint that accepts a JSON object (Todo item) app.MapPost("/todoitems", (Todo todo) => Results.Created($"/todoitems/{todo.Id}", todo));
app.Run();
// Data model for the POST example publicrecordTodo(int Id, string Name, bool IsComplete);
Making Outgoing Requests (Client-Side)
To consume other web services or APIs from within your ASP.NET application, you use the HttpClient class.
而Lock用的是乐观锁方式。所谓乐观锁就是,每次不加锁而是假设没有冲突而去完成某项操作,如果因为冲突失败就重试,直到成功为止。乐观锁实现的机制就是CAS操作(Compare and Swap )。我们可以进一步研究ReentrantLock的源代码,会发现其中比较重要的获得锁的一个方法是compareAndSetState。这里其实就是调用的CPU提供的特殊指令。
线程:共享堆资源,利用多核处理器可以实现并行处理。 线程是进程内部的一条执行路径,是 CPU 调度和执行的基本单位。一个进程可以包含多个线程,这些线程共享进程的地址空间(包括代码段、数据段、堆等)以及系统资源(如打开的文件、网络连接等),但每个线程都有自己独立的栈空间用于保存局部变量、函数调用的上下文等信息。比如在一个文字处理软件进程中,可能有一个线程负责接收用户的键盘输入,另一个线程负责实时进行拼写检查,它们都在同一个进程的环境下协同工作。