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");
                    }
                }
            }
        }
Esempio n. 2
0
        async public void DoWSServeSocket(WSServerTaskAndOject wssto)
        {
            CancellationToken       ct  = wssto.ct;
            CancellationTokenSource cts = wssto.wsCts;

            NSXUserObj.sIP           = hlcIO.Request.RemoteEndPoint.Address.ToString();
            NSXUserObj.sUserAgent    = hlcIO.Request.UserAgent;
            NSXUserObj.DateStarted   = DateTime.Now;
            NSXUserObj.DateOfLastMsg = DateTime.Now;
            try
            {
                //hlcIO.Response.SendChunked = true;
                wsc = await hlcIO.AcceptWebSocketAsync("Neals");
            }
            catch (WebSocketException we)
            {
                isOKReading = false; IsNeeded = false;
                return;
            }
            WebSocketReceiveResult wr = null;

            while (isOKReading && !ct.IsCancellationRequested)
            {
                byte[] bWsIn = new byte[1024];

                try
                {
                    wr = await wsc.WebSocket.ReceiveAsync(new ArraySegment <byte>(bWsIn), wsso.ct);
                }
                catch
                {
                    isOKReading = false; IsNeeded = false;
                    cts.Cancel();
                    wssto.wsioh.Dispose();
                    IsNeeded = false;
                    ctsServerThread.Cancel();
                    wssto.wsioh.Dispose();
                    return;
                }
                if (wr.MessageType == WebSocketMessageType.Close)
                {
                    isOKReading = false; IsNeeded = false;
                }
                if (wr.MessageType == WebSocketMessageType.Text)
                {
////                    ChatServerTypes.UpdateTextSafe(ParentCW.Frm.textBox1, "Starting Server \r\n");

                    string sIN = Encoding.ASCII.GetString(bWsIn, 0, wr.Count);
                    //ChatServerTypes.UpdateTextSafe(ParentCW.Frm.textBox1, sIN + "\r\n");
                    string sHeads = "<textarea style=\"height:200px; width:200px;\">";

                    foreach (string hrh in hlcIO.Request.Headers)
                    {
                        sHeads += hrh + "\\" + hlcIO.Request.Headers[hrh] + "\r\n";
                    }
                    sHeads += "</textarea>\r\n";
                    bInBuff = null;

                    byte[] b = Encoding.ASCII.GetBytes(sHeads);

                    //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());
                    bool   isBroadcast; bool isToSend;

                    string sData = sIN.Substring(0, sIN.IndexOf("</data>"));
                    sData = sData.Substring(sData.IndexOf("<data>") + 6);

                    string sid = sIN.Substring(sIN.IndexOf("<sid>") + 5).ToLower();
                    sid = sid.Substring(0, sid.IndexOf("</sid>")).ToLower();

                    sID = sid;
                    try
                    {
                        SessionInfo = SessionDataHandler.SessionDataList[sid];
                    }
                    catch { SessionInfo = new WebSessionClass(); }

                    //ChatServerTypes.UpdateTextSafe(ChatServerTypes.UiForm.textBox1, "Session not matching on WS request!");

                    InboundMessageParser.InterpreterResponse[] sRet = InboundMessageParser.InterpretCommands(sID, sData, ref SessionInfo);
                    foreach (InboundMessageParser.InterpreterResponse irMSG in sRet)
                    {
                        if (irMSG.IsToSEnd && irMSG.SResponse != "")
                        {
                            if (irMSG.IsRoomCast)
                            {
                                ParentCW.SendToRoom(irMSG.sRoomTo, irMSG.SResponse);
                            }
                            else if (irMSG.IsBroadcast)
                            {
                                ParentCW.SendToAll(irMSG.SResponse);
                            }
                            else
                            {
                                AddToWriteList(Encoding.ASCII.GetBytes(irMSG.SResponse));
                            }
                        }
                    }
                    //t = Task.Run(() => DoSomeWork(1, token), token);
                }
            }
            ctsServerThread.Cancel();
            wssto.wsioh.Dispose();


            string s = ctsServerThread.Token.CanBeCanceled.ToString();
        }