| 
									
										
										
										
											2025-01-03 19:47:36 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 文件名:Packet.nut | 
					
						
							|  |  |  | 路径:User/Socket/Packet.nut | 
					
						
							|  |  |  | 创建日期:2025-01-04	22:20 | 
					
						
							|  |  |  | 文件用途:数据封包类 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | class Packet { | 
					
						
							|  |  |  |     Data = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     constructor(...) { | 
					
						
							|  |  |  |         //不传参数时,构造空包 | 
					
						
							|  |  |  |         if (vargv.len() == 0) { | 
					
						
							|  |  |  |             this.Data = blob(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         //传入数据流以流的形式构造包 | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             this.Data = vargv[0]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //字节 | 
					
						
							|  |  |  |     function Put_Byte(Value) { | 
					
						
							| 
									
										
										
										
											2025-01-04 20:03:17 +08:00
										 |  |  |         this.Data.writen(Value, 'c'); | 
					
						
							| 
									
										
										
										
											2025-01-03 19:47:36 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //短整型 | 
					
						
							|  |  |  |     function Put_Short(Value) { | 
					
						
							| 
									
										
										
										
											2025-01-04 20:03:17 +08:00
										 |  |  |         this.Data.writen(Value, 's'); | 
					
						
							| 
									
										
										
										
											2025-01-03 19:47:36 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //整型 | 
					
						
							|  |  |  |     function Put_Int(Value) { | 
					
						
							| 
									
										
										
										
											2025-01-04 20:03:17 +08:00
										 |  |  |         this.Data.writen(Value, 'i'); | 
					
						
							| 
									
										
										
										
											2025-01-03 19:47:36 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //浮点型 | 
					
						
							|  |  |  |     function Put_Float(Value) { | 
					
						
							| 
									
										
										
										
											2025-01-04 20:03:17 +08:00
										 |  |  |         this.Data.writen(Value, 'f'); | 
					
						
							| 
									
										
										
										
											2025-01-03 19:47:36 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //字符串 | 
					
						
							|  |  |  |     function Put_String(String) { | 
					
						
							|  |  |  |         foreach(char in String) { | 
					
						
							|  |  |  |             Put_Byte(char); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //字节数组 | 
					
						
							|  |  |  |     function Put_Binary(BinaryArray) { | 
					
						
							|  |  |  |         //流 | 
					
						
							|  |  |  |         if (typeof BinaryArray == "blob") { | 
					
						
							|  |  |  |             this.Data.writeblob(BinaryArray); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         //数组 | 
					
						
							|  |  |  |         else if (typeof BinaryArray == "array") { | 
					
						
							|  |  |  |             foreach(byte in BinaryArray) { | 
					
						
							|  |  |  |                 Put_Byte(byte); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //获取字节 | 
					
						
							|  |  |  |     function Get_Byte() { | 
					
						
							|  |  |  |         return this.Data.readn('c'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //获取短整型 | 
					
						
							|  |  |  |     function Get_Short() { | 
					
						
							|  |  |  |         return this.Data.readn('s'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //获取整型 | 
					
						
							|  |  |  |     function Get_Int() { | 
					
						
							|  |  |  |         return this.Data.readn('i'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //获取浮点型 | 
					
						
							|  |  |  |     function Get_Float() { | 
					
						
							|  |  |  |         return this.Data.readn('f'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //获取字符串 | 
					
						
							|  |  |  |     function Get_String(len) { | 
					
						
							| 
									
										
										
										
											2025-01-05 00:48:35 +08:00
										 |  |  |         return stream_myreadstring.pcall(this.Data, len); | 
					
						
							| 
									
										
										
										
											2025-01-03 19:47:36 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //获取字节数组 | 
					
						
							|  |  |  |     function Get_Binary(len) { | 
					
						
							|  |  |  |         return this.Data.readblob(len); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |