void IListener.Call(params object[] args) { if (Parameters.Failed[0]) { return; } var packet = new EnginePacket(EnginePacket.PING, "probe"); Parameters.Transport[0].Once(Transport.EVENT_PACKET, new ProbeEventPacketListener(this)); Parameters.Transport[0].Send(new List <EnginePacket>() { packet }); }
public bool Call(EnginePacket packet, int index, int total) { if (polling.ReadyState == ReadyStateEnum.OPENING) { polling.OnOpen(); } if (packet.Type == EnginePacket.CLOSE) { polling.OnClose(); return(false); } polling.OnPacket(packet); return(true); }
private void SendPacket(EnginePacket packet, Action fn) { if (fn == null) { fn = () => { }; } Emit(EVENT_PACKET_CREATE, packet); var log = LogManager.GetLogger(GlobalHelper.CallerName()); log.Info(string.Format("SendPacket WriteBuffer.Add(packet) packet ={0}", packet.Type)); lock (WriteBuffer) { WriteBuffer.Add(packet); } CallbackBuffer.Add(fn); Flush(); }
public void OnPacket(EnginePacket packet) { var log = LogManager.GetLogger(GlobalHelper.CallerName()); if (ReadyState == ReadyStateEnum.OPENING || ReadyState == ReadyStateEnum.OPEN) { log.Info(string.Format("socket received: type '{0}', data '{1}'", packet.Type, packet.Data)); Emit(EVENT_PACKET, packet); Emit(EVENT_HEARTBEAT); if (packet.Type == EnginePacket.OPEN) { OnHandshake(new HandshakeData((string)packet.Data)); } else if (packet.Type == EnginePacket.PONG) { this.SetPing(); } else if (packet.Type == EnginePacket.ERROR) { var err = new SocketEngineException("server error"); err.code = packet.Data; this.Emit(EVENT_ERROR, err); } else if (packet.Type == EnginePacket.MESSAGE) { Emit(EVENT_DATA, packet.Data); Emit(EVENT_MESSAGE, packet.Data); } } else { log.Info(string.Format("OnPacket packet received with socket readyState '{0}'", ReadyState)); } }
public static void DecodePayload(byte[] data, IDecodePayloadCallback callback) { EnginePacket.DecodePayload(data, callback); }
public static void EncodePayload(EnginePacket[] packets, IEncodeCallback callback) { EnginePacket.EncodePayload(packets, callback); }
public static EnginePacket DecodePacket(byte[] data) { return(EnginePacket.DecodePacket(data)); }
public static EnginePacket DecodePacket(string data, bool utf8decode = false) { return(EnginePacket.DecodePacket(data, utf8decode)); }
public static void EncodePacket(EnginePacket packet, IEncodeCallback callback) { packet.Encode(callback); }
/// <summary> /// Decodes data when a payload is maybe expected. /// </summary> /// <param name="data"></param> /// <param name="callback"></param> public static void DecodePayload(string data, IDecodePayloadCallback callback) { if (String.IsNullOrEmpty(data)) { callback.Call(_err, 0, 1); return; } var length = new StringBuilder(); for (int i = 0, l = data.Length; i < l; i++) { var chr = Convert.ToChar(data.Substring(i, 1)); if (chr != ':') { length.Append(chr); } else { int n; if (!int.TryParse(length.ToString(), out n)) { callback.Call(_err, 0, 1); return; } string msg; try { msg = data.Substring(i + 1, n); } catch (ArgumentOutOfRangeException) { callback.Call(_err, 0, 1); return; } if (msg.Length != 0) { EnginePacket packet = DecodePacket(msg, true); if (_err.Type == packet.Type && _err.Data == packet.Data) { callback.Call(_err, 0, 1); return; } bool ret = callback.Call(packet, i + n, l); if (!ret) { return; } } i += n; length = new StringBuilder(); } } if (length.Length > 0) { callback.Call(_err, 0, 1); } }
protected void OnPacket(EnginePacket packet) { this.Emit(EVENT_PACKET, packet); }