miduo_client/Assets/LuaFramework/Scripts/Manager/NetworkManager.cs

308 lines
9.8 KiB
C#
Raw Blame History

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using LuaInterface;
using System.Text;
using GameCore;
using UnityEngine.Networking;
namespace GameLogic
{
class AcceptAllCertificatesSignedWithASpecificPublicKey : CertificateHandler
{
protected override bool ValidateCertificate(byte[] certificateData)
{
return true;
}
}
public class NetMsg
{
public int msgId;
public int sid;
public int result;
public ByteBuffer msg;
public NetMsg(int msgId, int sid, int result, ByteBuffer msg)
{
this.msgId = msgId;
this.sid = sid;
this.msg = msg;
this.result = result;
}
}
public class NetworkStateInfo
{
public NetworkStateType type;
public string msg;
}
public class NetworkManager : UnitySingleton<NetworkManager>
{
private List<SocketClient> socketList = new List<SocketClient>();
void Awake()
{
}
void OnApplicationQuit()
{
Reset();
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
void OnDestroy()
{
Reset();
}
public void Reset()
{
foreach(var s in socketList)
{
s.Close();
}
socketList.Clear();
}
void Update()
{
for(int i = 0; i < socketList.Count; i++)
{
socketList[i].Update();
}
}
public void OnInit()
{
Util.CallMethod("SocketManager", "Start");
}
public void Unload()
{
Util.CallMethod("SocketManager", "Unload");
}
public SocketClient AddSocket(string ipAddress, int port)
{
XDebug.Log.l("SocketClient AddSocket");
SocketClient socket = new SocketClient(ipAddress, port);
socket.netMgr = this;
socketList.Add(socket);
return socket;
}
public void SendGetHttp(string url, LuaFunction callback, Action<string> sharpFunc, Action errorAction, LuaFunction errorLuaFunc)
{
StartCoroutine(HttpGet_Co(url, callback, sharpFunc, errorAction, errorLuaFunc));
}
IEnumerator HttpGet_Co(string url, LuaFunction callback, Action<string> sharpFunc, Action errorAction, LuaFunction errorLuaFunc)
{
if (string.IsNullOrEmpty(url))
{
if (errorAction!=null)
{
errorAction();
}
if (errorLuaFunc != null)
{
errorLuaFunc.Call();
}
yield break;
}
UnityWebRequest request = UnityWebRequest.Get(url);
request.certificateHandler = new AcceptAllCertificatesSignedWithASpecificPublicKey();
yield return request.SendWebRequest();
if (request.isNetworkError)
{
Util.LogError("url::" + url + " HttpGet Error: " + request.error);
if (errorAction != null)
errorAction();
if (errorLuaFunc != null)
{
errorLuaFunc.Call(request.responseCode, request.error);
}
yield break;
}
else
{
//var result = Encoding.UTF8.GetString(getData.bytes);
var result = request.downloadHandler.text;
if (callback != null)
callback.Call(result);
if (sharpFunc != null)
sharpFunc(result);
}
}
public void SendHttpPost_Raw_Lua(string url, string data, LuaFunction callback, LuaFunction errorLuaFunc)
{
StartCoroutine(HttpPost_Co(url, data, callback, null, null, errorLuaFunc));
}
public void SendHttpPost_Json_Lua(string url, string data, LuaFunction callback, LuaFunction errorLuaFunc)
{
//Debug.LogError("data==="+data);
var strs = data.Split(';');
Hashtable table = new Hashtable();
for (int i = 0; i < strs.Length; i = i + 2)
{
string v1 = strs[i];
string v3 = strs[i + 1];
//long v2 = 0;
//if (strs[i].Equals("total_amount") || strs[i].Equals("trade_time") || strs[i].Equals("valid_time"))
//{
// long.TryParse(v3, out v2);
// Debug.LogError("v2=="+v2);
// table.Add(v1, v2);
//}
//else
//{
// table.Add(v1, strs[i + 1]);
//}
table.Add(v1, v3);
}
var jsonData = MiniJSON.jsonEncode(table);
Debug.LogError("url " + jsonData);
// StartCoroutine(HttpPost_Co(url, jsonData, callback, null, null, errorLuaFunc));
StartCoroutine(PostData(url, jsonData, callback, null, null, errorLuaFunc));
}
public IEnumerator PostData(string url, string data, LuaFunction callback, Action<string> sharpFunc, Action errorAction, LuaFunction errorLuaFunc)
{
//UnityWebRequest _request;
//byte[] databyte = Encoding.UTF8.GetBytes(data);
//_request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
//_request.uploadHandler = new UploadHandlerRaw(databyte);
//_request.downloadHandler = new DownloadHandlerBuffer();
//_request.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
//yield return _request.SendWebRequest();
//Debug.Log(_request.responseCode);
//if (_request.isHttpError || _request.isNetworkError)
//{
// Debug.LogError(_request.error);
//}
//else
//{
// Debug.Log(_request.downloadHandler.text);
//if (callback != null)
//{
// callback.Call(_request.downloadHandler.text);
//}
//}
UnityWebRequest www = UnityWebRequest.Post(url, data);
www.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
byte[] bodyRaw = Encoding.UTF8.GetBytes(data);
www.uploadHandler = new UploadHandlerRaw(bodyRaw);
yield return www.SendWebRequest();
Debug.LogError("111111111111");
if (www.isNetworkError)
{
Debug.LogError(www.error);
}
Debug.LogError(www.downloadHandler.text);
if (callback != null)
{
callback.Call(www.downloadHandler.text);
}
}
public void SendHttpPost_Raw_CSharp(string url, string data, Action<string> sharpFunc, Action errorAction)
{
StartCoroutine(HttpPost_Co(url, data, null, sharpFunc, errorAction, null));
}
public void SendHttpPost_Json_CSharp(string url, string data, Action<string> sharpFunc, Action errorAction)
{
var strs = data.Split(';');
Hashtable table = new Hashtable();
for (int i = 0; i < strs.Length; i = i + 2)
{
table.Add(strs[i], strs[i + 1]);
}
var jsonData = MiniJSON.jsonEncode(table);
StartCoroutine(HttpPost_Co(url, jsonData, null, sharpFunc, errorAction, null));
}
public IEnumerator HttpPost_Co(string url, string data, LuaFunction callback, Action<string> sharpFunc, Action errorAction, LuaFunction errorLuaFunc)
{
float duration = 0;
if(string.IsNullOrEmpty(url))
{
if (errorAction!=null)
{
errorAction();
}
if (errorLuaFunc!=null)
{
errorLuaFunc.Call();
}
yield break;
}
Dictionary<string, string> header = new Dictionary<string, string>();
header.Add("Content-Type", "application/json");
header.Add("charset", "utf-8");
Debug.LogError("data=="+data);
Debug.LogError("utf8=="+Encoding.UTF8.GetBytes(data));
byte[] b=Encoding.UTF8.GetBytes(data);
for (int i=0;i<b.Length;i++){
Debug.LogError(b[i]);
}
WWW postData = new WWW(url, Encoding.UTF8.GetBytes(data), header);
while (!postData.isDone)
{
yield return new WaitForEndOfFrame();
duration += Time.deltaTime;
if(duration>=AppConst.HttpTimeout)
{
XDebug.Log.l(url + " HttpPostError: HttpTimeout:"+ AppConst.HttpTimeout+" "+ data);
if (errorAction != null)
errorAction();
if (errorLuaFunc != null)
errorLuaFunc.Call();
postData.Dispose();
yield break;
}
}
var result = Encoding.UTF8.GetString(postData.bytes);
Debug.LogWarning(url+" Result: " + result);
if (postData.error != null)
{
XDebug.Log.l("HttpPostError: " + postData.error);
if (errorAction != null)
errorAction();
if (errorLuaFunc != null)
errorLuaFunc.Call();
yield break;
}
if (callback != null)
callback.Call(result);
if (sharpFunc != null)
sharpFunc(result);
}
}
}