コード例 #1
0
        public void Publish(IMessage message, ICompletionListener listener)
        {
            if (!IsConnected())
            {
                throw new Exception("not connected");
            }

            JsonObject envelope = new JsonObject();

            // synchronized to ensure message sequence numbers are ordered
            lock (writeLock)
            {
                long seqNum = Interlocked.Increment(ref messageIdGenerator);

                envelope.Add(ProtocolConstants.OP_FIELD, ProtocolOpConstants.OP_MESSAGE);
                envelope.Add(ProtocolConstants.BODY_FIELD, ((JSONMessage)message)._rawData());

                if (qos)
                {
                    envelope.Add(ProtocolConstants.SEQ_NUM_FIELD, seqNum);
                }

                PublishContext ctx = new PublishContext(seqNum, envelope.ToString(), message, listener);

                if (maxMessageSize > 0 && ctx.GetJson().Length > maxMessageSize)
                {
                    throw new Exception("maximum message size exceeded");
                }

                requests.Put(seqNum, ctx);

                Queue(ctx);
            }
        }
コード例 #2
0
        public void Run()
        {
            try
            {
                while (webSocket.isConnected())
                {
                    PublishContext ctx = null;

                    lock (writeQueueLock)
                    {
                        if (writeQueue.Count == 0)
                        {
                            Monitor.Wait(writeQueueLock);
                        }

                        if (writerInterrupted)
                        {
                            break;
                        }

                        ctx = writeQueue.Dequeue();
                    }

                    if (ctx == DISCONNECT)
                    {
                        // don't send the disconnect until the message(s) currently^M
                        // being processed have finished and acknowledged^M
                        lock (processLock)
                        {
                            // send a disconnect message and close
                            JsonObject message = new JsonObject();
                            message[ProtocolConstants.OP_FIELD] = ProtocolOpConstants.OP_DISCONNECT;
                            webSocket.send(message.ToString());
                            webSocket.close();
                        }
                    }
                    else
                    {
                        webSocket.send(ctx.GetJson());

                        if (!qos && ctx.GetSeqNum() > 0)
                        {
                            publishComplete(ctx.GetSeqNum());
                        }
                    }
                }
            }
            catch (System.Threading.ThreadAbortException) { }
            catch (Exception)
            {
                // expected
            }
        }
コード例 #3
0
        public void SendReply(IMessage reply, IMessage request, ICompletionListener listener)
        {
            if (!IsConnected())
            {
                throw new Exception("not connected");
            }

            if (((JSONMessage)request).ReplyTo == null)
            {
                throw new ArgumentException("not a request message");
            }

            JsonObject envelope = new JsonObject();

            // synchronized to ensure message sequence numbers are ordered
            lock (writeLock)
            {
                long seqNum = Interlocked.Increment(ref messageIdGenerator);

                envelope.Add(ProtocolConstants.OP_FIELD, ProtocolOpConstants.OP_REPLY);
                envelope.Add(ProtocolConstants.SEQ_NUM_FIELD, seqNum);
                envelope.Add(ProtocolConstants.TO_FIELD, ((JSONMessage)request).ReplyTo);
                envelope.Add(ProtocolConstants.REQ_ID_FIELD, ((JSONMessage)request).ReqId);
                envelope.Add(ProtocolConstants.BODY_FIELD, ((JSONMessage)reply)._rawData());

                PublishContext ctx = new PublishContext(seqNum, envelope.ToString(), reply, listener);

                if (protocol < 1)
                {
                    throw new NotSupportedException("send reply is not supported with this server");
                }

                if (maxMessageSize > 0 && ctx.GetJson().Length > maxMessageSize)
                {
                    throw new Exception("maximum message size exceeded");
                }

                requests.Put(seqNum, ctx);

                Queue(ctx);
            }
        }