コード例 #1
0
        /**
         * <summary>
         * Tries to send packet over network.</summary>
         *
         * <param name="msg">Message being sent.</param>
         * <exception cref="IOException">If client was closed before message was sent over network.</exception>
         */
        private void sendPacket(GridClientRequest msg)
        {
            try {
                byte[] data = marshaller.Marshal(msg);
                byte[] size = U.ToBytes(40 + data.Length);
                byte[] head = new byte[1 + size.Length + 40 + data.Length];

                // Prepare packet.
                head[0] = (byte)0x90;
                Array.Copy(size, 0, head, 1, 4);
                Array.Copy(GridClientUtils.ToBytes(msg.RequestId), 0, head, 5, 8);
                Array.Copy(GridClientUtils.ToBytes(msg.ClientId), 0, head, 13, 16);
                Array.Copy(GridClientUtils.ToBytes(msg.DestNodeId), 0, head, 29, 16);
                Array.Copy(data, 0, head, 45, data.Length);

                // Enqueue packet to send.
                lock (stream) {
                    stream.Write(head, 0, head.Length);
                    stream.Flush();
                }

                lastPacketSndTime = U.Now;
            }
            catch (IOException e) {
                // In case of IOException we should shutdown the whole client since connection is broken.
                Close(false);

                throw new GridClientConnectionResetException("Failed to send message over network (will try to " +
                                                             "reconnect): " + ServerAddress, e);
            }
        }
コード例 #2
0
        /**
         * <summary>
         * Tries to send packet over network.</summary>
         *
         * <param name="msg">Message being sent.</param>
         * <exception cref="IOException">If client was closed before message was sent over network.</exception>
         */
        private void sendPacket(GridClientRequest msg)
        {
            try {
                MemoryStream buf = new MemoryStream(1024);

                // Header.
                buf.WriteByte((byte)0x90);

                // Reserve place for size.
                buf.WriteByte((byte)0);
                buf.WriteByte((byte)0);
                buf.WriteByte((byte)0);
                buf.WriteByte((byte)0);

                buf.Write(GridClientUtils.ToBytes(msg.RequestId), 0, 8);
                buf.Write(GridClientUtils.ToBytes(msg.ClientId), 0, 16);
                buf.Write(GridClientUtils.ToBytes(msg.DestNodeId), 0, 16);

                marshaller.Marshal(msg, buf);

                int len = (int)buf.Length;

                buf.Seek(1, SeekOrigin.Begin);

                buf.Write(U.ToBytes(len - 1 - 4), 0, 4);

                buf.Position = len;

                q.Add(buf);
            }
            catch (Exception e) {
                // In case of Exception we should shutdown the whole client since connection is broken
                // and future for this reques will never be completed.
                Close(false);

                throw new GridClientConnectionResetException("Failed to send message over network (will try to " +
                                                             "reconnect): " + ServerAddress, e);
            }
        }