Beispiel #1
0
 public void SendFeed(byte[] buf, int offset, int count)
 {
     for (var x = state; STANDBY < x && x < CLOSE_PENDING; x = state)
     {
         if (SetState(HTTP, HTTP_PROCESSING))
         {
             sndTextStore.Write(buf, offset, count);
             SetState(HTTP_PROCESSING, HTTP);
             return;
         }
         else if (SetState(WEBSOCKET, WEBSOCKET_PROCESSING))
         {
             OnWebsocketSend?.Invoke(buf, offset, count);
             SetState(WEBSOCKET_PROCESSING, WEBSOCKET);
             return;
         }
     }
 }
Beispiel #2
0
 private void MainLoop()
 {
     try
     {
         SetState(HTTP_PENDING, HTTP);
         for (var x = state; x == HTTP || x == HTTP_PROCESSING; x = state)
         {
             RequestMessage.Parse();
             ResponseMessage.Parse(RequestMessage.Method);
             if (ResponseMessage.StatusCode == 101 /* Switching Protocols */)
             {
                 if (ResponseMessage.HeaderList.Connection == HttpConnection.UPGRADE)
                 {
                     if (ResponseMessage.HeaderList.Upgrade == HttpUpgrade.WEBSOCKET)
                     {
                         while (state < CLOSE_PENDING)
                         {
                             if (SetState(HTTP, WEBSOCKET_PENDING))
                             {
                                 var sndData = sndTextStore.ReadBytes();
                                 if (sndData.Length > 0)
                                 {
                                     OnWebsocketSend?.Invoke(sndData, 0, sndData.Length);
                                 }
                                 var rcvData = rcvTextStore.ReadBytes();
                                 if (rcvData.Length > 0)
                                 {
                                     OnWebsocketReceive?.Invoke(rcvData, 0, rcvData.Length);
                                 }
                                 SetState(WEBSOCKET_PENDING, WEBSOCKET);
                                 break;
                             }
                         }
                     }
                     else
                     {
                         OnUpgradeOfOtherThanWebsocket?.Invoke();
                         break;
                     }
                 }
                 else
                 {
                     OnConnectionOfOtherThanUpgrade?.Invoke();
                     break;
                 }
             }
         }
         for (var x = state; x == WEBSOCKET || x == WEBSOCKET_PROCESSING; x = state)
         {
             Thread.Sleep(1);
         }
     }
     catch (Exception ex)
     {
         if (SetState(HTTP, HTTP) && RequestMessage.Method != HttpMethod.None)
         {
             OnError?.Invoke(ex);
         }
     }
     OnCompletion?.Invoke();
 }