private void SendUnsubscribe(Connection conn, long sid) { conn.Send("UNSUB " + sid + " 0"); }
private void SendSubscribe(Connection conn, Subscription sub) { string queue = sub.Options.Queue; conn.Send("SUB " + sub.Topic + " " + queue + " " + sub.Id); }
private void ReceiveMessages(Connection conn) { Message msg = null; while (!_cancellation.Token.IsCancellationRequested) { Debug.WriteLine("NATS-READ"); if (msg != null) { var buf = new char[msg.BodyLen]; conn.Reader.ReadBlock(buf, 0, buf.Length); msg.Body = new string(buf); msg = DispatchMessage(msg); } else { var line = conn.Reader.ReadLine(); Debug.WriteLine(line, "NATS"); var tokens = line.Split(new char[] {' '}); switch (tokens[0]) { case "MSG": msg = ParseMsg(tokens); if (msg != null && msg.BodyLen == 0) { msg.Body = string.Empty; msg = DispatchMessage(msg); } else { Trace.TraceWarning("NATS-MSG: INVALID {0}", line); } break; case "PING": conn.Send("PONG"); break; case "-ERR": Trace.TraceError("NATS-ERR: {0}", line); // Reconnect Disconnect(conn); return; default: // accepting "PONG", "INFO", "+OK" break; } } } }
private void SendPublish(Connection conn, string topic, string message, string replyTopic) { string reply = string.IsNullOrEmpty(replyTopic) ? " " : (" " + replyTopic + " "); conn.Send(new string[]{ "PUB " + topic + reply + message.Length, message }); }
private Connection ConnectServer() { lock (_stateLock) { if (_connection != null) { return _connection; } if (_processor == null) { return null; } Connection newConn = null; try { newConn = new Connection(_serverAddr, _serverPort); newConn.Send(_connectMsg); foreach (var sub in _subscriptions.Values) { SendSubscribe(newConn, sub); } _connection = newConn; } catch (SocketException err) { Trace.TraceError("NATS-CLIENT-EXCEPTION: {0}", err); } catch (IOException err) { Trace.TraceError("NATS-CLIENT-EXCEPTION: {0}", err); } finally { if (_connection != newConn) { newConn.Dispose(); } } return _connection; } }