public bool HandlesEvent(SocketEvent Event)
 {
     return ASocket.StringsEqual((EventName == null ? Event.EventName : EventName), Event.EventName) &&
         ASocket.StringsEqual((UUID == null ? Event.UniqueID : UUID), Event.UniqueID) &&
         ASocket.StringsEqual((ChannelName == null ? Event.ChannelName : ChannelName), Event.ChannelName) &&
         ASocket.StringsEqual((CallerUUID == null ? Event.CallerUUID : CallerUUID), Event.CallerUUID);
 }
 private void _MessageProcessorStart()
 {
     Thread.CurrentThread.Name = "EventMessageProcessor_" + Thread.CurrentThread.ManagedThreadId.ToString();
     while (!_exit)
     {
         if (_mreMessageWaiting.WaitOne(1000))
         {
             lock (_splitMessages)
             {
                 while (_splitMessages.Count > 0)
                 {
                     _processingMessages.Add(_splitMessages[0]);
                     _splitMessages.RemoveAt(0);
                 }
             }
             bool run = true;
             Queue<ASocketMessage> msgs = new Queue<ASocketMessage>();
             while (run)
             {
                 while (_processingMessages.Count > 0)
                 {
                     string origMsg = _processingMessages[0];
                     _processingMessages.RemoveAt(0);
                     Dictionary<string, string> pars = ASocketMessage.ParseProperties(origMsg);
                     string subMsg = "";
                     //fail safe for delayed header
                     if (!pars.ContainsKey("Content-Type"))
                     {
                         if (_disposeInvalidMesssage != null)
                             _disposeInvalidMesssage(origMsg);
                         break;
                     }
                     if (pars.ContainsKey("Content-Length"))
                     {
                         if (int.Parse(pars["Content-Length"]) > 0)
                         {
                             if (_processingMessages.Count > 0)
                             {
                                 subMsg = _processingMessages[0];
                                 _processingMessages.RemoveAt(0);
                             }
                             else
                             {
                                 _processingMessages.Insert(0, origMsg);
                                 break;
                             }
                         }
                     }
                     switch (pars["Content-Type"])
                     {
                         case "text/event-plain":
                             if (subMsg == "")
                             {
                                 _processingMessages.Insert(0, origMsg);
                                 break;
                             }
                             else
                             {
                                 SocketEvent se;
                                 se = new SocketEvent(subMsg);
                                 if (se["Content-Length"] != null)
                                 {
                                     if (_processingMessages.Count > 0)
                                     {
                                         se.Message = _processingMessages[0];
                                         _processingMessages.RemoveAt(0);
                                     }
                                     else
                                     {
                                         _processingMessages.Insert(0, origMsg);
                                         _processingMessages.Insert(1, subMsg);
                                         break;
                                     }
                                 }
                                 if (se.EventName == "BACKGROUND_JOB")
                                 {
                                     lock (_commandThreads)
                                     {
                                         if (_commandThreads.ContainsKey(se["Job-UUID"]))
                                         {
                                             lock (_awaitingCommandReturns)
                                             {
                                                 _awaitingCommandReturns.Add(se["Job-UUID"], se.Message.Trim('\n'));
                                             }
                                             ManualResetEvent mre = _commandThreads[se["Job-UUID"]];
                                             _commandThreads.Remove(se["Job-UUID"]);
                                             mre.Set();
                                         }
                                     }
                                 }
                                 msgs.Enqueue(se);
                             }
                             break;
                         case "command/reply":
                             CommandReplyMessage crm = new CommandReplyMessage(origMsg, subMsg);
                             msgs.Enqueue(crm);
                             if (crm["Job-UUID"] != null)
                             {
                                 lock (_awaitingCommandsEvents)
                                 {
                                     _currentCommandID = crm["Job-UUID"];
                                     _awaitingCommandsEvents.Dequeue().Set();
                                 }
                             }
                             break;
                         case "log/data":
                             SocketLogMessage lg;
                             lg = new SocketLogMessage(subMsg);
                             if (_processingMessages.Count > 0)
                             {
                                 string eventMsg = _processingMessages[0];
                                 _processingMessages.RemoveAt(0);
                                 lg.FullMessage = eventMsg;
                                 msgs.Enqueue(lg);
                             }
                             else
                             {
                                 _processingMessages.Insert(0, origMsg);
                                 _processingMessages.Insert(1, subMsg);
                                 break;
                             }
                             break;
                         case "text/disconnect-notice":
                             msgs.Enqueue(new DisconnectNoticeMessage(origMsg));
                             break;
                         case "auth/request":
                             msgs.Enqueue(new AuthenticationRequestMessage(origMsg));
                             break;
                         default:
                             if (_disposeInvalidMesssage != null)
                                 _disposeInvalidMesssage(origMsg);
                             break;
                     }
                 }
                 if (msgs.Count > 0)
                     _processMessageQueue(msgs);
                 lock (_processingMessages)
                 {
                     lock (_splitMessages)
                     {
                         if (_splitMessages.Count > 0)
                         {
                             while (_splitMessages.Count > 0)
                             {
                                 _processingMessages.Add(_splitMessages[0]);
                                 _splitMessages.RemoveAt(0);
                             }
                         }
                         else
                         {
                             run = false;
                         }
                     }
                 }
             }
         }
         lock (_splitMessages)
         {
             if (_splitMessages.Count == 0)
                 _mreMessageWaiting.Reset();
         }
     }
 }
 private void ProcessEvent(SocketEvent message)
 {
     sEventHandler[] handlers;
     lock (_handlers)
     {
         handlers = new sEventHandler[_handlers.Count];
         _handlers.CopyTo(handlers, 0);
     }
     foreach (sEventHandler eh in handlers)
     {
         if (eh.HandlesEvent(message))
             eh.Handler.BeginInvoke(message, new AsyncCallback(ProcessingComplete), this);
     }
 }
 private SocketEvent ExecuteApplication(string applicationName, string applicationArguements, bool eventLock)
 {
     StringBuilder sb = new StringBuilder();
     sb.AppendLine("sendmsg");
     if (eventLock)
         sb.AppendLine("event-lock: true");
     sb.AppendLine("call-command: execute");
     sb.AppendLine("execute-app-name: " + applicationName);
     if (applicationArguements != null)
         sb.AppendLine("execute-app-arg: " + applicationArguements);
     sb.AppendLine("");
     ManualResetEvent mreComm = new ManualResetEvent(false);
     SocketEvent ret = null;
     lock (_awaitingCommands)
     {
         _awaitingCommands.Enqueue(mreComm);
     }
     _sendCommand(sb.ToString());
     if (eventLock)
     {
         lock (_awaitingCommand)
         {
             _awaitingCommand = applicationName + " " + (applicationArguements == null ? "" : applicationArguements);
         }
     }
     mreComm.WaitOne();
     if (eventLock)
     {
         _awaitingCommandEvent.WaitOne();
         lock (_awaitingCommand)
         {
             _awaitingCommand = "";
             ret = _currentEvent;
             _currentEvent = null;
             _awaitingCommandEvent.Reset();
         }
     }
     return ret;
 }
 protected override void _processMessageQueue(Queue<ASocketMessage> messages)
 {
     while (messages.Count > 0)
     {
         ASocketMessage asm = messages.Dequeue();
         if (asm is CommandReplyMessage)
         {
             if (asm["Job-UUID"] == null)
             {
                 ManualResetEvent mre = null;
                 lock (_awaitingCommands)
                 {
                     if (_awaitingCommands.Count > 0)
                         mre = _awaitingCommands.Dequeue();
                 }
                 if (mre != null)
                     mre.Set();
             }
         }
         else if (asm is SocketEvent)
         {
             SocketEvent se = (SocketEvent)asm;
             lock (_properties)
             {
                 se.CopyParameters(ref _properties);
             }
             if (se.EventName == EXECUTE_COMPLETE_EVENT_NAME)
             {
                 if ((se.CallerUUID == CallerUUID) && (se.ChannelName == ChannelName))
                 {
                     lock (_awaitingCommand)
                     {
                         if (_awaitingCommand == se[APP_NAME_VARIABLE_NAME] + " " + (se[APP_DATA_VARIABLE_NAME] == null ? "" : se[APP_DATA_VARIABLE_NAME]))
                         {
                             _currentEvent = se;
                             _awaitingCommandEvent.Set();
                         }
                     }
                 }
             }
             else if (se.EventName == CHANNEL_END_EVENT_NAME)
             {
                 if ((se.CallerUUID == CallerUUID) && (se.ChannelName == ChannelName))
                 {
                     _isHungup = true;
                     _currentEvent = se;
                     _awaitingCommandEvent.Set();
                     Close();
                 }
             }
         }
     }
 }
 public static void ProcessEvent(SocketEvent evnt)
 {
     Console.WriteLine("Event recieved of type " + evnt.EventName);
 }