Esempio n. 1
0
        public async void DoWSServeClientPostDataIn()
        {
            //tfServerThread.StartNew(DoWSServeSocket, ctsServerThread.Token);
            /////
            wsso = new WSServerTaskAndOject()
            {
                ct = ctsServerThread.Token, CanBeDisposed = false, wsioh = this, wsCts = ctsServerThread
            };
            ////
            wsso.task = Task.Run(() => DoWSServeSocket(wsso));

            if (hlcIO.Request.HttpMethod == "post" || hlcIO.Request.HttpMethod == "put")
            {
                while (hlcIO.Request.InputStream.CanRead & !isPostDataRead)
                {
                    int n = await hlcIO.Request.InputStream.ReadAsync(bInBuff, 0, 1024);

                    if (n == 0)
                    {
                        isPostDataRead = true;
                    }
                    if (n != 0)
                    {
                        bInBuff = new byte[1024];
                        string sInString = Encoding.ASCII.GetString(bInBuff, 0, n);
                        sPostDataString += sInString;
                        ChatServerTypes.UpdateTextSafe(ChatServerTypes.UiForm.textBox1, "Post:\r\n" + sInString + "\r\n");
                    }
                }
            }
        }
        public void DoServeClientDataIn()
        {
            if (hlcIO.Request.HttpMethod == "POST")
            {
                while (!isPostDataRead)
                {
                    int n = hlcIO.Request.InputStream.Read(bInBuff, 0, 1024);
                    if (n == 0)
                    {
                        isPostDataRead = true;
                    }
                    else
                    {
                        string sInString = Encoding.ASCII.GetString(bInBuff, 0, n);
                        //                (parent. OnMessage).
                        PostData = sInString;
                        ChatServerTypes.UpdateTextSafe(ChatServerTypes.UiForm.textBox1, "Logon: \r\n" + sInString);
                    }
                }
            }
            //string sHeads = "<textarea style=\"height:600px; width:600px;\">";

            //foreach (string hrh in hlcIO.Request.Headers)
            //{ sHeads += hrh + "\\" + hlcIO.Request.Headers[hrh] + "\r\n"; }
            //sHeads += "</textarea>\r\n";
            bInBuff = null;
            hlcIO.Response.StatusCode  = 200;
            hlcIO.Response.SendChunked = false;
            string sRet  = "";
            string sFile = "Web\\";
            string sID   = "";

            try
            {
                hlcIO.Response.StatusCode = 200;
                if (hlcIO.Request.Url.AbsolutePath == "/")
                {
                    sFile += "Welcome.html";
                }
                else
                {
                    string sF = hlcIO.Request.Url.AbsolutePath;
                    if (sF.Contains("css"))
                    {
                        sF = sF;
                    }
                    sF     = sF.StartsWith("/") ? sF.Substring(1) : sFile;
                    sF     = sF.Replace("/", "\\");
                    sFile += sF;
                }


                sRet = System.IO.File.ReadAllText(sFile);
                sRet = WebPageParser.interpretFile(sRet, ref sID);
            }
            catch
            {
                hlcIO.Response.StatusCode = 404;
                sRet = "<html><body>404</body></html>";
            }
            bool isBinary = false;

            hlcIO.Response.ContentType     = UrlHelper.getMimeType(sFile, out isBinary);
            hlcIO.Response.ContentEncoding = System.Text.Encoding.ASCII;
            hlcIO.Response.ContentLength64 = sRet.Length;
            nContentLength = sRet.Length;
            byte[] b = Encoding.ASCII.GetBytes(sRet);

            int iLen; int iExtras;

            Int32[] b32 = StringByteInt.BytesToInt32s(b, out iLen, out iExtras);
            StringByteInt.minusFromAll(ref b32, 127);

            StringByteInt.plusToAll(ref b32, 127);

            b = StringByteInt.Int32sToBytes(b32, iLen, iExtras);
            string sss = Encoding.ASCII.GetString(b, 0, b.Count());

            AddToWriteList(b);
        }
Esempio n. 3
0
      public static InterpreterResponse[] InterpretCommands(string sID, string sIn, ref WebSessionClass SessInf)
      {
          string sin = sIn.ToLower();
          InterpreterResponse irRet = new InterpreterResponse()
          {
              SResponse = "", IsBroadcast = false, IsToSEnd = false
          };

          irRet.IsToSEnd = true; irRet.IsBroadcast = false;
          string sOP = "getdiv";

          sOP = sIn.Substring(0, sIn.IndexOf(";")).ToLower();
          switch (sOP)
          {
          case "regsession":
          {
              foreach (KeyValuePair <string, WebSessionClass> wsc in SessionDataHandler.SessionDataList)
              {
                  if (wsc.Key == sID)
                  {
                      SessInf = wsc.Value;

                      irRet.SResponse = GetSetDivString(sID, "statusDiv", "Welcome to " + SessInf.Room + " " + SessInf.SUser);
                      irRet.IsToSEnd  = true; irRet.IsBroadcast = false;
                      return(new InterpreterResponse[] { irRet });
                  }
              }
              break;
          }

          case "msgpost":
          {
              string sKey = sIn.Substring(sIn.IndexOf("<key>") + 5);
              sKey = sKey.Substring(0, sKey.IndexOf("</key>"));
              string sDiv = sIn.Substring(sIn.IndexOf("<div>") + 5);
              sDiv = sDiv.Substring(0, sDiv.IndexOf("</div>"));
              SessInf.TypingNowString += (sKey.Contains("Enter")) ? "\r\n" : sKey;
              if (sKey == "Enter")
              {
                  SessionDataHandler.SessionDataList[SessInf.sSession].sLastWords  = SessInf.TypingNowString;
                  SessionDataHandler.SessionDataList[SessInf.sSession].LastMSGDate = DateTime.Now;
                  SessInf.sLastWords = SessInf.TypingNowString;
                  InterpreterResponse ir1 = new InterpreterResponse();
                  InterpreterResponse ir2 = new InterpreterResponse();
                  ir1.SResponse   = GetSetDivString("*", sDiv, "");        // SessInf.TypingNowString);
                  ir1.IsBroadcast = false; ir1.IsToSEnd = true;

                  ir2.SResponse   = GetAddToDivString("*", "mainPageDiv", "<span style=\"background-color:green\">" + SessInf.SUser + "</span>:<div style=\"background-color:blue;\">" + SessInf.TypingNowString.Replace("\r\n", "<br />") + "</div>");
                  ir2.IsRoomCast  = true;
                  ir2.sRoomTo     = SessInf.Room;
                  ir2.IsBroadcast = true; ir2.IsToSEnd = true;
                  if (sKey == "Enter")
                  {
                      SessInf.TypingNowString = "";
                  }
                  return(new InterpreterResponse[] { ir1, ir2 });
              }
              if (sKey == "ShiftEnter")
              {
                  SessInf.TypingNowString += "\r\n";
                  InterpreterResponse ir = new InterpreterResponse()
                  {
                      IsBroadcast = false,
                      IsToSEnd    = true,
                      IsRoomCast  = false
                  };
                  string sKeys = GetSetDivString("*", sDiv, SessInf.TypingNowString.Replace("\r\n", "<br />"));
                  return(new InterpreterResponse[] { irRet });
              }


              if (sKey == "Enter")
              {
                  SessInf.TypingNowString = "";
                  return(new InterpreterResponse[] { irRet });
              }

              if (!sKey.Contains("Enter"))
              {
                  irRet.IsBroadcast = false;
                  irRet.IsToSEnd    = true;
                  string sKeys = GetSetDivString("*", sDiv, SessInf.TypingNowString);
                  return(new InterpreterResponse[] { irRet });
              }
              break;
          }

          case "getdiv":
          {
              try
              {
                  string sDiv = sIn.Substring(0, sIn.IndexOf("</div>"));
                  sDiv            = sDiv.Substring(sDiv.ToLower().IndexOf("<div>") + 5);
                  irRet.SResponse = GetSetDivString(sID, sDiv, "User: "******".");
                  irRet.IsToSEnd  = true; irRet.IsBroadcast = false;
                  return(new InterpreterResponse[] { irRet });
              }
              catch (Exception e)
              {
                  ChatServerTypes.UpdateTextSafe(ChatServerTypes.UiForm.textBox1, "GetDiv Error;\r\n");
                  return(new InterpreterResponse[] { });
              }
          }
          }
          return(new InterpreterResponse[] { irRet });
      }
Esempio n. 4
0
        private void wsWriteTimerTick(object state)
        {
            if (isTicking)
            {
                return;
            }
            try
            {
                isTicking = true;
                if (IsNeeded == false)
                {
                    hlcIO.Response.Close();
                }
                if (wsOutWriteList == null)
                {
                    tIOWriteTimer.Dispose();
                    this.Dispose();
                }

                if (wsOutWriteList.Count() == 0)
                {
                    tIOWriteTimer.Change(200, 200);
                    isTicking = false;
                    return;
                }
                if (wsOutWriteList.Count() > 0 & wsOutWriteList.Count() < 10)
                {
                    tIOWriteTimer.Change(25, 25);
                    isTicking = false;
                }
                if (wsOutWriteList.Count() >= 10)
                {
                    tIOWriteTimer.Change(10, 10);
                }
                if (wsOutWriteList.Count > 25)
                {
                    wsOutWriteList.RemoveRange(0, 10);
                    ChatServerTypes.UpdateTextSafe(ChatServerTypes.UiForm.textBox1, "Dropped-packets-out session:" + SSIDws);
                }
                try
                {
                    SendMessage(new ArraySegment <byte>(wsOutWriteList[0], 0, wsOutWriteList[0].Count()), wsOutWriteList.Count <= 1);

                    wscontentSentCount += wsOutWriteList[0].Count();
                    wsOutWriteList.RemoveAt(0);
                    string sSend = "setdivhtm;";
                    sSend += "<target>*</target>";

                    sSend += "<elementid>";
                    sSend += "DateTimeDiv";
                    sSend += "</elementid>";
                    string sShms = DateTime.Now.ToLongTimeString() + ":";
                    string sMs   = "000" + DateTime.Now.Millisecond.ToString();
                    int    nnn   = sMs.Length;
                    sMs = sMs.Substring(nnn - 3);
                    string sd = sMs;

                    //sd = sd.Substring(0, nnn - 2);
                    sShms += sd;

                    sSend += sShms;
                    byte[] bSend = Encoding.ASCII.GetBytes(sSend);
                    isTicking = false;
                    //AddToWriteList(bSend);

                    //sDiv = sS.substring(sS.toLowerCase().indexOf("<element>") + 9);
                    //sDiv = sDiv.substring(0, sDiv.toLowerCase().indexOf("</element>"));
                }
                catch (WebSocketException e)
                {
                    string s = e.InnerException.ToString();
                    s         = s;
                    isTicking = false;
                }
                catch (Exception e)
                {
                    string s = e.InnerException.ToString();
                    s         = s;
                    IsNeeded  = false;
                    isTicking = false;
                }
            }
            catch
            {
                isTicking = false;
            }
        }