socket_protobuf
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -18,16 +18,17 @@ "use strict"; | ||
} | ||
let buffer2 = this.buffer; | ||
while (buffer2.length >= 10) { | ||
const _info_length = buffer.readUInt32BE(SocketPackage_1.SocketPackage.idxProtoLen); | ||
let buff = this.buffer; | ||
while (buff.length > SocketPackage_1.SocketPackage.headLength) { | ||
const _info_length = buff.readUInt32BE(SocketPackage_1.SocketPackage.idxProtoLen); | ||
let bufferLen = SocketPackage_1.SocketPackage.headLength + _info_length; | ||
if (buffer.length < bufferLen) { //数据流长度,不足一个完整的proto协议包 | ||
if (buff.length < bufferLen) { //数据流长度,不足一个完整的proto协议包 | ||
return; | ||
} | ||
const pkg = new SocketPackage_1.SocketPackage(buffer.readUInt32BE(SocketPackage_1.SocketPackage.idxCmdId)); | ||
pkg.setBuffer(buffer.subarray(0, bufferLen), _info_length); //保存一个完整的proto协议包 | ||
this.buffer = buffer.subarray(bufferLen); //返回proto协议包之外数据流; | ||
const pkg = new SocketPackage_1.SocketPackage(buff.readUInt32BE(SocketPackage_1.SocketPackage.idxCmdId)); | ||
pkg.setBuffer(buff.subarray(0, bufferLen), _info_length); //保存一个完整的proto协议包 | ||
buff = buff.subarray(bufferLen); //返回proto协议包之外数据流; | ||
this.data_handler.apply(null, [pkg]); | ||
} | ||
this.buffer = buff; | ||
} | ||
} | ||
exports.BufferReceiver = BufferReceiver; |
@@ -26,3 +26,3 @@ import { PServer } from "../Server"; | ||
private readonly _connect_delay; | ||
private _socket_client; | ||
private socket; | ||
private _int_timeOut; | ||
@@ -34,2 +34,3 @@ private _connect_index; | ||
private onConnected; | ||
sendMsg(cmdType: number, newBuff: Uint8Array | Buffer): boolean; | ||
private onMessage; | ||
@@ -51,3 +52,2 @@ private onClose; | ||
private lineLogErr; | ||
private lineLogWarn; | ||
} |
@@ -27,7 +27,10 @@ "use strict"; | ||
this.clearTimeOut(); | ||
this.handlerMgr = new HandlerMgr_1.HandlerMgr(this._socket_client); | ||
this.handlerMgr = new HandlerMgr_1.HandlerMgr(this.socket); | ||
this.handlerMgr.getHandler(HandlerMgr_1.Cmd.ServerInfo).serverInfo = this; | ||
this.lineLogInfo(this.lineInfo + "服务器---连接成功\n\n正在请求服务器数据...\n"); | ||
this._socket_client.sendTypeBuff(HandlerMgr_1.Cmd.ServerInfo, Server_1.PServer.encode(this.clientInfo).finish()); //发送获取数据要求 | ||
this.sendMsg(HandlerMgr_1.Cmd.ServerInfo, Server_1.PServer.encode(this.clientInfo).finish()); //发送获取数据要求 | ||
} | ||
sendMsg(cmdType, newBuff) { | ||
return this.socket.send(cmdType, newBuff); | ||
} | ||
async onMessage(pkg) { | ||
@@ -53,3 +56,3 @@ const handler = this.handlerMgr.getHandler(pkg.cmdId); | ||
onError() { | ||
this.lineLogErr(this.lineInfo + " Web_Sever服务器---连接错误"); | ||
this.lineLogErr(this.lineInfo + " Sever服务器---连接错误"); | ||
this.clearSocket(); | ||
@@ -65,3 +68,3 @@ this.startTimeOut(); | ||
name: os.hostname(), | ||
version: machineId.toString(32), | ||
version: machineId.toString(32).toUpperCase(), | ||
ip: this.ip, | ||
@@ -87,5 +90,5 @@ port: this.port, | ||
doConnect() { | ||
this.lineLogInfo("Web_Sever服务器---连接开始:" + this.ip + ":" + this.port); | ||
this.lineLogInfo("服务器连接开始:" + this.ip + ":" + this.port); | ||
this._connect_stat = NetStatus.connecting; | ||
this._socket_client = new ServerSocket_1.ServerSocket(this.ip, this.port, [ | ||
this.socket = new ServerSocket_1.ServerSocket(this.ip, this.port, [ | ||
this.onConnected.bind(this), | ||
@@ -97,3 +100,3 @@ this.onMessage.bind(this), | ||
]); | ||
this._socket_client.onConnect(); | ||
this.socket.onConnect(); | ||
this.startTimeOut(); | ||
@@ -104,5 +107,5 @@ } | ||
this._connect_stat = NetStatus.start; | ||
if (this._socket_client != null) { | ||
this._socket_client.dispose(); | ||
this._socket_client = null; | ||
if (this.socket != null) { | ||
this.socket.dispose(); | ||
this.socket = null; | ||
} | ||
@@ -120,3 +123,3 @@ } | ||
onReconnect() { | ||
this.lineLogWarn(`!!!已达到${this._connect_index}次连接尝试....`); | ||
console.warn(this.lineInfo + `-------!!!已达到${this._connect_index}次连接尝试....`); | ||
this.start(); | ||
@@ -133,3 +136,3 @@ } | ||
get lineInfo() { | ||
return "Sever线路:" + this.ip + ":" + this.port; | ||
return this.clientInfo.name + " ---> " + this.ip + ":" + this.port; | ||
} | ||
@@ -142,7 +145,4 @@ lineLogInfo(msg) { | ||
} | ||
lineLogWarn(msg) { | ||
console.warn(this.lineInfo + "-------" + msg); | ||
} | ||
} | ||
Client.index = 1; | ||
exports.Client = Client; |
@@ -9,3 +9,3 @@ "use strict"; | ||
sendMsg(cmdId, msg) { | ||
this.sender.sendTypeBuff(cmdId, msg); | ||
this.sender.send(cmdId, msg); | ||
} | ||
@@ -12,0 +12,0 @@ setServer(server) { |
@@ -12,3 +12,3 @@ import { Handler } from "./Handler"; | ||
export declare enum Cmd { | ||
ServerInfo = 1 | ||
ServerInfo = 0 | ||
} |
@@ -22,3 +22,3 @@ "use strict"; | ||
(function (Cmd) { | ||
Cmd[Cmd["ServerInfo"] = 1] = "ServerInfo"; | ||
Cmd[Cmd["ServerInfo"] = 0] = "ServerInfo"; | ||
})(Cmd = exports.Cmd || (exports.Cmd = {})); |
import { Socket } from "net"; | ||
/** | ||
* 连接服务器的socket | ||
*/ | ||
export declare class ServerSocket { | ||
@@ -21,4 +24,3 @@ private _is_alive; | ||
private removeEvent; | ||
sendTypeBuff(cmdType: number, buff: Uint8Array | Buffer): boolean; | ||
sendBuff(buff: Buffer): boolean; | ||
send(cmdType: number, newBuff: Uint8Array | Buffer): boolean; | ||
private onReceivePkg; | ||
@@ -25,0 +27,0 @@ private onMessage; |
@@ -6,2 +6,5 @@ "use strict"; | ||
const SocketPackage_1 = require("../SocketPackage"); | ||
/** | ||
* 连接服务器的socket | ||
*/ | ||
class ServerSocket { | ||
@@ -59,9 +62,8 @@ /** | ||
} | ||
sendTypeBuff(cmdType, buff) { | ||
send(cmdType, newBuff) { | ||
if (this._is_alive) { | ||
let buffer = Buffer.alloc(10); | ||
buffer.writeUInt32BE(0, SocketPackage_1.SocketPackage.idxProtoLen); | ||
buffer.writeUInt32BE(newBuff.byteLength, SocketPackage_1.SocketPackage.idxProtoLen); | ||
buffer.writeUInt32BE(cmdType, SocketPackage_1.SocketPackage.idxCmdId); | ||
buffer.writeUInt32BE(buffer.byteLength, SocketPackage_1.SocketPackage.idxProtoLen); | ||
buffer = Buffer.concat([buffer, buff]); | ||
buffer = Buffer.concat([buffer, newBuff]); | ||
return this._client.write(buffer); | ||
@@ -71,8 +73,2 @@ } | ||
} | ||
sendBuff(buff) { | ||
if (this._is_alive) { | ||
return this._client.write(buff); | ||
} | ||
return false; | ||
} | ||
onReceivePkg(pkg) { | ||
@@ -79,0 +75,0 @@ if (this._is_alive) { |
@@ -26,3 +26,3 @@ "use strict"; | ||
async loopHandlerPkgList() { | ||
if (this.handling || this.inputPkgList.length == 0) { | ||
if (this.handling || this.inputPkgList.length === 0) { | ||
return; | ||
@@ -44,6 +44,6 @@ } | ||
async pkg2handler(pkg) { | ||
let cmdType = pkg.cmdId; | ||
let handler = this.server.handlerMgr.getHandler(cmdType); | ||
let cmdId = pkg.cmdId; | ||
let handler = this.server.handlerMgr.getHandler(cmdId); | ||
if (handler != null) { | ||
await handler.doHandler(this.id, cmdType, pkg); | ||
await handler.doHandler(cmdId, this.id, pkg); | ||
} | ||
@@ -50,0 +50,0 @@ } |
@@ -8,5 +8,5 @@ import { SocketPackage } from "../../SocketPackage"; | ||
protected getMyClient(socketId: number): ClientSocket; | ||
doHandler(socketId: number, cmdId: number, pkg: SocketPackage): Promise<void>; | ||
doHandler(cmdId: number, socketId: number, pkg: SocketPackage): Promise<void>; | ||
protected abstract execute(cmdId: number, socketId: number, pkg: SocketPackage): Promise<void>; | ||
sendMsg(cmdId: number, socketId: number, buff: Uint8Array | Buffer): void; | ||
} |
@@ -11,3 +11,3 @@ "use strict"; | ||
} | ||
async doHandler(socketId, cmdId, pkg) { | ||
async doHandler(cmdId, socketId, pkg) { | ||
await this.execute(cmdId, socketId, pkg); | ||
@@ -14,0 +14,0 @@ } |
@@ -9,6 +9,7 @@ import { Handler } from "./Handler"; | ||
setHandler(cmdId: number, handler: Handler): void; | ||
private setHandler0; | ||
} | ||
/**通信协议*/ | ||
export declare enum Cmd { | ||
ServerInfo = 1 | ||
ServerInfo = 0 | ||
} |
@@ -8,3 +8,3 @@ "use strict"; | ||
this.server = server; | ||
this.setHandler(Cmd.ServerInfo, new ClientHandler_1.ClientHandler()); | ||
this.setHandler0(Cmd.ServerInfo, new ClientHandler_1.ClientHandler()); | ||
} | ||
@@ -15,2 +15,8 @@ getHandler(cmdId) { | ||
setHandler(cmdId, handler) { | ||
if (cmdId <= 0) { | ||
throw new Error("协议Id必须大于0"); | ||
} | ||
this.setHandler0(cmdId, handler); | ||
} | ||
setHandler0(cmdId, handler) { | ||
handler.setServer(this.server); | ||
@@ -24,3 +30,3 @@ this.handlers.set(cmdId, handler); | ||
(function (Cmd) { | ||
Cmd[Cmd["ServerInfo"] = 1] = "ServerInfo"; | ||
Cmd[Cmd["ServerInfo"] = 0] = "ServerInfo"; | ||
})(Cmd = exports.Cmd || (exports.Cmd = {})); |
@@ -17,3 +17,3 @@ import { ClientSocket } from "./ClientSocket"; | ||
onClientInfo: (socketId: number, PServer: any) => void; | ||
constructor(port: number, ipv4?: boolean, serverInfo?: PServer); | ||
constructor(port: number, ipv4?: boolean, onClientInfo?: (socketId: number, PServer: any) => void, serverInfo?: PServer); | ||
/** | ||
@@ -25,3 +25,3 @@ * 侦听客户端连接与断开 | ||
*/ | ||
startServer(onConnectClient: (client: ClientSocket) => void, onDisconnectClient: (socketId: number, client: ClientSocket) => void, onClientInfo?: (socketId: number, PServer: any) => void): void; | ||
startServer(onConnectClient: (client: ClientSocket) => void, onDisconnectClient: (socketId: number, client: ClientSocket) => void): void; | ||
private tryGetIpv4; | ||
@@ -28,0 +28,0 @@ private startListen; |
@@ -7,3 +7,3 @@ "use strict"; | ||
class ServerListener { | ||
constructor(port, ipv4 = true, serverInfo = null) { | ||
constructor(port, ipv4 = true, onClientInfo = null, serverInfo = null) { | ||
this.clientMap = new Map(); | ||
@@ -14,2 +14,3 @@ this.lastClientId = 0; | ||
this.ipv4 = ipv4; | ||
this.onClientInfo = null == onClientInfo ? this._onClientInfo.bind(this) : onClientInfo; | ||
if (null == serverInfo) { | ||
@@ -38,6 +39,5 @@ const os = require("os"); | ||
*/ | ||
startServer(onConnectClient, onDisconnectClient, onClientInfo = null) { | ||
startServer(onConnectClient, onDisconnectClient) { | ||
this.onConnectClient = onConnectClient; | ||
this.onDisconnectClient = onDisconnectClient; | ||
this.onClientInfo = null == onClientInfo ? this._onClientInfo.bind(this) : onClientInfo; | ||
if (this.ipv4) { | ||
@@ -95,3 +95,3 @@ this.tryGetIpv4(this.startListen.bind(this)); | ||
this.server.listen(this.port, this.ipv4 ? "127.0.0.1" : ""); | ||
console.log(`tcp开始侦听:IPv4 ${this.ipv4}:${this.port}`); | ||
console.log(`tcp开始侦听:IPv4 ${this.ipv4} 端口:${this.port}`); | ||
} | ||
@@ -117,3 +117,4 @@ onConnect(sock) { | ||
_onClientInfo(socketId, client) { | ||
console.log(client.toJSON().toString() + `\n socket id:${socketId} 端口:${this.port}`); | ||
const log = `socket id:${socketId} 端口:${this.port}`; | ||
console.log(`${JSON.stringify(client.toJSON())}\n${log}`); | ||
} | ||
@@ -120,0 +121,0 @@ getClient(socketId) { |
{ | ||
"name": "socket_protobuf", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "socket发送protobuf二进制流的SDK", | ||
@@ -5,0 +5,0 @@ "main": "App.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43770
1215