// ------------------------------------------------------------------------------------ private void OnClk_Test(object sender, EventArgs e) { #if TEST_Set_DirFileNames FileLister.Set_DirFileNames(ms_TEST_SetDirFilesNames_WS_buf, "./"); #endif }
// ------------------------------------------------------------------------------------ public async Task Spawn_Context(HttpListenerContext context, uint idx_context) { // AcceptWebSocketAsync() は、キャンセルトークンをサポートしていない // 引数: サポートされている WebSocket サブプロトコル HttpListenerWebSocketContext wsc = await context.AcceptWebSocketAsync(null); MainForm.StdOut("--- WebSocket 接続完了\r\n"); using (m_WS = wsc.WebSocket) using (WS_Buf_Pool.MemBlock mem_blk = WS_Buf_Pool.Lease_MemBlock()) { m_read_WS_buf = new Read_WS_Buf(mem_blk.m_ary_buf); m_write_WS_buf = new Write_WS_Buffer(mem_blk.m_ary_buf); try { // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #if CREATE_TEST_LEXED_CODE // Test Lexed Code を送信する Write_WS_Buffer write_ws_buf = MainForm.ms_DBG_write_WS_buf; await m_WS.SendAsync( new ArraySegment <byte>(write_ws_buf.Get_buf(), 0, write_ws_buf.Get_idx_byte_cur()) , WebSocketMessageType.Binary, true, ms_cts_shutdown.Token); #else // まずはルートフォルダのファイル情報を送信しておく FileLister.Set_DirFileNames(m_write_WS_buf, "./"); await m_write_WS_buf.SendAsync(m_WS, ms_cts_shutdown); #endif while (true) { WebSocketReceiveResult rslt = await m_WS.ReceiveAsync( new ArraySegment <byte>(mem_blk.m_ary_buf), ms_cts_shutdown.Token); if (rslt.EndOfMessage == false) { MainForm.StdOut("--- 不正な大きさのデータを受信しました。クライアントを切断します\r\n"); break; } if (m_WS.State == WebSocketState.CloseReceived) { MainForm.StdOut("--- クライアントが接続を Close しました\r\n"); break; } m_read_WS_buf.Renew(rslt.Count); switch (m_read_WS_buf.Read_ID()) { case ID.DirFileList: { ID id = m_read_WS_buf.Read_ID(); if (id != ID.Text) { throw new Exception($"不正なパラメータを受信 DirFileList / id -> {((byte)id).ToString()}"); } FileLister.Set_DirFileNames(m_write_WS_buf, m_read_WS_buf.Get_text_cur()); await m_write_WS_buf.SendAsync(m_WS, ms_cts_shutdown); } continue; case ID.Files_inDir: { ID id = m_read_WS_buf.Read_ID(); if (id != ID.Text) { throw new Exception($"不正なパラメータを受信 Files_inDir / id -> {((byte)id).ToString()}"); } string str_path_dir = m_read_WS_buf.Get_text_cur(); id = m_read_WS_buf.Read_ID(); if (id != ID.Num_int) { throw new Exception($"不正なパラメータを受信 Files_inDir / id -> {((byte)id).ToString()}"); } int SEC_Updated_recv = m_read_WS_buf.Get_Num_int(); FileLister.OnFiles_inDir(m_write_WS_buf, str_path_dir, SEC_Updated_recv); await m_write_WS_buf.SendAsync(m_WS, ms_cts_shutdown); } continue; case ID.MD_file: { var(path_md_file, SEC_Updated, idx_byte_SEC_updated) = m_read_WS_buf.Read_Req_MD(); MainForm.StdOut($"path_md_file : {path_md_file} / SEC_Updated : {SEC_Updated.ToString()}\r\n"); // ID.MD_file -> ID_Text (path_dir) -> ID_Text (file name) は Receive時の値を流用 m_write_WS_buf.Set_idx_byte(idx_byte_SEC_updated); m_write_WS_buf.Wrt_Num_int(1000); // 1000 は試験値 Lexer.LexFile(m_write_WS_buf, path_md_file); MainForm.StdOut("--- Lexing 処理完了\r\n"); m_write_WS_buf.Simplify_Buf(); MainForm.StdOut("--- Simplify_Buf 処理完了\r\n"); await m_write_WS_buf.SendAsync(m_WS, ms_cts_shutdown); } continue; } MainForm.StdOut($"--- ReceiveAsync() : 受信バイト数 -> {rslt.Count}\r\n"); MainForm.HexDump(mem_blk.m_ary_buf, 0, 20); DBG_WS_Buffer.Show_WS_buf(MainForm.ms_RBox_stdout, mem_blk.m_ary_buf, rslt.Count); // 今は、index.md のみを解析するようにしている // string ret_str = Lexer.LexFile(m_write_WS_buf, "md_root/index.md"); // string str_recv = ms_utf16_encoding.GetString(mem_blk.m_ary_buf, 0, rslt.Count); // MainForm.StdOut(str_recv); } await m_WS.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "接続を終了します", ms_cts_shutdown.Token); } catch (OperationCanceledException) { MainForm.StdOut("--- サーバーシャットダウンのシグナルを受信しました\r\n"); } catch (WebSocketException ex) { MainForm.StdOut($"!!! 例外発を補足しました WebSoketErrorCode : {ex.WebSocketErrorCode.ToString()}\r\n"); MainForm.StdOut($" {ex.ToString()}\r\n"); } catch (Exception ex) { MainForm.StdOut($"!!! 例外を補足しました : {ex.ToString()}\r\n"); } } MdSvr.Remove_task_context(idx_context); MainForm.StdOut("--- WebSocket 切断完了\r\n"); }