c cgi解析unity3d的二进制webform

/*
set PATH=C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin;C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE;
set INCLUDE=C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\;C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include\;
set LIB=C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\lib\;C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\;C:\Program Files (x86)\Windows Kits\8.0\Lib\win8\um\x86\;
del Test.exe
cl Test.c
del Test.obj,Test.exp,Test.lib
Test.exe
copy Test.exe "C:\Program Files (x86)\Apache Software Foundation\Apache2.2\cgi-bin\saveLog.cgi"
@rem    pause
*/

#include <stdio.h>
#include <stdlib.h>
void main(int argc, char *argv[]){    
        int i, n;
        #define _SIZE_ 2048
        char _buffer[_SIZE_];
        char _bytes[_SIZE_];
        int _bytesLength;
        char* _se;
           printf("Content-type:application/octet-stream\n\n" );
       //    fprintf( stdout, "<html><title>post</title>" );
        if( getenv("CONTENT_LENGTH") ){
            n = atoi( getenv("CONTENT_LENGTH") );
        }else {
            n = 0;
            fprintf( stdout, "(NULL)" );
            return;
        }
        memset(_buffer,0,_SIZE_);
        for( i=0; i<n; i++ ){
//           fputc( getc(stdin), stdout );
            _buffer[i] = getc(stdin);
        }
        #define _SEACH_STRING_ "filename"
        _se = strstr(_buffer,_SEACH_STRING_);
        //printf(_se);
        //fprintf( stdout, "" );
        //http://127.0.0.1/cgi-bin/saveLog.cgi
        if(_se == NULL)    {
            printf("not found!");
        }else{
            //printf("%d,(%s)",strlen(_buffer,_se));
            _bytesLength = 4;
            memset(_bytes,0,_SIZE_);
            memcpy(_bytes,_se+strlen(_SEACH_STRING_)+5,_bytesLength);
            
            for(i = 0;i < _bytesLength;i++){
                printf("%d,",_bytes[i]);
            }
        }
}
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

public class Init :MonoBehaviour
{
    void Awake ()
    {
        //print(gameObject.name);
//        TweenPosition
    }

    void Update ()
    {

    }

    void Start ()
    {

    }

    public void onCgiTest ()
    {
        screenAndUpload ();
    }

    void screenAndUpload ()
    {
        WWWForm form = new WWWForm ();
        form.AddField ("frameCount", "(" + Time.frameCount.ToString () + ")");
        byte[] _uploadBytes = testBytes;

        string _tStr = "";
        for (int i = 0; i < _uploadBytes.Length; i++) {
            _tStr += _uploadBytes [i].ToString () + ",";
        }
        Debug.Log ("client:(" + _tStr + ")");
        form.AddBinaryData ("bytes", _uploadBytes, "");
        WWW w = new WWW ("http://127.0.0.1/cgi-bin/savelog.cgi?" + UnityEngine.Random.Range (0.0f, 1.0f).ToString (), form);
        StartCoroutine (screenAndUploadCoroutine (w));
    }

    IEnumerator screenAndUploadCoroutine (WWW w)
    {
        yield return w;
        if (!String.IsNullOrEmpty (w.error)) {
            //print (w.error);
        } else {
            print (w.data);
        }
    }

    byte[] testBytes {
        get {
            MBytes b = new MBytes ();
            b.addInt (69923);
            return b.data;
        }
    }
}

/*

--zcYvw8EWu622BRvwDuR9ZMyucYxHAapZXRlzb4Sa
    Content-Type: text/plain; charset="utf-8"
    Content-disposition: form-data; name="frameCount"
    
    (756)
        --zcYvw8EWu622BRvwDuR9ZMyucYxHAapZXRlzb4Sa
        Content-Type: application/octet-stream
        Content-disposition: form-data; name="bytes"; filename=""
    
    ABab
        --zcYvw8EWu622BRvwDuR9ZMyucYxHAapZXRlzb4Sa--
        ������������
        UnityEngine.MonoBehaviour:print(Object)
        <screenAndUploadCoroutine>c__Iterator4:MoveNext() (at Assets/Script/Login.cs:81)
*/

public class MBytes
{
    ArrayList send = new ArrayList ();

    public void addInt (int s)
    {
        send.Add ((byte)((0xff000000 & s) >> 24));
        send.Add ((byte)((0xff0000 & s) >> 16));
        send.Add ((byte)((0xff00 & s) >> 8));
        send.Add ((byte)(0xff & s));
    }

    public void addByte (byte s)
    {
        send.Add (s);
    }
    
    public void addShort (short s)
    {
        send.Add ((byte)((s & 0xff00) >> 8));
        send.Add ((byte)(s & 0xff));
    }

    public void addString (string s)
    {
        byte[] str = Encoding.UTF8.GetBytes (s);
        short sl = (short)str.Length;
        addShort (sl);
        for (int i = 0; i < sl; i++) {
            send.Add (str [i]);
        }
    }

    public byte[] data {
        get {
            return (byte[])send.ToArray (typeof(byte));
        }
    }
}

 

posted @ 2015-06-25 13:59  泥潭里的金鱼  阅读(213)  评论(0)    收藏  举报