Пример #1
0
        protected NetworkData GetNetworkData(List <PCInfo> pcList)
        {
            var currentPC  = default(PCInfo);
            var currentIPs = Tool.GetLocalIPAddress();

            foreach (var ip in currentIPs)
            {
                var pcData = pcList.FindAll(pc => pc.ipAddress == ip);
                if (pcData != null && pcData.Count > 0)
                {
                    var first = pcData[0];
                    if (pcData.Count > 1)
                    {
                        Debug.LogWarningFormat("Multiple pc ip found, using the first one({0}) for networking", first.name);
                    }

                    currentPC = first;
                    break;
                }
            }
            var serverData = pcList.FindAll(pc => pc.role == PCInfo.Role.Server);

            if (currentPC == null)
            {
                LogTool.Log("Current pc not found, use current ip " + currentIPs[0], LogLevel.Warning);
                currentPC           = new PCInfo();
                currentPC.ipAddress = currentIPs[0];
                currentPC.role      = PCInfo.Role.None;
            }
            if (serverData == null)
            {
                LogTool.Log("serverData not found, add a default one", LogLevel.Warning);
                serverData = new List <PCInfo>()
                {
                    new PCInfo()
                    {
                        role = PCInfo.Role.Server
                    }
                };
            }
            LogTool.LogFormat("setup current pc ip {1} as {0}", LogLevel.Info, LogChannel.Debug | LogChannel.Network, currentPC.role.ToString(), currentPC.ipAddress);

            var clientData = pcList.FindAll(pc => pc.role == PCInfo.Role.Client && currentIPs.Contains(pc.ipAddress) == false);
            var devData    = pcList.FindAll(pc => pc.role == PCInfo.Role.Development);

            var data = new NetworkData()
            {
                current = currentPC,
                servers = serverData,
                devPCs  = devData,
                client  = new List <PCInfo>(clientData)
            };

            return(data);
        }
Пример #2
0
        protected void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                var stateFrom = ar.AsyncState as ReceiveState;
                Assert.IsTrue(stateFrom == this.receiveState);

                EndPoint epFrom        = stateFrom.remote.endPoint;
                int      bytesReceived = stateFrom.remote.socket.EndReceiveFrom(ar, ref epFrom);

                var ipFrom = epFrom as IPEndPoint;
                stateFrom.remote.endPoint = ipFrom;

                if (bytesReceived > 0)
                {
                    if (DebugLog)
                    {
                        Debug.LogFormat("RECV: {0}: {1}", epFrom.ToString(), bytesReceived);
                    }
                    T data = this.OnDeserialize(stateFrom.buffer, bytesReceived);
                    this.OnMessage(stateFrom.remote, data);
                }

                if (this.connections[Connection.Incoming].Exists(c => c.endPoint.Address.Equals(ipFrom.Address)) == false)
                {
                    this.connections[Connection.Incoming].Add(SocketData.Make(ipFrom));
                    if (DebugLog)
                    {
                        LogTool.LogFormat("Add in connection: {0}", LogLevel.Verbose, LogChannel.Network, ipFrom.ToString());
                    }
                }

                epFrom = this.receiveState.remote.endPoint;
                stateFrom.remote.socket.BeginReceiveFrom(this.receiveState.buffer, 0, this.receiveState.buffer.Length, SocketFlags.None, ref epFrom, this.ReceiveCallback, this.receiveState);
            }
            catch (Exception e)
            {
                if (!(e is ObjectDisposedException))
                {
                    LogTool.Log(e.ToString());
                    var se = e as SocketException;
                    if (se != null)
                    {
                        LogTool.Log(se.ErrorCode.ToString());
                        LogTool.Log(se.Message);
                    }
                }
            }
        }
Пример #3
0
 public override void SetToGPU(string kernal = null)
 {
     if (this.IsShaderValid(kernal) && this.data != null)
     {
         this.Set(kernal);
     }
     else
     {
         if (this.data == null)
         {
             LogTool.Log(this.VariableName + " Data is null", LogLevel.Warning);
         }
         else
         {
             LogTool.LogFormat("Please bind {0} with compute shader first/or supply kernal name", LogLevel.Warning, LogChannel.Debug, this.VariableName);
         }
     }
 }
Пример #4
0
        protected void SendCallback(IAsyncResult ar)
        {
            try
            {
                if (ar.IsCompleted)
                {
                    var socketData = ar.AsyncState as SocketData;

                    int bytes = socketData.socket.EndSendTo(ar);

                    if (this.connections[Connection.Outcoming].Exists(c => c.endPoint.Address.Equals(socketData.endPoint.Address)) == false &&
                        socketData.endPoint.Address != IPAddress.Broadcast)
                    {
                        this.connections[Connection.Outcoming].Add(socketData);
                        if (DebugLog)
                        {
                            LogTool.LogFormat("Add out connection: {0}", LogLevel.Warning, LogChannel.Network, socketData.endPoint.ToString());
                        }
                    }

                    if (DebugLog)
                    {
                        LogTool.LogFormat("SEND: {0} bytes To {1}", LogLevel.Dev, LogChannel.Network, bytes, socketData.endPoint.ToString());
                    }

                    if (DebugLog)
                    {
                        foreach (var c in this.connections)
                        {
                            LogTool.Log("connections count " + c.Key.ToString() + " " + c.Value.Count);
                        }
                    }
                }
            }
            catch (SocketException e)
            {
                LogTool.Log(e.ToString());
                LogTool.Log((e as SocketException).ErrorCode.ToString());
                LogTool.Log((e as SocketException).Message);
            }
        }