WIP
parent
f8275974b1
commit
6dc79e6682
|
@ -50,6 +50,7 @@
|
|||
<Compile Include="collections\JSONCollectionResource<T>.cs" />
|
||||
<Compile Include="collections\IEntityCollectionInterface.cs" />
|
||||
<Compile Include="collections\EntityMapper.cs" />
|
||||
<Compile Include="websocket\WebsocketResource.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ln.http\ln.http.csproj">
|
||||
|
@ -73,6 +74,7 @@
|
|||
<Folder Include="reflection\" />
|
||||
<Folder Include="doc\" />
|
||||
<Folder Include="collections\" />
|
||||
<Folder Include="websocket\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Reflection;
|
|||
using ln.http.exceptions;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.ComponentModel;
|
||||
namespace ln.http.resources.reflection
|
||||
{
|
||||
public class Reflector
|
||||
|
@ -55,6 +56,10 @@ namespace ln.http.resources.reflection
|
|||
{
|
||||
currentValue = properties[next].GetValue(currentValue);
|
||||
}
|
||||
else if (methods.ContainsKey(next))
|
||||
{
|
||||
currentValue = InvokeMethod(next, httpRequest, currentValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new KeyNotFoundException(next);
|
||||
|
@ -93,6 +98,31 @@ namespace ln.http.resources.reflection
|
|||
return currentValue;
|
||||
}
|
||||
|
||||
object InvokeMethod(string methodName, HttpRequest httpRequest, object currentValue)
|
||||
{
|
||||
MethodInfo methodInfo = methods[methodName];
|
||||
ParameterInfo[] parameterInfos = methodInfo.GetParameters();
|
||||
object[] parameters = new object[parameterInfos.Length];
|
||||
|
||||
for (int n=0;n<parameterInfos.Length;n++)
|
||||
{
|
||||
if (httpRequest.Query.ContainsKey(parameterInfos[n].Name))
|
||||
{
|
||||
TypeConverter typeConverter = TypeDescriptor.GetConverter(parameterInfos[n].ParameterType);
|
||||
if ((typeConverter != null) && typeConverter.CanConvertFrom(typeof(string)))
|
||||
{
|
||||
parameters[n] = typeConverter.ConvertFrom(httpRequest.Query[parameterInfos[n].Name]);
|
||||
}
|
||||
else
|
||||
{
|
||||
parameters[n] = Convert.ChangeType(httpRequest.Query[parameterInfos[n].Name], parameterInfos[n].ParameterType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return methodInfo.Invoke(currentValue, parameters);
|
||||
}
|
||||
|
||||
|
||||
public static bool IsValidNameChar(char ch)
|
||||
{
|
||||
|
|
|
@ -0,0 +1,116 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ln.http.websocket;
|
||||
using ln.logging;
|
||||
using System.IO;
|
||||
using ln.types;
|
||||
using System.Text;
|
||||
|
||||
namespace ln.http.resources.websocket
|
||||
{
|
||||
public class WebSocketResourceRequestContext
|
||||
{
|
||||
public WebsocketResource WebsocketResource { get; }
|
||||
public WebSocket WebSocket { get; }
|
||||
public HttpRequest HttpRequest { get; }
|
||||
|
||||
public WebSocketResourceRequestContext(WebsocketResource websocketResource,WebSocket webSocket,HttpRequest httpRequest)
|
||||
{
|
||||
WebsocketResource = websocketResource;
|
||||
WebSocket = webSocket;
|
||||
HttpRequest = httpRequest;
|
||||
}
|
||||
}
|
||||
|
||||
public enum WSREvent { CONNECT, CLOSE }
|
||||
public delegate void WSRConnection(WebsocketResource webSocketResource, WebSocket webSocket, WSREvent ev);
|
||||
|
||||
public class WebsocketResource : Resource
|
||||
{
|
||||
public event WSRConnection Connection;
|
||||
|
||||
public WebsocketResource(Resource container,String resourceName)
|
||||
:base(container,resourceName)
|
||||
{
|
||||
}
|
||||
|
||||
public override void AddResource(Resource resource)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override bool Contains(string name)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override IEnumerable<Resource> GetResources()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override HttpResponse GetResponse(HttpRequest httpRequest)
|
||||
{
|
||||
MemoryStream buffer = null;
|
||||
|
||||
WebSocket webSocket = new WebSocket(httpRequest);
|
||||
webSocket.WebSocketEvent += (WebSocket sender, WebSocketEventArgs e) => {
|
||||
switch (e.EventType)
|
||||
{
|
||||
case WebSocketEventType.MESSAGE:
|
||||
if (buffer == null)
|
||||
buffer = new MemoryStream();
|
||||
|
||||
buffer.WriteBytes(e.BinaryMessage);
|
||||
|
||||
if (e.Frame.FIN)
|
||||
{
|
||||
WebSocketResourceRequestContext context = new WebSocketResourceRequestContext(
|
||||
this,
|
||||
webSocket,
|
||||
httpRequest
|
||||
);
|
||||
|
||||
if (e.IsBinary)
|
||||
MessageReceived(context, buffer.ToArray());
|
||||
else
|
||||
MessageReceived(context, Encoding.UTF8.GetString(buffer.ToArray()));
|
||||
|
||||
buffer.Dispose();
|
||||
buffer = null;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
Connection(this, webSocket, WSREvent.CONNECT);
|
||||
webSocket.Run();
|
||||
} catch (Exception e)
|
||||
{
|
||||
Logging.Log(e);
|
||||
}
|
||||
Connection(this, webSocket, WSREvent.CLOSE);
|
||||
|
||||
httpRequest.GetConnectionStream().Close();
|
||||
return null;
|
||||
}
|
||||
|
||||
public override void RemoveResource(Resource resource)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public virtual void MessageReceived(WebSocketResourceRequestContext requestContext,byte[] binaryMessage)
|
||||
{
|
||||
Logging.Log(LogLevel.WARNING, "WebSocketResource: received unhandled binary message: {0}",BitConverter.ToString(binaryMessage));
|
||||
}
|
||||
public virtual void MessageReceived(WebSocketResourceRequestContext requestContext, String textMessage)
|
||||
{
|
||||
Logging.Log(LogLevel.WARNING, "WebSocketResource: received unhandled text message: {0}", textMessage);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue