first commit

This commit is contained in:
2022-07-08 09:14:55 +08:00
commit 4d6bd72555
1123 changed files with 456307 additions and 0 deletions

View File

@@ -0,0 +1,369 @@
#if UNITY_WEBGL || WEBSOCKET || ((UNITY_XBOXONE || UNITY_GAMECORE) && UNITY_EDITOR)
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SocketWebTcp.cs" company="Exit Games GmbH">
// Copyright (c) Exit Games GmbH. All rights reserved.
// </copyright>
// <summary>
// Internal class to encapsulate the network i/o functionality for the realtime library.
// </summary>
// <author>developer@exitgames.com</author>
// --------------------------------------------------------------------------------------------------------------------
namespace ExitGames.Client.Photon
{
using System;
using System.Collections;
using UnityEngine;
using SupportClassPun = ExitGames.Client.Photon.SupportClass;
#if !(UNITY_WEBGL || NETFX_CORE)
using System.Net;
using System.Net.Sockets;
using System.Threading;
#endif
/// <summary>
/// Yield Instruction to Wait for real seconds. Very important to keep connection working if Time.TimeScale is altered, we still want accurate network events
/// </summary>
public sealed class WaitForRealSeconds : CustomYieldInstruction
{
private readonly float _endTime;
public override bool keepWaiting
{
get { return this._endTime > Time.realtimeSinceStartup; }
}
public WaitForRealSeconds(float seconds)
{
this._endTime = Time.realtimeSinceStartup + seconds;
}
}
/// <summary>
/// Internal class to encapsulate the network i/o functionality for the realtime libary.
/// </summary>
public class SocketWebTcp : IPhotonSocket, IDisposable
{
private WebSocket sock;
private readonly object syncer = new object();
public SocketWebTcp(PeerBase npeer) : base(npeer)
{
this.ServerAddress = npeer.ServerAddress;
if (this.ReportDebugOfLevel(DebugLevel.INFO))
{
this.Listener.DebugReturn(DebugLevel.INFO, "new SocketWebTcp() for Unity. Server: " + this.ServerAddress);
}
//this.Protocol = ConnectionProtocol.WebSocket;
this.PollReceive = false;
}
public void Dispose()
{
this.State = PhotonSocketState.Disconnecting;
if (this.sock != null)
{
try
{
if (this.sock.Connected) this.sock.Close();
}
catch (Exception ex)
{
this.EnqueueDebugReturn(DebugLevel.INFO, "Exception in Dispose(): " + ex);
}
}
this.sock = null;
this.State = PhotonSocketState.Disconnected;
}
GameObject websocketConnectionObject;
public override bool Connect()
{
//bool baseOk = base.Connect();
//if (!baseOk)
//{
// return false;
//}
this.State = PhotonSocketState.Connecting;
if (this.websocketConnectionObject != null)
{
UnityEngine.Object.Destroy(this.websocketConnectionObject);
}
this.websocketConnectionObject = new GameObject("websocketConnectionObject");
MonoBehaviour mb = this.websocketConnectionObject.AddComponent<MonoBehaviourExt>();
this.websocketConnectionObject.hideFlags = HideFlags.HideInHierarchy;
UnityEngine.Object.DontDestroyOnLoad(this.websocketConnectionObject);
#if UNITY_WEBGL || NETFX_CORE
this.sock = new WebSocket(new Uri(this.ConnectAddress), this.SerializationProtocol);
this.sock.Connect();
mb.StartCoroutine(this.ReceiveLoop());
#else
mb.StartCoroutine(this.DetectIpVersionAndConnect(mb));
#endif
return true;
}
#if !(UNITY_WEBGL || NETFX_CORE)
private bool ipVersionDetectDone;
private IEnumerator DetectIpVersionAndConnect(MonoBehaviour mb)
{
Uri uri = null;
try
{
uri = new Uri(this.ConnectAddress);
}
catch (Exception ex)
{
if (this.ReportDebugOfLevel(DebugLevel.ERROR))
{
this.Listener.DebugReturn(DebugLevel.ERROR, "Failed to create a URI from ConnectAddress (" + ConnectAddress + "). Exception: " + ex);
}
}
if (uri != null && uri.HostNameType == UriHostNameType.Dns)
{
ipVersionDetectDone = false;
ThreadPool.QueueUserWorkItem(this.DetectIpVersion, uri.Host);
while (!this.ipVersionDetectDone)
{
yield return new WaitForRealSeconds(0.1f);
}
}
if (this.AddressResolvedAsIpv6)
{
this.ConnectAddress += "&IPv6";
}
if (this.ReportDebugOfLevel(DebugLevel.INFO))
{
this.Listener.DebugReturn(DebugLevel.INFO, "DetectIpVersionAndConnect() AddressResolvedAsIpv6: " + this.AddressResolvedAsIpv6 + " ConnectAddress: " + ConnectAddress);
}
this.sock = new WebSocket(new Uri(this.ConnectAddress), this.SerializationProtocol);
this.sock.Connect();
mb.StartCoroutine(this.ReceiveLoop());
}
// state has to be the hostname string
private void DetectIpVersion(object state)
{
string host = state as string;
IPAddress[] ipAddresses;
try
{
ipAddresses = Dns.GetHostAddresses(host);
foreach (IPAddress ipAddress in ipAddresses)
{
if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
{
this.AddressResolvedAsIpv6 = true;
break;
}
}
}
catch (Exception ex)
{
this.Listener.DebugReturn(DebugLevel.INFO, "DetectIpVersionAndConnect (uri: " + host + "= thread failed: " + ex);
}
this.ipVersionDetectDone = true;
}
#endif
public override bool Disconnect()
{
if (this.ReportDebugOfLevel(DebugLevel.INFO))
{
this.Listener.DebugReturn(DebugLevel.INFO, "SocketWebTcp.Disconnect()");
}
this.State = PhotonSocketState.Disconnecting;
lock (this.syncer)
{
if (this.sock != null)
{
try
{
this.sock.Close();
}
catch (Exception ex)
{
this.Listener.DebugReturn(DebugLevel.ERROR, "Exception in Disconnect(): " + ex);
}
this.sock = null;
}
}
if (this.websocketConnectionObject != null)
{
UnityEngine.Object.Destroy(this.websocketConnectionObject);
}
this.State = PhotonSocketState.Disconnected;
return true;
}
/// <summary>
/// used by TPeer*
/// </summary>
public override PhotonSocketError Send(byte[] data, int length)
{
if (this.State != PhotonSocketState.Connected)
{
return PhotonSocketError.Skipped;
}
try
{
if (data.Length > length)
{
byte[] trimmedData = new byte[length];
Buffer.BlockCopy(data, 0, trimmedData, 0, length);
data = trimmedData;
}
if (this.ReportDebugOfLevel(DebugLevel.ALL))
{
this.Listener.DebugReturn(DebugLevel.ALL, "Sending: " + SupportClassPun.ByteArrayToString(data));
}
if (this.sock != null)
{
this.sock.Send(data);
}
}
catch (Exception e)
{
this.Listener.DebugReturn(DebugLevel.ERROR, "Cannot send to: " + this.ServerAddress + ". " + e.Message);
this.HandleException(StatusCode.Exception);
return PhotonSocketError.Exception;
}
return PhotonSocketError.Success;
}
public override PhotonSocketError Receive(out byte[] data)
{
data = null;
return PhotonSocketError.NoData;
}
internal const int ALL_HEADER_BYTES = 9;
internal const int TCP_HEADER_BYTES = 7;
internal const int MSG_HEADER_BYTES = 2;
public IEnumerator ReceiveLoop()
{
//this.Listener.DebugReturn(DebugLevel.INFO, "ReceiveLoop()");
if (this.sock != null)
{
while (this.sock != null && !this.sock.Connected && this.sock.Error == null)
{
yield return new WaitForRealSeconds(0.1f);
}
if (this.sock != null)
{
if (this.sock.Error != null)
{
this.Listener.DebugReturn(DebugLevel.ERROR, "Exiting receive thread. Server: " + this.ServerAddress + ":" + this.ServerPort + " Error: " + this.sock.Error);
this.HandleException(StatusCode.ExceptionOnConnect);
}
else
{
// connected
if (this.ReportDebugOfLevel(DebugLevel.ALL))
{
this.Listener.DebugReturn(DebugLevel.ALL, "Receiving by websocket. this.State: " + this.State);
}
this.State = PhotonSocketState.Connected;
this.peerBase.OnConnect();
while (this.State == PhotonSocketState.Connected)
{
if (this.sock != null)
{
if (this.sock.Error != null)
{
this.Listener.DebugReturn(DebugLevel.ERROR, "Exiting receive thread (inside loop). Server: " + this.ServerAddress + ":" + this.ServerPort + " Error: " + this.sock.Error);
this.HandleException(StatusCode.ExceptionOnReceive);
break;
}
else
{
byte[] inBuff = this.sock.Recv();
if (inBuff == null || inBuff.Length == 0)
{
// nothing received. wait a bit, try again
yield return new WaitForRealSeconds(0.02f);
continue;
}
if (this.ReportDebugOfLevel(DebugLevel.ALL))
{
this.Listener.DebugReturn(DebugLevel.ALL, "TCP << " + inBuff.Length + " = " + SupportClassPun.ByteArrayToString(inBuff));
}
if (inBuff.Length > 0)
{
try
{
this.HandleReceivedDatagram(inBuff, inBuff.Length, false);
}
catch (Exception e)
{
if (this.State != PhotonSocketState.Disconnecting && this.State != PhotonSocketState.Disconnected)
{
if (this.ReportDebugOfLevel(DebugLevel.ERROR))
{
this.EnqueueDebugReturn(DebugLevel.ERROR, "Receive issue. State: " + this.State + ". Server: '" + this.ServerAddress + "' Exception: " + e);
}
this.HandleException(StatusCode.ExceptionOnReceive);
}
}
}
}
}
}
}
}
}
this.Disconnect();
}
private class MonoBehaviourExt : MonoBehaviour { }
}
}
#endif

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ac953d6a57a9ea94e96ec689598995d5
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

View File

@@ -0,0 +1,188 @@
#if UNITY_WEBGL || WEBSOCKET || ((UNITY_XBOXONE || UNITY_GAMECORE) && UNITY_EDITOR)
// --------------------------------------------------------------------------------------------------------------------
// <summary>
// Provided originally by Unity to cover WebSocket support in WebGL and the Editor. Modified by Exit Games GmbH.
// </summary>
// <author>developer@exitgames.com</author>
// --------------------------------------------------------------------------------------------------------------------
namespace ExitGames.Client.Photon
{
using System;
using System.Text;
#if UNITY_WEBGL && !UNITY_EDITOR
using System.Runtime.InteropServices;
#else
using WebSocketSharp;
using System.Collections.Generic;
using System.Security.Authentication;
#endif
public class WebSocket
{
private Uri mUrl;
/// <summary>Photon uses this to agree on a serialization protocol. Either: GpBinaryV16 or GpBinaryV18. Based on enum SerializationProtocol.</summary>
private string protocols = "GpBinaryV16";
public WebSocket(Uri url, string serialization = null)
{
this.mUrl = url;
if (serialization != null)
{
this.protocols = serialization;
}
string protocol = mUrl.Scheme;
if (!protocol.Equals("ws") && !protocol.Equals("wss"))
throw new ArgumentException("Unsupported protocol: " + protocol);
}
public void SendString(string str)
{
Send(Encoding.UTF8.GetBytes (str));
}
public string RecvString()
{
byte[] retval = Recv();
if (retval == null)
return null;
return Encoding.UTF8.GetString (retval);
}
#if UNITY_WEBGL && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern int SocketCreate (string url, string protocols);
[DllImport("__Internal")]
private static extern int SocketState (int socketInstance);
[DllImport("__Internal")]
private static extern void SocketSend (int socketInstance, byte[] ptr, int length);
[DllImport("__Internal")]
private static extern void SocketRecv (int socketInstance, byte[] ptr, int length);
[DllImport("__Internal")]
private static extern int SocketRecvLength (int socketInstance);
[DllImport("__Internal")]
private static extern void SocketClose (int socketInstance);
[DllImport("__Internal")]
private static extern int SocketError (int socketInstance, byte[] ptr, int length);
int m_NativeRef = 0;
public void Send(byte[] buffer)
{
SocketSend (m_NativeRef, buffer, buffer.Length);
}
public byte[] Recv()
{
int length = SocketRecvLength (m_NativeRef);
if (length == 0)
return null;
byte[] buffer = new byte[length];
SocketRecv (m_NativeRef, buffer, length);
return buffer;
}
public void Connect()
{
m_NativeRef = SocketCreate (mUrl.ToString(), this.protocols);
//while (SocketState(m_NativeRef) == 0)
// yield return 0;
}
public void Close()
{
SocketClose(m_NativeRef);
}
public bool Connected
{
get { return SocketState(m_NativeRef) != 0; }
}
public string Error
{
get {
const int bufsize = 1024;
byte[] buffer = new byte[bufsize];
int result = SocketError (m_NativeRef, buffer, bufsize);
if (result == 0)
return null;
return Encoding.UTF8.GetString (buffer);
}
}
#else
WebSocketSharp.WebSocket m_Socket;
Queue<byte[]> m_Messages = new Queue<byte[]>();
bool m_IsConnected = false;
string m_Error = null;
public void Connect()
{
m_Socket = new WebSocketSharp.WebSocket(mUrl.ToString(), new string[] { this.protocols });
m_Socket.SslConfiguration.EnabledSslProtocols = m_Socket.SslConfiguration.EnabledSslProtocols | (SslProtocols)(3072| 768);
m_Socket.OnMessage += (sender, e) => m_Messages.Enqueue(e.RawData);
m_Socket.OnOpen += (sender, e) => m_IsConnected = true;
//this.m_Socket.Log.Level = LogLevel.Debug;
//this.m_Socket.Log.Output += Output;
this.m_Socket.OnClose += SocketOnClose;
m_Socket.OnError += (sender, e) => m_Error = e.Message + (e.Exception == null ? "" : " / " + e.Exception);
m_Socket.ConnectAsync();
}
private void SocketOnClose(object sender, CloseEventArgs e)
{
//UnityEngine.Debug.Log(e.Code.ToString());
// this code is used for cases when the socket failed to get created (specifically used to detect "blocked by Windows firewall")
// for some reason this situation is not calling OnError
if (e.Code == 1006)
{
this.m_Error = e.Reason;
this.m_IsConnected = false;
}
}
public bool Connected { get { return m_IsConnected; } }// added by TS
public void Send(byte[] buffer)
{
m_Socket.Send(buffer);
}
public byte[] Recv()
{
if (m_Messages.Count == 0)
return null;
return m_Messages.Dequeue();
}
public void Close()
{
m_Socket.Close();
}
public string Error
{
get
{
return m_Error;
}
}
#endif
}
}
#endif

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b1bad04f7805f764dba77f0d4518e0f0
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

View File

@@ -0,0 +1,117 @@
var LibraryWebSockets = {
$webSocketInstances: [],
SocketCreate: function(url, protocols)
{
var str = UTF8ToString(url);
var prot = UTF8ToString(protocols);
var socket = {
socket: new WebSocket(str, [prot]),
buffer: new Uint8Array(0),
error: null,
messages: []
}
socket.socket.binaryType = 'arraybuffer';
socket.socket.onmessage = function (e) {
// if (e.data instanceof Blob)
// {
// var reader = new FileReader();
// reader.addEventListener("loadend", function() {
// var array = new Uint8Array(reader.result);
// socket.messages.push(array);
// });
// reader.readAsArrayBuffer(e.data);
// }
if (e.data instanceof ArrayBuffer)
{
var array = new Uint8Array(e.data);
socket.messages.push(array);
}
};
socket.socket.onclose = function (e) {
if (e.code != 1000)
{
if (e.reason != null && e.reason.length > 0)
socket.error = e.reason;
else
{
switch (e.code)
{
case 1001:
socket.error = "Endpoint going away.";
break;
case 1002:
socket.error = "Protocol error.";
break;
case 1003:
socket.error = "Unsupported message.";
break;
case 1005:
socket.error = "No status.";
break;
case 1006:
socket.error = "Abnormal disconnection.";
break;
case 1009:
socket.error = "Data frame too large.";
break;
default:
socket.error = "Error "+e.code;
}
}
}
}
var instance = webSocketInstances.push(socket) - 1;
return instance;
},
SocketState: function (socketInstance)
{
var socket = webSocketInstances[socketInstance];
return socket.socket.readyState;
},
SocketError: function (socketInstance, ptr, bufsize)
{
var socket = webSocketInstances[socketInstance];
if (socket.error == null)
return 0;
var str = socket.error.slice(0, Math.max(0, bufsize - 1));
writeStringToMemory(str, ptr, false);
return 1;
},
SocketSend: function (socketInstance, ptr, length)
{
var socket = webSocketInstances[socketInstance];
socket.socket.send (HEAPU8.buffer.slice(ptr, ptr+length));
},
SocketRecvLength: function(socketInstance)
{
var socket = webSocketInstances[socketInstance];
if (socket.messages.length == 0)
return 0;
return socket.messages[0].length;
},
SocketRecv: function (socketInstance, ptr, length)
{
var socket = webSocketInstances[socketInstance];
if (socket.messages.length == 0)
return 0;
if (socket.messages[0].length > length)
return 0;
HEAPU8.set(socket.messages[0], ptr);
socket.messages = socket.messages.slice(1);
},
SocketClose: function (socketInstance)
{
var socket = webSocketInstances[socketInstance];
socket.socket.close();
}
};
autoAddDeps(LibraryWebSockets, '$webSocketInstances');
mergeInto(LibraryManager.library, LibraryWebSockets);

View File

@@ -0,0 +1,21 @@
fileFormatVersion: 2
guid: 04bb5f307f2e48b4fbaa6da865fd4091
PluginImporter:
serializedVersion: 1
iconMap: {}
executionOrder: {}
isPreloaded: 0
platformData:
Any:
enabled: 0
settings: {}
Editor:
enabled: 0
settings:
DefaultValueInitialized: true
WebGL:
enabled: 1
settings: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,3 @@
websocket-sharp.dll built from https://github.com/sta/websocket-sharp.git, commit 869dfb09778de51081b0ae64bd2c3217cffe0699 on Aug 24, 2016.
websocket-sharp is provided under The MIT License as mentioned here: https://github.com/sta/websocket-sharp#license

View File

@@ -0,0 +1,4 @@
fileFormatVersion: 2
guid: 3e262c2b04eaa8440987b50a91e86674
DefaultImporter:
userData:

Binary file not shown.

View File

@@ -0,0 +1,137 @@
fileFormatVersion: 2
guid: 748eb70bc0d7515498ef73fed155520a
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
isPreloaded: 0
isOverridable: 0
platformData:
- first:
'': Any
second:
enabled: 0
settings:
Exclude Android: 0
Exclude Editor: 0
Exclude Linux: 0
Exclude Linux64: 0
Exclude LinuxUniversal: 0
Exclude OSXUniversal: 0
Exclude WebGL: 1
Exclude Win: 0
Exclude Win64: 0
Exclude WindowsStoreApps: 1
- first:
'': OSXIntel
second:
enabled: 0
settings:
CPU: AnyCPU
- first:
'': OSXIntel64
second:
enabled: 0
settings:
CPU: AnyCPU
- first:
'': WP8
second:
enabled: 0
settings:
CPU: AnyCPU
DontProcess: False
PlaceholderPath:
- first:
Android: Android
second:
enabled: 1
settings:
CPU: ARMv7
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 1
settings:
CPU: AnyCPU
DefaultValueInitialized: true
OS: AnyOS
- first:
Facebook: Win
second:
enabled: 0
settings:
CPU: AnyCPU
- first:
Facebook: Win64
second:
enabled: 0
settings:
CPU: AnyCPU
- first:
Standalone: Linux
second:
enabled: 1
settings:
CPU: x86
- first:
Standalone: Linux64
second:
enabled: 1
settings:
CPU: x86_64
- first:
Standalone: LinuxUniversal
second:
enabled: 1
settings:
CPU: AnyCPU
- first:
Standalone: OSXUniversal
second:
enabled: 1
settings:
CPU: AnyCPU
- first:
Standalone: Win
second:
enabled: 1
settings:
CPU: AnyCPU
- first:
Standalone: Win64
second:
enabled: 1
settings:
CPU: AnyCPU
- first:
Windows Store Apps: WindowsStoreApps
second:
enabled: 0
settings:
CPU: AnyCPU
DontProcess: False
PlaceholderPath:
SDK: AnySDK
ScriptingBackend: AnyScriptingBackend
- first:
XboxOne: XboxOne
second:
enabled: 0
settings: {}
- first:
iPhone: iOS
second:
enabled: 0
settings:
CompileFlags:
FrameworkDependencies:
userData:
assetBundleName:
assetBundleVariant: