Example #1
0
 private void SendSubscribe(Connection conn, Subscription sub)
 {
     string queue = sub.Options.Queue;
     conn.Send("SUB " + sub.Topic + " " + queue + " " + sub.Id);
 }
Example #2
0
 private void SendUnsubscribe(Connection conn, long sid)
 {
     conn.Send("UNSUB " + sid + " 0");
 }
Example #3
0
 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 });
 }
Example #4
0
        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;
                    }
                }
            }
        }
Example #5
0
 private void Dispose(bool disposing)
 {
     ThrowIfDisposed();
     if (disposing)
     {
         _cancellation.Cancel();
         SyncedIf(() => _processor != null, () => {
             var conn = _connection;
             _connection = null;
             if (conn != null)
             {
                 conn.Dispose();
             }
         });
         if (_processor != null)
         {
             _processor.Join();
             _processor = null;
         }
         _cancellation.Dispose();
         _cancellation = null;
     }
     _disposed = true;
 }
Example #6
0
 private void Disconnect(Connection conn)
 {
     lock (_stateLock)
     {
         if (_connection != null && _connection == conn)
         {
             _connection.Dispose();
             _connection = null;
             return;
         }
     }
     conn.Dispose();
 }
Example #7
0
        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;
            }
        }