public DealManager(DealTransfer dealTransfer)
 {
     transfer        = dealTransfer;
     transferContext = dealTransfer.Context;
     dealContext     = dealTransfer.MyHeader.Context;
     site            = dealContext.IdentitySite;
 }
        /// <summary>
        /// The HeaderSent.
        /// </summary>
        /// <param name="inetdealcontext">The inetdealcontext<see cref="object"/>.</param>
        /// <returns>The <see cref="object"/>.</returns>
        public object HeaderSent(object inetdealcontext)
        {
            WriteEcho("Server header sent");

            ITransferContext context = (ITransferContext)inetdealcontext;

            if (context.Close)
            {
                context.Transfer.Dispose();
                server.CloseClient(context.Id);
            }
            else
            {
                if (!context.Synchronic)
                {
                    if (context.ReceiveMessage)
                    {
                        server.Receive(MessagePart.Message, context.Id);
                    }
                }
                if (context.SendMessage)
                {
                    server.Send(MessagePart.Message, context.Id);
                }
            }
            return(context);
        }
        private void HeaderReceivedCallBack(IAsyncResult result)
        {
            ITransferContext context = (ITransferContext)result.AsyncState;
            int receive = context.Listener.EndReceive(result);

            if (receive > 0)
            {
                context.IncomingHeader(receive);
            }

            if (context.DeserialPacketSize > 0)
            {
                int buffersize = (context.DeserialPacketSize < context.BufferSize) ? (int)context.DeserialPacketSize : context.BufferSize;
                context.Listener.BeginReceive(context.HeaderBuffer, 0, buffersize, SocketFlags.None, HeaderReceivedCallBack, context);
            }
            else
            {
                TransferOperation request = new TransferOperation(context.Transfer, MessagePart.Header, DirectionType.Receive);
                request.Resolve(context.DeserialPacket);

                if (!context.ReceiveMessage &&
                    !context.SendMessage)
                {
                    context.Close = true;
                }

                context.HeaderReceivedNotice.Set();
                HeaderReceived.Execute(this);
            }
        }
Пример #4
0
        /// <summary>
        /// The HeaderSentCallback.
        /// </summary>
        /// <param name="result">The result<see cref="IAsyncResult"/>.</param>
        public void HeaderSentCallback(IAsyncResult result)
        {
            ITransferContext context = (ITransferContext)result.AsyncState;

            try
            {
                int sendcount = context.Listener.EndSend(result);
            }
            catch (SocketException) { }
            catch (ObjectDisposedException) { }

            if (!context.ReceiveMessage && !context.SendMessage)
            {
                //int _timeout = 0;
                //while (IsConnected(context.Id) && timeout < 10) _timeout++;
                context.Close = true;
            }

            context.HeaderSentNotice.Set();

            try
            {
                HeaderSent.Execute(context);
            }
            catch (Exception ex)
            {
                Echo(ex.Message);
                CloseClient(context.Id);
            }
        }
Пример #5
0
        /// <summary>
        /// The CloseClient.
        /// </summary>
        /// <param name="card">The card<see cref="ICard{ITransferContext}"/>.</param>
        public void CloseClient(ICard <ITransferContext> card)
        {
            ITransferContext context = card.Value;

            if (context == null)
            {
                Echo(string.Format("Client {0} does not exist.", context.Id));
            }
            else
            {
                try
                {
                    if (context.Listener != null && context.Listener.Connected)
                    {
                        context.Listener.Shutdown(SocketShutdown.Both);
                        context.Listener.Close();
                    }
                }
                catch (SocketException sx)
                {
                    Echo(sx.Message);
                }
                finally
                {
                    ITransferContext contextRemoved = clients.Remove(context.Id);
                    contextRemoved.Dispose();
                    Echo(string.Format("Client disconnected with Id {0}", context.Id));
                }
            }
        }
Пример #6
0
        /// <summary>
        /// The DealHeaderReceived.
        /// </summary>
        /// <param name="context">The context<see cref="ITransferContext"/>.</param>
        public void DealHeaderReceived(ITransferContext context)
        {
            if (context.BlockSize > 0)
            {
                int buffersize = (context.BlockSize < context.BufferSize) ? (int)context.BlockSize : context.BufferSize;
                context.Listener.BeginReceive(context.HeaderBuffer, 0, buffersize, SocketFlags.None, HeaderReceivedCallback, context);
            }
            else
            {
                TransferOperation request = new TransferOperation(context.Transfer, MessagePart.Header, DirectionType.Receive);
                request.Resolve(context);

                context.HeaderReceivedNotice.Set();

                try
                {
                    HeaderReceived.Execute(context);
                }
                catch (Exception ex)
                {
                    Echo(ex.Message);
                    CloseClient(context.Id);
                }
            }
        }
Пример #7
0
        /// <summary>
        /// The ClearClients.
        /// </summary>
        public void ClearClients()
        {
            foreach (ITransferContext closeContext in clients.AsValues())
            {
                ITransferContext context = closeContext;

                if (context == null)
                {
                    throw new Exception("Client does not exist.");
                }

                try
                {
                    context.Listener.Shutdown(SocketShutdown.Both);
                    context.Listener.Close();
                }
                catch (SocketException sx)
                {
                    Echo(sx.Message);
                }
                finally
                {
                    context.Dispose();
                    Echo(string.Format("Client disconnected with Id {0}", context.Id));
                }
            }
            clients.Clear();
        }
Пример #8
0
        /// <summary>
        /// The Send.
        /// </summary>
        /// <param name="messagePart">The messagePart<see cref="MessagePart"/>.</param>
        /// <param name="id">The id<see cref="int"/>.</param>
        public void Send(MessagePart messagePart, int id)
        {
            ITransferContext context = GetClient(id).Value;

            if (!IsConnected(context.Id))
            {
                throw new Exception("Destination socket is not connected.");
            }

            AsyncCallback callback = HeaderSentCallback;

            if (messagePart == MessagePart.Header)
            {
                callback = HeaderSentCallback;
                TransferOperation request = new TransferOperation(context.Transfer, MessagePart.Header, DirectionType.Send);
                request.Resolve();
            }
            else if (context.SendMessage)
            {
                callback = MessageSentCallback;
                context.SerialBlockId = 0;
                TransferOperation request = new TransferOperation(context.Transfer, MessagePart.Message, DirectionType.Send);
                request.Resolve();
            }
            else
            {
                return;
            }

            context.Listener.BeginSend(context.SerialBlock, 0, context.SerialBlock.Length, SocketFlags.None, callback, context);
        }
        private void MessageSentCallback(IAsyncResult result)
        {
            ITransferContext context = (ITransferContext)result.AsyncState;

            try
            {
                int sendcount = context.Listener.EndSend(result);
            }
            catch (SocketException) { }
            catch (ObjectDisposedException) { }

            if (context.SerialPacketId >= 0)
            {
                TransferOperation request = new TransferOperation(context.Transfer, MessagePart.Message, DirectionType.Send);
                request.Resolve();
                context.Listener.BeginSend(context.SerialPacket, 0, context.SerialPacket.Length, SocketFlags.None, MessageSentCallback, context);
            }
            else
            {
                if (!context.ReceiveMessage)
                {
                    context.Close = true;
                }

                context.MessageSentNotice.Set();
                MessageSent.Execute(this);
            }
        }
Пример #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DealHeader"/> class.
 /// </summary>
 /// <param name="_transaction">The _transaction<see cref="DealTransfer"/>.</param>
 /// <param name="context">The context<see cref="ITransferContext"/>.</param>
 public DealHeader(DealTransfer _transaction, ITransferContext context)
 {
     Context = new DealContext();
     Context.LocalEndPoint  = (IPEndPoint)context.Listener.LocalEndPoint;
     Context.RemoteEndPoint = (IPEndPoint)context.Listener.RemoteEndPoint;
     transaction            = _transaction;
     SerialCount            = 0; DeserialCount = 0;
 }
        private DealManager treatment;// Important Field !!! - Dealer Treatment initiatie, filtering, sorting, saving, editing all treatment here.

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TransferManager"/> class.
        /// </summary>
        /// <param name="_transaction">The _transaction<see cref="DealTransfer"/>.</param>
        public TransferManager(DealTransfer _transaction)
        {
            transaction     = _transaction;
            transferContext = transaction.Context;
            context         = transaction.MyHeader.Context;
            site            = context.IdentitySite;
            treatment       = new DealManager(_transaction);
        }
Пример #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DealHeader"/> class.
 /// </summary>
 /// <param name="_transaction">The _transaction<see cref="DealTransfer"/>.</param>
 /// <param name="context">The context<see cref="ITransferContext"/>.</param>
 /// <param name="identity">The identity<see cref="MemberIdentity"/>.</param>
 public DealHeader(DealTransfer _transaction, ITransferContext context, MemberIdentity identity)
 {
     Context = new DealContext();
     Context.LocalEndPoint  = (IPEndPoint)context.Listener.LocalEndPoint;
     Context.RemoteEndPoint = (IPEndPoint)context.Listener.RemoteEndPoint;
     Context.Identity       = identity;
     Context.IdentitySite   = identity.Site;
     transaction            = _transaction;
     SerialCount            = 0; DeserialCount = 0;
 }
 public TransferOperation(DealTransfer _transaction, MessagePart _part, DirectionType _direction)
 {
     transaction      = _transaction;
     vsspContext      = transaction.Context;
     transportContext = transaction.MyHeader.Context;
     site             = transportContext.IdentitySite;
     direction        = _direction;
     part             = _part;
     protocol         = vsspContext.Protocol;
     method           = vsspContext.Method;
 }
Пример #14
0
 public ImportResult GetStatus()
 {
     if (ExportImportBase.CurrentContext != null)
     {
         ITransferContext dataImporter = ExportImportBase.CurrentContext;
         ImportResult     result       = new ImportResult();
         result.Errors   = dataImporter.Log.Errors;
         result.Warnings = dataImporter.Log.Warnings;
         return(result);
     }
     return(null);
 }
        /// <summary>
        /// The MessageSent.
        /// </summary>
        /// <param name="inetdealcontext">The inetdealcontext<see cref="object"/>.</param>
        /// <returns>The <see cref="object"/>.</returns>
        public object MessageSent(object inetdealcontext)
        {
            WriteEcho("Server message sent");
            ITransferContext result = (ITransferContext)inetdealcontext;

            if (result.Close)
            {
                result.Transfer.Dispose();
                server.CloseClient(result.Id);
            }
            return(result);
        }
Пример #16
0
        public void MessageReceivedCallback(IAsyncResult result)
        {
            ITransferContext context   = (ITransferContext)result.AsyncState;
            MarkupType       noiseKind = MarkupType.None;

            int receive = context.Listener.EndReceive(result);

            if (receive > 0)
            {
                noiseKind = context.IncomingMessage(receive);
            }

            if (context.DeserialPacketSize > 0)
            {
                int buffersize = (context.DeserialPacketSize < context.BufferSize) ? (int)context.DeserialPacketSize : context.BufferSize;
                context.Listener.BeginReceive(context.MessageBuffer, 0, buffersize, SocketFlags.None, MessageReceivedCallback, context);
            }
            else
            {
                object received     = context.DeserialPacket;
                object readPosition = context.DeserialPacketId;

                if (noiseKind == MarkupType.Block || (noiseKind == MarkupType.End && (int)readPosition < (context.Transfer.HeaderReceived.Context.ObjectsCount - 1)))
                {
                    context.Listener.BeginReceive(context.MessageBuffer, 0, context.BufferSize, SocketFlags.None, MessageReceivedCallback, context);
                }

                TransferOperation request = new TransferOperation(context.Transfer, MessagePart.Message, DirectionType.Receive);
                request.Resolve(received, readPosition);

                if (context.ObjectsLeft <= 0 && !context.BatchesReceivedNotice.SafeWaitHandle.IsClosed)
                {
                    context.BatchesReceivedNotice.Set();
                }

                if (noiseKind == MarkupType.End && (int)readPosition >= (context.Transfer.HeaderReceived.Context.ObjectsCount - 1))
                {
                    context.BatchesReceivedNotice.WaitOne();
                    context.MessageReceivedNotice.Set();

                    try
                    {
                        MessageReceived.Execute(context);
                    }
                    catch (Exception ex)
                    {
                        Echo(ex.Message);
                        CloseClient(context.Id);
                    }
                }
            }
        }
        private void OnConnectCallback(IAsyncResult result)
        {
            ITransferContext context = (ITransferContext)result.AsyncState;

            try
            {
                context.Listener.EndConnect(result);
                connectNotice.Set();
            }
            catch (SocketException ex)
            {
            }
        }
Пример #18
0
        /// <summary>
        /// The IsConnected.
        /// </summary>
        /// <param name="id">The id<see cref="int"/>.</param>
        /// <returns>The <see cref="bool"/>.</returns>
        public bool IsConnected(int id)
        {
            ITransferContext context = GetClient(id).Value;

            if (context != null && context.Listener != null && context.Listener.Connected)
            {
                return(!(context.Listener.Poll(timeout * 100, SelectMode.SelectRead) && context.Listener.Available == 0));
            }
            else
            {
                return(false);
            }
        }
        private void HeaderSentCallback(IAsyncResult result)
        {
            ITransferContext context = (ITransferContext)result.AsyncState;

            try
            {
                int sendcount = context.Listener.EndSend(result);
            }
            catch (SocketException) { }
            catch (ObjectDisposedException) { }

            context.HeaderSentNotice.Set();
            HeaderSent.Execute(this);
        }
        public object MessageReceived(object inetdealclient)
        {
            WriteEcho(string.Format("Server message received"));

            ITransferContext context = ((IDealClient)inetdealclient).Context;
            if (context.Close)
                ((IDealClient)inetdealclient).Dispose();

            if (CompleteEvent != null)
                CompleteEvent.Execute(context);
            if(!isAsync)
                completeNotice.Set();
            return context;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DealTransfer"/> class.
        /// </summary>
        /// <param name="identity">The identity<see cref="MemberIdentity"/>.</param>
        /// <param name="message">The message<see cref="object"/>.</param>
        /// <param name="context">The context<see cref="ITransferContext"/>.</param>
        public DealTransfer(MemberIdentity identity, object message = null, ITransferContext context = null)
        {
            Context = context;
            if (Context != null)
            {
                MyHeader = new DealHeader(this, Context, identity);
            }
            else
            {
                MyHeader = new DealHeader(this, identity);
            }

            Identity  = identity;
            Manager   = new TransferManager(this);
            MyMessage = new DealMessage(this, DirectionType.Send, message);
        }
Пример #22
0
        /// <summary>
        /// The Receive.
        /// </summary>
        /// <param name="messagePart">The messagePart<see cref="MessagePart"/>.</param>
        /// <param name="id">The id<see cref="int"/>.</param>
        public void Receive(MessagePart messagePart, int id)
        {
            ITransferContext context = GetClient(id).Value;

            AsyncCallback callback = HeaderReceivedCallback;

            if (messagePart != MessagePart.Header && context.ReceiveMessage)
            {
                callback            = MessageReceivedCallback;
                context.ObjectsLeft = context.Transfer.HeaderReceived.Context.ObjectsCount;
                context.Listener.BeginReceive(context.MessageBuffer, 0, context.BufferSize, SocketFlags.None, callback, context);
            }
            else
            {
                context.Listener.BeginReceive(context.HeaderBuffer, 0, context.BufferSize, SocketFlags.None, callback, context);
            }
        }
Пример #23
0
        private void DataExportEvents_ContentExporting(ITransferContext transferContext, ContentExportingEventArgs e)
        {
            var exporter = transferContext as ITransferHandlerContext;
            if (exporter != null && exporter.TransferType == TypeOfTransfer.MirroringExporting)
            {
                var ddsHandler = exporter.TransferHandlers.Single(p => p.GetType() == typeof(DynamicDataTransferHandler)) as DynamicDataTransferHandler;

                var store = typeof(Tag).GetStore();
                var externalId = store.GetIdentity().ExternalId;
                var storeName = store.Name;

                if (ddsHandler != null)
                {
                    ddsHandler.AddToExport(externalId, storeName);
                }
            }
        }
Пример #24
0
        private void DataExporter_Exporting(ITransferContext transferContext, ContentExportingEventArgs e)
        {
            var exporter = transferContext as ITransferHandlerContext;

            if (exporter != null && exporter.TransferType == TypeOfTransfer.MirroringExporting)
            {
                var ddsHandler = exporter.TransferHandlers.Single(p => p.GetType() == typeof(DynamicDataTransferHandler)) as DynamicDataTransferHandler;

                var store      = typeof(RobotsTxtData).GetStore();
                var externalId = store.GetIdentity().ExternalId;
                var storeName  = store.Name;

                if (ddsHandler != null)
                {
                    ddsHandler.AddToExport(externalId, storeName);
                }
            }
        }
Пример #25
0
        private void DataExportEvents_ContentExporting(ITransferContext transferContext, ContentExportingEventArgs e)
        {
            if (!(transferContext is ITransferHandlerContext exporter) ||
                exporter.TransferType != TypeOfTransfer.MirroringExporting)
            {
                return;
            }

            var ddsHandler = exporter
                             .TransferHandlers
                             .Single(p => p.GetType() == typeof(DynamicDataTransferHandler)) as DynamicDataTransferHandler;

            var store      = typeof(Tag).GetStore();
            var externalId = store.GetIdentity().ExternalId;
            var storeName  = store.Name;

            ddsHandler?.AddToExport(externalId, storeName);
        }
        public void Connect()
        {
            ushort     _port    = port;
            string     hostname = host.HostName;
            IPAddress  _ip      = ip;
            IPEndPoint endpoint = new IPEndPoint(_ip, _port);

            try
            {
                socket  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                context = new TransferContext(socket);
                socket.BeginConnect(endpoint, OnConnectCallback, context);
                connectNotice.WaitOne();

                Connected.Execute(this);
            }
            catch (SocketException ex)
            { }
        }
Пример #27
0
        /// <summary>
        /// The HeaderReceivedCallback.
        /// </summary>
        /// <param name="result">The result<see cref="IAsyncResult"/>.</param>
        public void HeaderReceivedCallback(IAsyncResult result)
        {
            ITransferContext context = (ITransferContext)result.AsyncState;
            int receive = context.Listener.EndReceive(result);

            if (receive > 0)
            {
                context.IncomingHeader(receive);
            }

            if (context.Protocol == DealProtocol.DOTP)
            {
                DealHeaderReceived(context);
            }
            else if (context.Protocol == DealProtocol.HTTP)
            {
                HttpHeaderReceived(context);
            }
        }
Пример #28
0
        public void MessageSentCallback(IAsyncResult result)
        {
            ITransferContext context = (ITransferContext)result.AsyncState;

            try
            {
                int sendcount = context.Listener.EndSend(result);
            }
            catch (SocketException) { }
            catch (ObjectDisposedException) { }

            if (context.SerialPacketId >= 0 || context.ObjectPosition < (context.Transfer.MyHeader.Context.ObjectsCount - 1))
            {
                TransferOperation request = new TransferOperation(context.Transfer, MessagePart.Message, DirectionType.Send);
                request.Resolve();
                context.Listener.BeginSend(context.SerialPacket, 0, context.SerialPacket.Length, SocketFlags.None, MessageSentCallback, context);
            }
            else
            {
                if (context.ReceiveMessage)
                {
                    context.MessageReceivedNotice.WaitOne();
                }

                //int _timeout = 0;
                // while (IsConnected(context.Id) && timeout < 10) _timeout++;
                context.Close = true;

                context.MessageSentNotice.Set();

                try
                {
                    MessageSent.Execute(context);
                }
                catch (Exception ex)
                {
                    Echo(ex.Message);
                    CloseClient(context.Id);
                }
            }
        }
Пример #29
0
        private string GetStatus(ITransferContext importer)
        {
            var logMessage = new StringBuilder();
            var lineBreak  = "<br>";

            if (importer.Log.Errors.Count > 0)
            {
                foreach (string err in importer.Log.Errors)
                {
                    logMessage.Append(err).Append(lineBreak);
                }
            }

            if (importer.Log.Warnings.Count > 0)
            {
                foreach (string err in importer.Log.Warnings)
                {
                    logMessage.Append(err).Append(lineBreak);
                }
            }
            return(logMessage.ToString());
        }
Пример #30
0
        /// <summary>
        /// The CloseListener.
        /// </summary>
        public void CloseListener()
        {
            foreach (ITransferContext closeContext in clients.AsValues())
            {
                ITransferContext context = closeContext;

                if (context == null)
                {
                    Echo(string.Format("Client  does not exist."));
                }
                else
                {
                    try
                    {
                        if (context.Listener != null && context.Listener.Connected)
                        {
                            context.Listener.Shutdown(SocketShutdown.Both);
                            context.Listener.Close();
                        }
                    }
                    catch (SocketException sx)
                    {
                        Echo(sx.Message);
                    }
                    finally
                    {
                        context.Dispose();
                        Echo(string.Format("Client disconnected with Id {0}", context.Id));
                    }
                }
            }
            clients.Clear();
            shutdown = true;
            connectingNotice.Set();
            GC.Collect();
        }
 public TransferRepository(ITransferContext context)
 {
     _context = context;
 }
        private string GetStatus(ITransferContext importer)
        {
            var logMessage = new StringBuilder();
            var lineBreak = "<br>";

            if (importer.Log.Errors.Count > 0)
            {
                foreach (string err in importer.Log.Errors)
                {
                    logMessage.Append(err).Append(lineBreak);
                }
            }

            if (importer.Log.Warnings.Count > 0)
            {
                foreach (string err in importer.Log.Warnings)
                {
                    logMessage.Append(err).Append(lineBreak);
                }
            }
            return logMessage.ToString();
        }