Esempio n. 1
0
        /// <inheritdoc />
        protected override async Task <Result <Unit, IError> > Run(
            IStateMonad stateMonad,
            CancellationToken cancellationToken)
        {
            var left = await Left.Run(stateMonad, cancellationToken);

            if (left.IsFailure)
            {
                return(left.ConvertFailure <Unit>());
            }

            var right = await Right.Run(stateMonad, cancellationToken);

            if (right.IsFailure)
            {
                return(right.ConvertFailure <Unit>());
            }

            var r = left.Value is not null && left.Value.Equals(right.Value);

            if (r)
            {
                return(Unit.Default);
            }

            var lString = await SerializationMethods.GetStringAsync(left.Value);

            var rString = await SerializationMethods.GetStringAsync(right.Value);

            var error = ErrorCode.AssertionFailed
                        .ToErrorBuilder($"Expected {lString} to equal {rString}")
                        .WithLocation(this);

            return(Result.Failure <Unit, IError>(error));
        }
Esempio n. 2
0
        private void HandleFileUpload(NetworkStream networkStream)
        {
            var header_len_bytes = new byte[sizeof(int)];

            networkStream.Read(header_len_bytes, 0, header_len_bytes.Length);
            var header_len   = BitConverter.ToInt32(header_len_bytes, 0);
            var header_bytes = new byte[header_len];

            networkStream.Read(header_bytes, 0, header_bytes.Length);
            var header         = (ClientUploadFileHeader)SerializationMethods.Deserialize(header_bytes);
            var session        = new FileUploadSession(FileDB, header.Filename);
            var totalBytesRead = 0;

            while (totalBytesRead < header.FileSize)
            {
                var data      = new byte[BlockSize];
                var bytesRead = networkStream.Read(data, 0, data.Length);
                session.WriteToFile(data, bytesRead);
                totalBytesRead += bytesRead;
            }
            session.TerminateSession();
            FileDB.FileEntries.Add(new FileEntry {
                Name = header.Filename, Size = header.FileSize
            });
            var response_buffer = BitConverter.GetBytes((int)ResponeType.Success);

            networkStream.Write(response_buffer, 0, sizeof(int));
            NewFileUploaded?.Invoke(this, new NewFileUploadedEventArgs {
                FileName = header.Filename, FileSize = header.FileSize
            });
        }
Esempio n. 3
0
        private SerializationMethods CreateSerializationMethods(TypeInformation typeInformation)
        {
            string        typeName                = BuildTypeName(typeInformation);
            TypeBuilder   typeBuilder             = _module.DefineType(typeName, TypeAttributes.Public | TypeAttributes.Class);
            MethodBuilder writeValueNotNullMethod = typeBuilder.DefineMethod("WriteValueNotNull",
                                                                             MethodAttributes.Public | MethodAttributes.Static,
                                                                             CallingConventions.Standard, typeof(void), new[]
            {
                typeof(BinaryWriter),
                typeInformation.Type,
                typeof(ISerializer),
                typeof(IRemotingEndPoint)
            });

            MethodInfo    writeObjectMethod      = CreateWriteValueWithTypeInformation(typeBuilder, writeValueNotNullMethod, typeInformation.Type);
            MethodInfo    writeValueMethod       = CreateWriteValue(typeBuilder, writeValueNotNullMethod, typeInformation.Type);
            MethodBuilder readValueNotNullMethod = typeBuilder.DefineMethod("ReadValueNotNull",
                                                                            MethodAttributes.Public | MethodAttributes.Static,
                                                                            CallingConventions.Standard, typeInformation.Type,
                                                                            new[]
            {
                typeof(BinaryReader),
                typeof(ISerializer),
                typeof(IRemotingEndPoint)
            });

            MethodInfo readObjectMethod = CreateReadObject(typeBuilder, readValueNotNullMethod, typeInformation.Type);
            MethodInfo readValueMethod  = CreateReadValue(typeBuilder, readValueNotNullMethod, typeInformation.Type);

            var m = new SerializationMethods(
                writeValueMethod,
                writeObjectMethod,
                readValueMethod,
                readObjectMethod);

            _serializationMethods.Add(typeInformation.Type, m);

            try
            {
                EmitWriteValueNotNullMethod(writeValueNotNullMethod.GetILGenerator(), typeInformation);
                EmitReadValueNotNullMethod(readValueNotNullMethod.GetILGenerator(), typeInformation);

                typeBuilder.CreateType();

                m.WriteDelegate =
                    (Action <BinaryWriter, object, ISerializer, IRemotingEndPoint>)
                    typeBuilder.GetMethod("WriteObject").CreateDelegate(typeof(Action <BinaryWriter, object, ISerializer, IRemotingEndPoint>));

                m.ReadObjectDelegate =
                    (Func <BinaryReader, ISerializer, IRemotingEndPoint, object>)
                    typeBuilder.GetMethod("ReadObject").CreateDelegate(typeof(Func <BinaryReader, ISerializer, IRemotingEndPoint, object>));

                return(m);
            }
            catch (Exception)
            {
                _serializationMethods.Remove(typeInformation.Type);
                throw;
            }
        }
Esempio n. 4
0
        private void Run(object t)
        {
            var token = (CancellationToken)t;

            while (token.IsCancellationRequested == false)
            {
                var deliveryArgs = _subscription.Next();

                try
                {
                    var body = SerializationMethods.FromByteArray <TRequest>(deliveryArgs.Body);

                    TResponse response = _processRequest(body);

                    // Send Response
                    var replyProperties = _model.CreateBasicProperties();
                    replyProperties.CorrelationId = deliveryArgs.BasicProperties.CorrelationId;
                    _model.BasicPublish("", deliveryArgs.BasicProperties.ReplyTo, replyProperties, response.ToByteArray());

                    // Send acknowledge that received (and processed)
                    _model.BasicAck(deliveryArgs.DeliveryTag, false);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception:" + ex.Message);
                }
            }
        }
Esempio n. 5
0
        public void Start()
        {
            try
            {
                using (var context = ZmqContext.Create())
                {
                    using (var socket = context.CreateSocket(SocketType.PULL))
                    {
                        foreach (var bindEndPoint in _bindEndPoints)
                        {
                            socket.Bind(bindEndPoint);
                        }

                        while (true)
                        {
                            var messageFrame = socket.ReceiveFrame();
                            var request      = SerializationMethods.FromByteArray <T>(messageFrame.Buffer);
                            _process(request);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// TODO: Replace with <see cref="SerializationMethodStorage{T}.GetOrAdd"/>.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="serializationMethods"></param>
        internal void RegisterType(Type type, out SerializationMethods serializationMethods)
        {
            lock (_serializationMethods)
            {
                // Usually we already have generated the methods necessary to serialize / deserialize
                // and thus we can simply retrieve them from the dictionary
                if (!_serializationMethods.TryGetValue(type, out serializationMethods))
                {
                    // If that's not the case, then we'll have to generate them.
                    // However we need to pay special attention to certain types, for example ByReference
                    // types where the serialization method is IDENTICAL for each implementation.
                    //
                    // Usually we would call PatchType() everytime, however this method is very time-expensive
                    // and therefore we will register both the type as well as the patched type, which
                    // causes subsequent calls to RegisterType to no longer invoke PatchType.
                    //
                    // In essence PatchType is only ever invoked ONCE per type instead of for every call to RegisterType.
                    var patchedType = PatchType(type);
                    if (!_serializationMethods.TryGetValue(patchedType, out serializationMethods))
                    {
                        var typeInfo = new TypeInformation(patchedType);
                        serializationMethods = CreateSerializationMethods(typeInfo);

                        if (type != patchedType)
                        {
                            _serializationMethods.Add(type, serializationMethods);
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        public static Dictionary <String, Byte[]> getDirectoryTree(String pathFisico, String pathRelativo)
        {
            FileAttributes attr = File.GetAttributes(@pathFisico);

            if ((attr & FileAttributes.Directory) == FileAttributes.Directory)//maybe  fastest way http://stackoverflow.com/questions/1395205/better-way-to-check-if-path-is-a-file-or-a-directory
            {
                Dictionary <String, Byte[]> mappaFiles = new Dictionary <String, Byte[]>();

                String[] files = Directory.GetFiles(pathFisico, "*", SearchOption.TopDirectoryOnly);
                foreach (string file in files)
                {
                    String pathRelFile = /*pathRelativo + "/" + */ Path.GetFileName(file);
                    //mappaFiles.Add(pathRelFile,getDirectoryTree(file, pathRelFile));

                    mappaFiles.Add(pathRelFile, getFile(file));
                }
                String[] folders = Directory.GetDirectories(pathFisico, "*", SearchOption.TopDirectoryOnly);
                foreach (string folder in folders)
                {
                    String pathRelFold = /*pathRelativo + "/" + */ Path.GetFileName(folder);
                    Dictionary <String, Byte[]> subDir = getDirectoryTree(folder, pathRelFold);
                    mappaFiles.Add(pathRelFold, SerializationMethods.ToByteArray(subDir, Marshal.SizeOf(subDir)));
                }
                return(mappaFiles);
            }
            else
            {
                throw new Exception("not supported");
                //return File.ReadAllBytes(pathFisico);//http://msdn.microsoft.com/en-us/library/ms997518.aspx
            }
        }
Esempio n. 8
0
                /// <inheritdoc />
                public string Serialize(IEnumerable <StepProperty> stepProperties)
                {
                    StringBuilder sb = new();

                    sb.Append('$');
                    sb.Append('"');

                    foreach (var step in stepProperties.Cast <StepProperty.StepListProperty>()
                             .Single()
                             .StepList)
                    {
                        if (step is StringConstant sc)
                        {
                            sb.Append(SerializationMethods.Escape(sc.Value.GetString()));
                        }
                        else
                        {
                            sb.Append('{');
                            var ser = step.Serialize();
                            sb.Append(ser);
                            sb.Append('}');
                        }
                    }

                    sb.Append('"');

                    return(sb.ToString());
                }
        private void Receive(object t)
        {
            var token = (CancellationToken)t;

            while (token.IsCancellationRequested == false)
            {
                try
                {
                    var ea = _consumer.Queue.Dequeue();

                    var body = SerializationMethods.FromByteArray <TResponse>(ea.Body);

                    var success = _processRequest(body);
                }
                catch (EndOfStreamException ex)
                {
                    // This comes when connection is closed (some better way to detect this?)
                    if (ex.Message == "SharedQueue closed")
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception:" + ex.Message);
                }
            }
        }
Esempio n. 10
0
        private void _zmqSocket_ReceiveReady(object sender, NetMQSocketEventArgs e)
        {
            var msq     = e.Socket.ReceiveMessage();
            var request = SerializationMethods.FromByteArray <TRequest>(msq[1].Buffer);

            _action(request);
        }
Esempio n. 11
0
        public void Start(U startResponse)
        {
            try
            {
                using (var context = ZmqContext.Create())
                {
                    using (var socket = context.CreateSocket(SocketType.REQ))
                    {
                        foreach (var connectEndpoint in _bindEndPoints)
                        {
                            socket.Connect(connectEndpoint);
                        }

                        var nextResponse = startResponse;

                        while (true)
                        {
                            var responseFrame = new Frame(nextResponse.ToByteArray());
                            socket.SendFrame(responseFrame);

                            var messageFrame = socket.ReceiveFrame();
                            var request      = SerializationMethods.FromByteArray <T>(messageFrame.Buffer);

                            var reply = _process(request);

                            nextResponse = reply;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Esempio n. 12
0
        private void SendFilesList(NetworkStream networkStream)
        {
            var data   = SerializationMethods.Serialize(FileDB.FileEntries);
            var length = BitConverter.GetBytes(data.Length);

            networkStream.Write(length, 0, sizeof(int));
            networkStream.Write(data, 0, data.Length);
        }
Esempio n. 13
0
 public static bool SerializeToDisk(object request, SerializationMethods Method, string Filename)
 {
     if (Method == SerializationMethods.XML)
     {
         return(SerializeXMLToDisk(request, Filename));
     }
     return(SerializeBinaryToDisk(request, Filename));
 }
Esempio n. 14
0
        public void TestSerializeToFile()
        {
            long bytesWritten   = SerializationMethods.SerializeToFile(birthday, "./Serialized/birthday.bday");
            long bytesWrittenDD = SerializationMethods.SerializeToFile(deathday, "./Serialized/deathday.dday");

            Assert.IsTrue(bytesWritten == 258);
            Assert.IsTrue(bytesWrittenDD == 399);
        }
Esempio n. 15
0
        private async void _socket_ReceiveReady(object sender, NetMQSocketEventArgs e)
        {
            var msg = e.Socket.ReceiveMessage();

            // Response socket has index 1, Dealer 2
            var request = SerializationMethods.FromByteArray <TData>(msg[1].Buffer);

            await DoWork(request);
        }
        private static object ExecuteMethod <T>(this T instance, SerializationMethods method, params object[] args)
        {
            lock (SerializationExtensions.syncObject)
            {
                object     methodArg    = args.FirstOrDefault();
                object[]   methodParams = args.Skip(1).ToArray();
                MethodInfo methodInfo   = (method == SerializationMethods.Serialize) ? SerializationExtensions.validFormatters[typeof(T)].Item1 : SerializationExtensions.validFormatters[typeof(T)].Item2;

                return(methodInfo.Invoke(methodArg, methodParams));
            }
        }
        void pullSocket_ReceiveReady(object sender, SocketEventArgs e)
        {
            var messageFrame = e.Socket.ReceiveFrame();
            var request      = SerializationMethods.FromByteArray <TRequest>(messageFrame.Buffer);

            var success = _clientProcess(request);

            if (success)
            {
                _requests.Add(request);
            }
        }
Esempio n. 18
0
        private static ClientDownloadFileHeader GetClientDownloadFileHeader(NetworkStream networkStream)
        {
            var headerLengthBytes = new byte[sizeof(int)];

            networkStream.Read(headerLengthBytes, 0, headerLengthBytes.Length);
            var headerLength = BitConverter.ToInt32(headerLengthBytes, 0);
            var headerBytes  = new byte[headerLength];

            networkStream.Read(headerBytes, 0, headerBytes.Length);
            var header = (ClientDownloadFileHeader)SerializationMethods.Deserialize(headerBytes);

            return(header);
        }
Esempio n. 19
0
 protected override void ExecutionMethod()
 {
     try
     {
         var msq     = _zmqSocket.ReceiveMessage();
         var request = SerializationMethods.FromByteArray <TRequest>(msq[1].Buffer);
         _action(request);
     }
     catch (Exception)
     {
         Debugger.Break();
         throw;
     }
 }
Esempio n. 20
0
        private void HandleFileDownload(NetworkStream networkStream)
        {
            // Get client download file header
            var clientHeader = GetClientDownloadFileHeader(networkStream);
            // Check file availability
            var query = FileDB.FileEntries.Where(f => f.Name == clientHeader.Filename);

            if (query.Count() < 1 || query.First().ToBeDeleted)
            {
                var negativeResponseBytes = BitConverter.GetBytes((int)ResponeType.Failure);
                networkStream.Write(negativeResponseBytes, 0, negativeResponseBytes.Length);
                return;
            }
            else
            {
                ++query.First().CurrentUses;
                var positiveResponseBytes = BitConverter.GetBytes((int)ResponeType.Success);
                networkStream.Write(positiveResponseBytes, 0, positiveResponseBytes.Length);
            }
            int fileSize      = FileDB.GetFileLength(clientHeader.Filename);
            int numBlocks     = fileSize / BlockSize;
            int lastBlockSize = fileSize % BlockSize;
            var serverHeader  = new ServerDownloadFileHeader()
            {
                FileSize      = fileSize,
                NumBlocks     = numBlocks,
                LastBlockSize = lastBlockSize
            };
            var serverHeaderBytes       = SerializationMethods.Serialize(serverHeader);
            var serverHeaderLengthBytes = BitConverter.GetBytes(serverHeaderBytes.Length);

            networkStream.Write(serverHeaderLengthBytes, 0, serverHeaderLengthBytes.Length);
            networkStream.Write(serverHeaderBytes, 0, serverHeaderBytes.Length);
            var downloadSession = new FileDownloadSession(FileDB, clientHeader.Filename);

            for (int i = 0; i < numBlocks; i++)
            {
                var fileData = FileDB.GetFileDataAtV2(downloadSession, i * BlockSize, (i + 1) * BlockSize - 1);
                networkStream.Write(fileData, 0, fileData.Length);
            }
            if (lastBlockSize > 0)
            {
                var file_data = FileDB.GetFileDataAtV2(downloadSession, numBlocks * BlockSize, fileSize - 1);
                networkStream.Write(file_data, 0, file_data.Length);
            }
            downloadSession.TerminateSession();
            --query.First().CurrentUses;
        }
        private void repSocket_ReceiveReady(object sender, SocketEventArgs e)
        {
            var rcvdMsg  = e.Socket.ReceiveFrame();
            var response = SerializationMethods.FromByteArray <TResponse>(rcvdMsg.Buffer);

            var success = _workerProcess(response);

            if (success)
            {
                // NOTE: _requests will block other REQs from connecting
                // Should use e.g. some Pirate pattern
                var request = _requests.Take();
                var frame   = new Frame(request.ToByteArray());
                e.Socket.SendFrame(frame);
            }
        }
        public void ToAndFromByteArray()
        {
            var dh = new DataRequest {
                Method = 2
            };

            ProtoBufHelper.Prepare(typeof(Object), typeof(DataRequest));

            var bytes = dh.ToByteArray();

            Assert.IsTrue(bytes.Length > 0, "Length is zero");

            var deserialized = SerializationMethods.FromByteArray <DataRequest>(bytes);

            Assert.AreEqual(dh.Method, deserialized.Method, "Not serialized correctly");
        }
Esempio n. 23
0
        public static IDictionary <String, Byte[]> getCurrentClipboard()
        {
            IDictionary <String, Byte[]> clipboardMap = new Dictionary <String, Byte[]>();;

            IDataObject iData = Clipboard.GetDataObject();

            String[] formats = iData.GetFormats();
            clipboardMap.Clear();
            // Object o;
            foreach (string format in formats)
            {
                if (format == DataFormats.FileDrop)
                {
                    IDictionary <String, Byte[]> filesMap = new Dictionary <String, Byte[]>();
                    String[] files = (String[])iData.GetData(format);
                    foreach (string file in files)
                    {
                        Dictionary <String, Byte[]> sub = getDirectoryTree(file, Path.GetFileName(file));
                        filesMap.Add(Path.GetFileName(file), SerializationMethods.ToByteArray(sub, Marshal.SizeOf(sub)));
                    }
                    clipboardMap.Add(DataFormats.FileDrop + "MyStruct", SerializationMethods.ToByteArray(filesMap, Marshal.SizeOf(filesMap)));
                }
                //else{
                try
                {
                    if (Marshal.IsComObject(iData.GetData(format)))
                    {
                        int a;
                    }
                }
                catch (Exception e)
                {
                }

                /*!!!rem      clipboardMap.Add(format, iData.GetData(format));
                 *    if (format == "FileName" || format == "FileNameW"|| format == DataFormats.FileDrop)
                 *    {
                 *        String[] files = (string[])clipboardMap[format];
                 *        for (int i = 0; i < files.Length; i++)
                 *            files[i] = Path.GetFileName(files[i]);
                 *    }
                 *///}
            }

            return(clipboardMap);
        }
Esempio n. 24
0
        private async void _socket_ReceiveReady(object sender, NetMQSocketEventArgs e)
        {
            var msg = e.Socket.ReceiveMessage();

            // Response socket has index 1, Dealer 2
            var request = SerializationMethods.FromByteArray <TRequest>(msg[1].Buffer);

            var response = await DoWork(request);

            var envelope = new NetMQFrame(Encoding.UTF8.GetBytes(response.ToString()));
            var body     = new NetMQFrame(response.ToByteArray());

            msg.Append(envelope);
            msg.Append(body);

            e.Socket.SendMessage(msg);
        }
Esempio n. 25
0
        /// <inheritdoc />
        protected override async Task <Result <Unit, IError> > Run(
            IStateMonad stateMonad,
            CancellationToken cancellationToken)
        {
            var r = await Value.Run(stateMonad, cancellationToken);

            if (r.IsFailure)
            {
                return(r.ConvertFailure <Unit>());
            }

            string stringToPrint = await SerializationMethods.GetStringAsync(r.Value);

            stateMonad.ExternalContext.Console.WriteLine(stringToPrint);

            return(Unit.Default);
        }
Esempio n. 26
0
        /// <inheritdoc />
        protected override async Task <Result <Unit, IError> > Run(
            IStateMonad stateMonad,
            CancellationToken cancellationToken)
        {
            var r = await Value.Run(stateMonad, cancellationToken);

            if (r.IsFailure)
            {
                return(r.ConvertFailure <Unit>());
            }

            var stringToPrint = await SerializationMethods.GetStringAsync(r.Value);

            stateMonad.Log(LogLevel.Information, stringToPrint, this);

            return(Unit.Default);
        }
Esempio n. 27
0
        public void Start(Func <T, bool> processRequest)
        {
            var factory = new ConnectionFactory()
            {
                HostName = _hostName
            };

            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(_queueName, true, false, false, null);

                    // Fair dispatch
                    channel.BasicQos(0, 1, false);

                    var consumer = new QueueingBasicConsumer(channel);
                    channel.BasicConsume(_queueName, false, consumer);

                    while (true)
                    {
                        var ea = consumer.Queue.Dequeue() as BasicDeliverEventArgs;

                        try
                        {
                            var body = SerializationMethods.FromByteArray <T>(ea.Body);

                            var success = processRequest(body);

                            // Send acknowledge that received (and processed)
                            if (success)
                            {
                                channel.BasicAck(ea.DeliveryTag, false);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Exception:" + ex.Message);
                        }
                    }
                }
            }
        }
Esempio n. 28
0
        public TResponse SendRequest(TRequest request)
        {
            using (var socket = _context.CreateRequestSocket())
            {
                socket.Connect(_bindEndPoint);

                var envelope = new NetMQFrame(Encoding.UTF8.GetBytes(request.ToString()));
                var body     = new NetMQFrame(request.ToByteArray());

                var msq = new NetMQMessage();
                msq.Append(envelope);
                msq.Append(body);

                socket.SendMessage(msq);

                var responseMsg = socket.ReceiveMessage();

                return(SerializationMethods.FromByteArray <TResponse>(responseMsg[3].Buffer));
            }
        }
Esempio n. 29
0
        private void Receive(object t)
        {
            var token = (CancellationToken)t;

            while (token.IsCancellationRequested == false)
            {
                try
                {
                    var ea = _consumer.Queue.Dequeue();

                    var body = SerializationMethods.FromByteArray <TResponse>(ea.Body);

                    var success = _processRequest(body);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception:" + ex.Message);
                }
            }
        }
Esempio n. 30
0
        public void Start(Func <CommonRequest, bool> processRequest)
        {
            var factory = new ConnectionFactory()
            {
                HostName = _hostName
            };

            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    // Fanout just broadcasts messages
                    channel.ExchangeDeclare(_exchangeName, "fanout");

                    // This will create an own queue to RabbitMQ server
                    var queueName = channel.QueueDeclare();

                    channel.QueueBind(queueName, _exchangeName, "");

                    var consumer = new QueueingBasicConsumer(channel);
                    channel.BasicConsume(queueName, true, consumer);

                    while (true)
                    {
                        var ea = consumer.Queue.Dequeue() as BasicDeliverEventArgs;

                        try
                        {
                            var body = SerializationMethods.FromByteArray <CommonRequest>(ea.Body);

                            var success = processRequest(body);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Exception:" + ex.Message);
                        }
                    }
                }
            }
        }