public static MqMessage Parse(byte header, uint len, MemoryStream stream) { MqMessage msg = null; switch ((MessageType)((header & 0xf0) >> 4)) { case MessageType.CONNECT: msg = new MqConnect(header, len, stream); break; case MessageType.CONNACK: msg = new MqConnack(header, len, stream); break; case MessageType.DISCONNECT: msg = new MqDisconnect(header, len, stream); break; case MessageType.PINGREQ: msg = new MqPingReq(header, len, stream); break; case MessageType.PINGRESP: msg = new MqPingResp(header, len, stream); break; case MessageType.PUBLISH: msg = new MqPublish(header, len, stream); break; case MessageType.SUBSCRIBE: msg = new MqSubscribe(header, len, stream); break; case MessageType.SUBACK: msg = new MqSuback(header, len, stream); break; case MessageType.UNSUBSCRIBE: msg = new MqUnsubscribe(header, len, stream); break; case MessageType.UNSUBACK: msg = new MqUnsuback(header, len, stream); break; case MessageType.PUBACK: case MessageType.PUBCOMP: case MessageType.PUBREC: case MessageType.PUBREL: msg = new MqMsgAck(header, len, stream); break; } return(msg); }
public MqClient() { _waitPingResp=false; _mq=Topic.root.Get("/local/MQ"); ConnInfo=new MqConnect(); ConnInfo.cleanSession=true; ConnInfo.keepAlive=this.KeepAlive; _tOut=new Timer(new TimerCallback(TimeOut)); _settings=Topic.root.Get("/local/cfg/Client"); _subs=new List<Topic.Subscription>(); _now=Topic.root.Get<DateTime>("/var/now"); _nowOffset=_settings.Get<long>("TimeOffset"); }
public static MqMessage Parse(byte header, uint len, MemoryStream stream) { MqMessage msg=null; switch((MessageType)((header & 0xf0) >> 4)) { case MessageType.CONNECT: msg=new MqConnect(header, len, stream); break; case MessageType.CONNACK: msg=new MqConnack(header, len, stream); break; case MessageType.DISCONNECT: msg=new MqDisconnect(header, len, stream); break; case MessageType.PINGREQ: msg=new MqPingReq(header, len, stream); break; case MessageType.PINGRESP: msg=new MqPingResp(header, len, stream); break; case MessageType.PUBLISH: msg=new MqPublish(header, len, stream); break; case MessageType.SUBSCRIBE: msg=new MqSubscribe(header, len, stream); break; case MessageType.SUBACK: msg=new MqSuback(header, len, stream); break; case MessageType.UNSUBSCRIBE: msg=new MqUnsubscribe(header, len, stream); break; case MessageType.UNSUBACK: msg=new MqUnsuback(header, len, stream); break; case MessageType.PUBACK: case MessageType.PUBCOMP: case MessageType.PUBREC: case MessageType.PUBREL: msg=new MqMsgAck(header, len, stream); break; } return msg; }
private void ConnectCB(IAsyncResult rez) { var _tcp = rez.AsyncState as TcpClient; try { _tcp.EndConnect(rez); _stream = new MqStreamer(_tcp, Received, SendIdle); var id = string.Format("{0}_{1:X4}", Environment.MachineName, System.Diagnostics.Process.GetCurrentProcess().Id); var ConnInfo = new MqConnect(); ConnInfo.keepAlive = (ushort)(KEEP_ALIVE / 900); ConnInfo.cleanSession = true; ConnInfo.clientId = id; if (_uName != null) { ConnInfo.userName = _uName; if (_uPass != null) { ConnInfo.userPassword = _uPass; } } this.Send(ConnInfo); _tOut.Change(3000, KEEP_ALIVE); // better often than never } catch (Exception ex) { var se = ex as SocketException; if (se != null && (se.SocketErrorCode == SocketError.ConnectionRefused || se.SocketErrorCode == SocketError.TryAgain || se.SocketErrorCode == SocketError.TimedOut)) { status = Status.Disconnected; _tOut.Change((new Random()).Next(KEEP_ALIVE * 3, KEEP_ALIVE * 6), Timeout.Infinite); } else { status = Status.NotAccepted; _tOut.Change(Timeout.Infinite, Timeout.Infinite); } Log.Warning("{0} Connection FAILED - {1}", this.Signature, ex.Message); } }