BadScript 2
Loading...
Searching...
No Matches
BadNetHost.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Net;
4
7
9
13public class BadNetHost
14{
18 private readonly HttpListener m_Listener = new HttpListener();
19
24 public BadNetHost(IEnumerable<string> prefixes)
25 {
26 foreach (string prefix in prefixes)
27 {
28 m_Listener.Prefixes.Add(prefix);
29 }
30 }
31
38 private static IEnumerator<BadObject> AcceptClient(HttpListener listener, Action<BadObject> callback)
39 {
40 bool accepted = false;
41 listener.BeginGetContext(
42 r =>
43 {
44 try
45 {
46 HttpListenerContext ctx = listener.EndGetContext(r);
47 callback(new BadHttpContext(ctx));
48 }
49 catch (Exception)
50 {
51 //Do Nothing
52 }
53
54 accepted = true;
55 },
56 null
57 );
58
59 while (!accepted)
60 {
61 yield return BadObject.Null;
62 }
63 }
64
65
71 {
72 BadInteropRunnable? runnable = null;
73
74 // ReSharper disable once AccessToModifiedClosure
75 // ReSharper disable once PossibleNullReferenceException
76 runnable = new BadInteropRunnable(AcceptClient(m_Listener, r => runnable!.SetReturn(r)));
77
78 return new BadTask(runnable, "AcceptClient");
79 }
80
84 public void Start()
85 {
86 m_Listener.Start();
87 }
88
92 public void Stop()
93 {
94 m_Listener.Stop();
95 }
96
100 public void Close()
101 {
102 m_Listener.Close();
103 }
104
108 public void Abort()
109 {
110 m_Listener.Abort();
111 }
112}
Implements a Runnable that can return a value.
Implements a Task Object.
Definition BadTask.cs:17
Wrapper for the HttpListenerContext.
Implements a BadScript HttpListener Host.
Definition BadNetHost.cs:14
void Close()
Close the Listener.
void Start()
Starts the Listener.
Definition BadNetHost.cs:84
void Abort()
Aborts the Listener.
BadNetHost(IEnumerable< string > prefixes)
Constructs a new BadNetHost with the given prefixes.
Definition BadNetHost.cs:24
BadTask AcceptClient()
Accepts a Client.
Definition BadNetHost.cs:70
readonly HttpListener m_Listener
The Listener that is to be used.
Definition BadNetHost.cs:18
void Stop()
Stops the Listener.
Definition BadNetHost.cs:92
static IEnumerator< BadObject > AcceptClient(HttpListener listener, Action< BadObject > callback)
Accepts a Client.
Definition BadNetHost.cs:38
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Contains task/async Extensions and Integrations for the BadScript2 Runtime.
Contains Network Hosting Extensions and APIs for the BadScript2 Runtime.
Contains the Runtime Objects.
Definition BadArray.cs:10