/**
         * <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);
            }
        }
Exemple #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);
            }
        }
 /**
  * <summary>
  * Converts Guid to a byte array.</summary>
  *
  * <param name="id">Guid to convert.</param>
  * <returns>Converted bytes.</returns>
  */
 private static ByteString WrapGuid(Guid id)
 {
     return(ByteString.CopyFrom(U.ToBytes(id)));
 }
        /**
         * <summary>
         * Converts java object to a protocol-understandable format.</summary>
         *
         * <param name="val">Value to convert.</param>
         * <returns>Wrapped protocol message object.</returns>
         */
        public static ObjectWrapper WrapObject(Object val)
        {
            ObjectWrapper.Builder builder = ObjectWrapper.CreateBuilder();

            // Primitives.

            if (val == null)
            {
                builder
                .SetType(ObjectWrapperType.NONE)
                .SetBinary(ByteString.Empty);
            }
            else if (val is bool)
            {
                builder
                .SetType(ObjectWrapperType.BOOL)
                .SetBinary(ByteString.CopyFrom(new byte[] { (byte)((bool)val == true ? 1 : 0) }));
            }
            else if (val is byte)
            {
                builder
                .SetType(ObjectWrapperType.BYTE)
                .SetBinary(ByteString.CopyFrom(new byte[] { (byte)val }));
            }
            else if (val is short)
            {
                builder
                .SetType(ObjectWrapperType.SHORT)
                .SetBinary(ByteString.CopyFrom(U.ToBytes((short)val)));
            }
            else if (val is int)
            {
                builder
                .SetType(ObjectWrapperType.INT32)
                .SetBinary(ByteString.CopyFrom(U.ToBytes((int)val)));
            }
            else if (val is long)
            {
                builder
                .SetType(ObjectWrapperType.INT64)
                .SetBinary(ByteString.CopyFrom(U.ToBytes((long)val)));
            }
            else if (val is float)
            {
                builder
                .SetType(ObjectWrapperType.FLOAT)
                .SetBinary(ByteString.CopyFrom(U.ToBytes((float)val)));
            }
            else if (val is double)
            {
                builder
                .SetType(ObjectWrapperType.DOUBLE)
                .SetBinary(ByteString.CopyFrom(U.ToBytes((double)val)));
            }
            else if (val is string)
            {
                builder
                .SetType(ObjectWrapperType.STRING)
                .SetBinary(ByteString.CopyFromUtf8((string)val));
            }
            else if (val is Guid)
            {
                builder
                .SetType(ObjectWrapperType.UUID)
                .SetBinary(WrapGuid((Guid)val));
            }
            else if (val is byte[])
            {
                builder
                .SetType(ObjectWrapperType.BYTES)
                .SetBinary(ByteString.CopyFrom((byte[])val));
            }

            // Common objects.

            else if (val is sc::IDictionary)
            {
                // Note! Map's check should goes BEFORE collections check due to IDictionary is inherited from ICollection.
                builder
                .SetType(ObjectWrapperType.MAP)
                .SetBinary(WrapMap((sc::IDictionary)val).ToByteString());
            }
            else if (val is sc::ICollection)
            {
                builder
                .SetType(ObjectWrapperType.COLLECTION)
                .SetBinary(WrapCollection((sc::ICollection)val).ToByteString());
            }

            // Requests.

            else if (val is GridClientAuthenticationRequest)
            {
                builder
                .SetType(ObjectWrapperType.AUTH_REQUEST)
                .SetBinary(WrapAuthRequest((GridClientAuthenticationRequest)val).ToByteString());
            }
            else if (val is GridClientCacheRequest)
            {
                builder
                .SetType(ObjectWrapperType.CACHE_REQUEST)
                .SetBinary(WrapCacheRequest((GridClientCacheRequest)val).ToByteString());
            }
            else if (val is GridClientTaskRequest)
            {
                builder
                .SetType(ObjectWrapperType.TASK_REQUEST)
                .SetBinary(WrapTaskRequest((GridClientTaskRequest)val).ToByteString());
            }
            else if (val is GridClientLogRequest)
            {
                builder
                .SetType(ObjectWrapperType.LOG_REQUEST)
                .SetBinary(WrapLogRequest((GridClientLogRequest)val).ToByteString());
            }
            else if (val is GridClientTopologyRequest)
            {
                builder
                .SetType(ObjectWrapperType.TOPOLOGY_REQUEST)
                .SetBinary(WrapTopologyRequest((GridClientTopologyRequest)val).ToByteString());
            }

            // Responses.

            else if (val is GridClientResponse)
            {
                builder
                .SetType(ObjectWrapperType.RESPONSE)
                .SetBinary(WrapResultBean((GridClientResponse)val).ToByteString());
            }
            else if (val is GridClientNodeBean)
            {
                builder
                .SetType(ObjectWrapperType.NODE_BEAN)
                .SetBinary(WrapNode((GridClientNodeBean)val).ToByteString());
            }
            else if (val is GridClientTaskResultBean)
            {
                builder
                .SetType(ObjectWrapperType.TASK_BEAN)
                .SetBinary(WrapTaskResult((GridClientTaskResultBean)val).ToByteString());
            }

            // To-string conversion for special cases.

            else if (val is Enum)
            {
                builder
                .SetType(ObjectWrapperType.STRING)
                .SetBinary(ByteString.CopyFromUtf8(Enum.GetName(val.GetType(), val)));
            }
            else if (val is System.Net.IPAddress)
            {
                builder
                .SetType(ObjectWrapperType.STRING)
                .SetBinary(ByteString.CopyFromUtf8(val.ToString()));
            }

            // In all other cases.

            else
            {
                throw new ArgumentException("Failed to serialize object (object serialization" +
                                            " of given type is not supported): " + val.GetType());
            }

            return(builder.Build());
        }