92 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
			
		
		
	
	
			92 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
| /*
 | |
| 文件名: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) {
 | |
|         this.Data.writen(Value, 'c');
 | |
|     }
 | |
| 
 | |
|     //短整型
 | |
|     function Put_Short(Value) {
 | |
|         this.Data.writen(Value, 's');
 | |
|     }
 | |
| 
 | |
|     //整型
 | |
|     function Put_Int(Value) {
 | |
|         this.Data.writen(Value, 'i');
 | |
|     }
 | |
| 
 | |
|     //浮点型
 | |
|     function Put_Float(Value) {
 | |
|         this.Data.writen(Value, 'f');
 | |
|     }
 | |
| 
 | |
|     //字符串
 | |
|     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) {
 | |
|         return stream_myreadstring.pcall(this.Data, len);
 | |
|     }
 | |
| 
 | |
|     //获取字节数组
 | |
|     function Get_Binary(len) {
 | |
|         return this.Data.readblob(len);
 | |
|     }
 | |
| } |