public override void Close() { try { base.Close(); if (socket != null) { socket.Shutdown(SocketShutdown.Both); socket.Close(); } socket = null; ChangeNetworkState(NetworkState.Closed); } catch (Exception e) { NDebug.Log(e); } }
public void OnPushMessage(UInt32 routeId, byte[] data) { var evt = GetEventListener(routeId); if (evt == null) { NDebug.Log("未监听: " + routeId); return; } try { evt.callback.Invoke(data); }catch (Exception e) { NDebug.Log(e); } }
public void OnReadBytes(byte[] data) { try { Packet pkg = handleBytes(data); do { if (pkg == null) { break; } processPacket(pkg); pkg = handleBytes(null); } while (true); } catch (Exception e) { NDebug.Log(e); } }
void processPacket(Packet pkg) { //NDebug.Log("processPacket:" + pkg.Type); switch (pkg.Type) { case PacketType.PacketType_DATA: { // 解析request ID var data = pkg.Data; var headerSize = 8; UInt32 requestID = ((UInt32)data[0]) << 24 | ((UInt32)data[1]) << 16 | ((UInt32)data[2]) << 8 | ((UInt32)data[3]); UInt32 statusCode = ((UInt32)data[4]) << 24 | ((UInt32)data[5]) << 16 | ((UInt32)data[6]) << 8 | ((UInt32)data[7]); var Body = new byte[data.Length - headerSize]; Buffer.BlockCopy(data, headerSize, Body, 0, data.Length - headerSize); if (RequestMap.ContainsKey(requestID)) { var cb = RequestMap[requestID]; RequestMap.Remove(requestID); try { cb.Invoke((int)statusCode, Body); } catch (Exception e) { NDebug.Log(e); client.LastError = e.ToString(); } } else { NDebug.Log("not request id not found."); } } break; case PacketType.PacketType_KICK: { NDebug.Log("踢下线了"); } break; case PacketType.PacketType_PUSH: { // 解析routeId var data = pkg.Data; UInt32 routeId = ((UInt32)data[0]) << 24 | ((UInt32)data[1]) << 16 | ((UInt32)data[2]) << 8 | ((UInt32)data[3]); var Body = new byte[data.Length - 4]; Buffer.BlockCopy(data, 4, Body, 0, data.Length - 4); client.OnPushMessage(routeId, Body); } break; default: NDebug.Log("type not support"); break; } }