public int Write(byte[] buffer, int offset, int size, int timeout)
        {
            try
            {
                lock (_writeSyncObject) //Can't write at the same time from different threads
                {
                    if (!TcpClient.Client.Connected)
                    {
                        throw new NotConnectedException();
                    }
                    if (_netStream == null)
                    {
                        throw new NotConnectedException();
                    }

                    TcpClient.SendTimeout = timeout;

                    SocketError err;
                    var         sendedBytes = TcpClient.Client.Send(buffer, offset, size, SocketFlags.None, out err);
                    _netStream.Flush();
                    if (err != SocketError.Success)
                    {
                        Exception ex;
                        if (err == SocketError.ConnectionAborted)
                        {
                            ex = new NotConnectedException();
                        }
                        else
                        {
                            ex = new NetworkWriteException(err.ToString());
                        }
                        throw ex;
                    }
                    return(sendedBytes);
                }
            }
            catch (NotConnectedException)
            {
                IsConnected = false;
            }
            catch (Exception ex)
            {
                OnErrorEvent(ex);
            }
            return(0);
        }
Beispiel #2
0
        /// <summary>
        /// Sends message to the server.
        /// </summary>
        /// <param name="words">Words to send.</param>
        protected virtual void InternalSend(byte[][] words)
        {
            if (!Connected)
            {
                LastException = new NotConnectedException();
                throw LastException;
            }

            var packet = PacketCodec.EncodePacket(false, false, _sequenceID++, words);

            try
            {
                var sendEventArgs = new SocketAsyncEventArgs();
                sendEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(sendEventArgs_Completed);
                sendEventArgs.SetBuffer(packet, 0, packet.Length);
                _sock.SendAsync(sendEventArgs);
            }
            catch (Exception ex)
            {
                Disconnect();
                LastException = ex;
                throw LastException;
            }
        }
        public int Write(byte[] buffer, int offset, int size, int timeout)
        {
            try
            {
                lock (_writeSyncObject) //Can't write at the same time from different threads
                {
                    if (_isSocketDisposed)
                    {
                        return(0);
                    }
                    if (Socket == null)
                    {
                        throw new NotConnectedException();
                    }
                    if (!Socket.Connected)
                    {
                        throw new NotConnectedException();
                    }

                    Socket.SendTimeout = timeout;

                    SocketError err;
                    var         sendedBytes = Socket.Send(buffer, offset, size, SocketFlags.None, out err);

                    if (err != SocketError.Success)
                    {
                        Exception ex;
                        switch (err)
                        {
                        case SocketError.ConnectionAborted:
                        case SocketError.ConnectionReset:
                        case SocketError.ConnectionRefused:
                        case SocketError.Interrupted:
                        case SocketError.TimedOut:
                            ex = new NotConnectedException();
                            break;

                        default:
                            ex = new NetworkWriteException(err.ToString());
                            break;
                        }
                        throw ex;
                    }

                    return(sendedBytes);
                }
            }
            catch (NotConnectedException)
            {
                IsConnected = false;
            }
            catch (ThreadAbortException)
            {
            }
            catch (SocketException sex)
            {
                switch (sex.SocketErrorCode)
                {
                case SocketError.ConnectionAborted:
                case SocketError.ConnectionReset:
                case SocketError.ConnectionRefused:
                case SocketError.Interrupted:
                case SocketError.TimedOut:
                    IsConnected = false;
                    break;

                default:
                    OnErrorEvent(sex);
                    break;
                }
            }
            catch (ObjectDisposedException)
            {
            }
            catch (Exception ex)
            {
                var type = ex.GetType();
                if (ex.InnerException != null && ex.InnerException.GetType() != typeof(ThreadAbortException))
                {
                    OnErrorEvent(ex);
                }
            }
            return(0);
        }
        /// <summary>
        /// Ensures that a workflow from the supplied definition id exists on the list, either updating or adding the subscription.
        /// </summary>
        /// <param name="web">The web</param>
        /// <param name="list">The list on which we are ensuring the workflow</param>
        /// <param name="workflowDefinitionId">The workflow definition id</param>
        /// <param name="displayName">The workflow display name</param>
        /// <param name="eventList">The list of events we are registering { possible: ["ItemAdded", "ItemUpdated", "WorkflowStart"] }</param>
        /// <param name="taskList">The task list.</param>
        /// <param name="historyList">The history list.</param>
        /// <param name="workflowPropertyData">The workflow property data.</param>
        /// <param name="remove">If true the workflow is removed, if false it is added or updated.</param>
        /// <returns>The workflow subscription id</returns>
        public static Guid EnsureWorkflowOnList(SPWeb web, SPList list, Guid workflowDefinitionId, string displayName, List <WorkflowStartEventType> eventList, SPList taskList, SPList historyList, Dictionary <string, string> workflowPropertyData, bool remove)
        {
            Guid subscriptionId = Guid.Empty;

            if (list == null)
            {
                throw new ArgumentNullException("list");
            }

            // get our manager and services
            var workflowServicesManager = new WorkflowServicesManager(web);

            if (!workflowServicesManager.IsConnected)
            {
                var notConnectedEx = new NotConnectedException();
                throw notConnectedEx;
            }

            var workflowDeploymentService   = workflowServicesManager.GetWorkflowDeploymentService();
            var workflowSubscriptionService = workflowServicesManager.GetWorkflowSubscriptionService();

            var workflowDefinition = workflowDeploymentService.GetDefinition(workflowDefinitionId);

            if (workflowDefinition == null)
            {
                var ex = new SPException("Failed to load workflow definition with id: " + workflowDefinitionId);
                throw ex;
            }

            var workflowSubscriptions = workflowSubscriptionService.EnumerateSubscriptionsByList(list.ID);
            var workflowSubscription  = default(WorkflowSubscription);

            if (workflowSubscriptions != null)
            {
                workflowSubscription = workflowSubscriptions.FirstOrDefault((s) => s.DefinitionId.Equals(workflowDefinitionId));
            }

            // remove the workflow based on the supplied flag
            if (remove)
            {
                if (workflowSubscription != null)
                {
                    workflowSubscriptionService.DeleteSubscription(workflowSubscription.Id);
                }

                return(subscriptionId);
            }

            if (workflowSubscription == null)
            {
                workflowSubscription = new WorkflowSubscription();
                workflowSubscription.EventSourceId = list.ID;
                workflowSubscription.DefinitionId  = workflowDefinition.Id;
                workflowSubscription.Id            = Guid.NewGuid();
            }

            if (workflowPropertyData == null)
            {
                workflowPropertyData = new Dictionary <string, string>();
            }

            workflowPropertyData["TaskListId"]    = (taskList != null) ? taskList.ID.ToString("D") : string.Empty;
            workflowPropertyData["HistoryListId"] = (historyList != null) ? historyList.ID.ToString("D") : string.Empty;
            workflowPropertyData["FormData"]      = string.Empty;

            try
            {
                foreach (string propDataKey in workflowPropertyData.Keys)
                {
                    workflowSubscription.PropertyDefinitions[propDataKey] = workflowPropertyData[propDataKey];
                }

                List <string> events = TransformEvents(eventList);

                workflowSubscription.Name       = displayName;
                workflowSubscription.EventTypes = events;

                subscriptionId = workflowSubscriptionService.PublishSubscriptionForList(workflowSubscription, list.ID);
            }
            catch (Exception err)
            {
                throw;
            }

            return(subscriptionId);
        }