Using Apache Thrift Webservices From Unity

From Unify Community Wiki
Jump to: navigation, search

TODO

Thrift page:

http://incubator.apache.org/thrift/

Windows binaries can now be downloaded from the normal Thrift downloads site.

Put this file in Thrift/Transport

TUnityWwwClient.cs

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *	 http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * 
 * 
 */
 
using UnityEngine;
using System;
using System.Collections.Generic;
using System.IO;
//using System.Net;
 
namespace Thrift.Transport
{
	public class TUnityWwwClient : TTransport
	{
		protected string url;
		private MemoryStream inputStream;
		private MemoryStream outputStream = new MemoryStream();
 
		public TUnityWwwClient(string u)
		{
			url = u;
		}
		public override bool IsOpen
		{
			get {return true;}
		}
 
		public override void Open() {}
 
		public override void Close() 
		{
			if (inputStream != null)
			{
				inputStream.Close();
				inputStream = null;
			}
			if (outputStream != null)
			{
				outputStream.Close();
				outputStream = null;
			}			
		}
 
		public override int Read(byte[] buf, int off, int len) 
		{
			if (inputStream == null)
			{
				throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No request has been sent");
			}
 
			try
			{
				int ret = inputStream.Read(buf, off, len);
				if (ret == -1)
				{
					throw new TTransportException(TTransportException.ExceptionType.EndOfFile, "No more data available");
				}
				return ret;
			}
			catch (IOException iox)
			{ 
				throw new TTransportException(TTransportException.ExceptionType.Unknown, iox.ToString());
			}			
		}
 
		public override void Write(byte[] buf, int off, int len)
		{
			outputStream.Write(buf, off, len);
		}
 
		public override void Flush()
		{
			try 
			{
				SendRequest();
			}
			finally
			{
				outputStream = new MemoryStream();
			}
		}	
 
		void SendRequest()
		{
			try
			{
				WWW w = new WWW(url, outputStream.ToArray());
				while (!w.isDone) {}
				inputStream = new MemoryStream(w.bytes);
			}
			catch (Exception e)
			{
				throw new TTransportException(TTransportException.ExceptionType.Unknown, "Error in server communication: " + e.Message);
			}
		}
	}
}

Note that this doesn't work in the Unity Web player as it locks up on the line:

while (!w.isDone) {}

However it does work in standalone builds.

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox