Skip to content

Instantly share code, notes, and snippets.

@jgauffin
Created January 12, 2015 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgauffin/d595865a538af306d99d to your computer and use it in GitHub Desktop.
Save jgauffin/d595865a538af306d99d to your computer and use it in GitHub Desktop.
Using Griffin.Framework without a BodyDecoder
using System;
using System.IO;
using System.Net;
using System.Text;
using Griffin.Net.Channels;
using Griffin.Net.Protocols.Http;
using Griffin.Net.Protocols.Http.Messages;
using Griffin.Net.Protocols.Http.Serializers;
using Griffin.Net.Protocols.Serializers;
namespace PlainTextExample
{
public class PlainTextSerializer : IMessageSerializer
{
public object Deserialize(string contentType, Stream source)
{
var buf = new byte[source.Length];
source.Read(buf, 0, buf.Length);
var result = Encoding.ASCII.GetString(buf);
var form = new ParameterCollection();
form.Add("EDI", result);
return new FormAndFilesResult {Form = form};
}
public void Serialize(object source, Stream destination, out string contentType)
{
throw new NotSupportedException("This serializer is for servers only.");
}
public string[] SupportedContentTypes
{
get
{
//just convert these formats to text.
return new[] {"text/plain", "text/html", "application/xml"};
}
}
}
class Program
{
static void Main(string[] args)
{
var listener = new Griffin.Net.Protocols.Http.HttpListener();
listener.BodyDecoder = new PlainTextSerializer();
listener.MessageReceived += OnMessage;
listener.Start(IPAddress.Any, 1234);
WebClient client = new WebClient();
client.UploadString("http://127.0.0.1:1234", "HEllo world");
Console.WriteLine("Running, press ENTER to quit");
Console.ReadLine();
}
private static void OnMessage(ITcpChannel channel, object message)
{
var request = (HttpRequest) message;
var data = request.Form["EDI"];
Console.WriteLine(data);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment