示例#1
0
        public void Send(byte[] data, int channelId, string proxyId, int connectionId)
        {
            var packet = WakeNet.Serialize(new Packet
            {
                Data    = data,
                ProxyId = proxyId,
                Server  = true
            });

            WakeNet.Log(string.Format("WakeServer:{0}:Send() - {1}b", Socket, packet.Length));
            byte error = 0;

            if (connectionId >= 0)
            {
                NetworkTransport.Send(Socket, connectionId, channelId, packet, packet.Length, out error);
            }
            else
            {
                foreach (var client in _clients.Keys)
                {
                    NetworkTransport.Send(Socket, client, channelId, packet, packet.Length, out error);
                }
            }
            if (error > 0)
            {
                Error = error;
            }
        }
示例#2
0
        public void Broadcast(string broadcastMessage, int serverPort)
        {
            if (IsBroadcasting)
            {
                return;
            }

            Socket = WakeNet.AddSocket(1);
            WakeNet.Log(string.Format("WakeDiscovery:{0}:Broadcast()", Socket));
            WakeNet.RegisterSocket(Socket);

            var sendInfo = new GameResult
            {
                DeviceId = SystemInfo.deviceUniqueIdentifier,
                Message  = broadcastMessage,
                Port     = serverPort
            };

            var  data = Encoding.UTF8.GetBytes(JsonUtility.ToJson(sendInfo, true));
            byte error;

            NetworkTransport.StartBroadcastDiscovery(Socket, _port, _key, _version, _subversion, data, data.Length, 1000, out error);
            if (error > 0)
            {
                Error = error;
            }
            else
            {
                IsBroadcasting = true;
            }
        }
示例#3
0
 public void DisconnectAllClients()
 {
     WakeNet.Log(string.Format("WakeServer:{0}:DisconnectAllClients()", Socket));
     foreach (var clientId in _clients.Keys.ToList())
     {
         DisconnectClient(clientId);
     }
 }
示例#4
0
        public WakeDiscovery(int port, int key, int version, int subversion, float interval = 1f)
        {
            WakeNet.Log("WakeDiscovery::Ctor()");

            _port       = port;
            _key        = key;
            _version    = version;
            _subversion = subversion;
            _interval   = interval;
        }
示例#5
0
        internal WakeClient()
        {
            WakeNet.Log("WakeClient::Ctor()");

            _proxySenders   = new Dictionary <string, IProxySender>();
            _proxyReceivers = new Dictionary <string, IProxyReceiver>();

            // require single connection
            Socket = WakeNet.AddSocket(1);
        }
示例#6
0
        internal WakeClient(int socket, int connectionId)
        {
            WakeNet.Log("WakeClient::Ctor()");

            _proxySenders   = new Dictionary <string, IProxySender>();
            _proxyReceivers = new Dictionary <string, IProxyReceiver>();

            Socket       = socket;
            ConnectionId = connectionId;
            IsConnected  = true;
        }
示例#7
0
 internal void ProcessOutgoingEvents()
 {
     foreach (var k in _proxySenders.Keys)
     {
         while (_proxySenders[k].SendQueueCount > 0)
         {
             var m = _proxySenders[k].PopMessageFromQueue();
             Send(m, _proxySenders[k].ChannelId, k, _proxySenders[k].Server);
             WakeNet.Log("Proxy[{0}] (Client) - Send : {1}b", NetworkLogLevel.Full, k, m.Length);
         }
     }
 }
示例#8
0
 public void Shutdown()
 {
     WakeNet.Log(string.Format("WakeDiscovery:{0}:Shutdown()", Socket));
     if (IsBroadcasting)
     {
         NetworkTransport.StopBroadcastDiscovery();
         IsBroadcasting = false;
     }
     if (IsSearching)
     {
         IsSearching = false;
     }
 }
示例#9
0
        public void DisconnectClient(int clientId)
        {
            WakeNet.Log(string.Format("WakeServer:{0}:DisconnectClient()", Socket));
            if (!_clients.ContainsKey(clientId))
            {
                WakeNet.Log(WakeError.ClientNotExists);
                return;
            }

            byte error;

            NetworkTransport.Disconnect(Socket, clientId, out error);
            _clients.Remove(clientId);
        }
示例#10
0
        internal WakeServer(int maxConnections, int port, int simMinTimeout = 0, int simMaxTimeout = 0)
        {
            WakeNet.Log("WakeServer::Ctor()");
            _port    = port;
            _clients = new Dictionary <int, WakeClient>();

            _proxySenders   = new Dictionary <string, IProxySender>();
            _proxyReceivers = new Dictionary <string, IProxyReceiver>();

            Socket = WakeNet.AddSocket(maxConnections, simMinTimeout, simMaxTimeout, _port);
            if (Socket >= 0)
            {
                IsConnected = true;
            }
        }
示例#11
0
        internal override void ProcessIncomingEvent(NetworkEventType netEvent, int connectionId, int channelId, byte[] buffer, int dataSize)
        {
            if (netEvent != NetworkEventType.BroadcastEvent)
            {
                return;
            }

            byte error;

            NetworkTransport.GetBroadcastConnectionMessage(Socket, buffer, buffer.Length, out dataSize, out error);
            if (error > 0)
            {
                Error = error;
                return;
            }
            string host;
            int    port;

            NetworkTransport.GetBroadcastConnectionInfo(Socket, out host, out port, out error);
            if (error > 0)
            {
                Error = error;
                return;
            }

            var data = new byte[dataSize];

            Buffer.BlockCopy(buffer, 0, data, 0, dataSize);
            WakeNet.Log(Encoding.UTF8.GetString(data));
            var gameResult = JsonUtility.FromJson <GameResult>(Encoding.UTF8.GetString(data));

            if (!_foundGames.ContainsKey(gameResult.DeviceId))
            {
                _foundGames.Add(gameResult.DeviceId, new Result {
                    Host = host, Port = gameResult.Port, GameResult = gameResult
                });
                _foundGames[gameResult.DeviceId].Routine = WakeNet.InvokeAt(() => { _foundGames.Remove(gameResult.DeviceId); }, Time.unscaledTime + _interval * 2);
            }
            else
            {
                _foundGames[gameResult.DeviceId].Host = host;
                _foundGames[gameResult.DeviceId].Port = gameResult.Port;
                _foundGames[gameResult.DeviceId].GameResult.Message = gameResult.Message;
                WakeNet.StopRoutine(_foundGames[gameResult.DeviceId].Routine);
                _foundGames[gameResult.DeviceId].Routine = WakeNet.InvokeAt(() => { _foundGames.Remove(gameResult.DeviceId); }, Time.unscaledTime + _interval * 2);
            }
        }
示例#12
0
        public void Disconnect()
        {
            WakeNet.Log(string.Format("WakeClient:{0}:Disconnect()", Socket));
            if (!IsConnected)
            {
                WakeNet.Log(WakeError.NotConnected);
                return;
            }

            byte error;

            NetworkTransport.Disconnect(Socket, ConnectionId, out error);
            if (error > 0)
            {
                Error = error;
            }
        }
示例#13
0
        public void Connect(string host, int port)
        {
            byte error;

            ConnectionId = NetworkTransport.Connect(Socket, host, port, 0, out error);
            WakeNet.Log(string.Format("WakeClient:{0}:Connect() - {1}", Socket, ConnectionId));
            if (error > 0)
            {
                Error = error;
            }
            else
            {
                IsConnected = true;
                Host        = host;
                Port        = port;
            }
        }
示例#14
0
        public void Send(byte[] data, int channelId, string proxyId, bool server)
        {
            var packet = WakeNet.Serialize(new Packet
            {
                Data    = data,
                ProxyId = proxyId,
                Server  = server
            });

            WakeNet.Log(string.Format("WakeClient:{0}:Send() - {1}b", Socket, packet.Length));
            byte error;

            NetworkTransport.Send(Socket, ConnectionId, channelId, packet, packet.Length, out error);
            if (error > 0)
            {
                Error = error;
            }
        }
示例#15
0
        internal override void ProcessIncomingEvent(NetworkEventType netEvent, int connectionId, int channelId, byte[] buffer, int dataSize)
        {
            switch (netEvent)
            {
            case NetworkEventType.ConnectEvent:
                if (Connected != null)
                {
                    Connected();
                }
                WakeNet.Log(string.Format("Client[{0}] - connected.", ConnectionId), NetworkLogLevel.Informational);
                break;

            case NetworkEventType.DisconnectEvent:
                if (Disconnected != null)
                {
                    Disconnected();
                }
                WakeNet.Log(string.Format("Client[{0}] - disconnected.", ConnectionId), NetworkLogLevel.Informational);
                break;

            case NetworkEventType.DataEvent:
                WakeNet.Log("Client[{0}] - Packet : {1}b", NetworkLogLevel.Full, ConnectionId, dataSize);
                var packet = WakeNet.Deserialzie <Packet>(buffer, 0, dataSize);
                if (string.IsNullOrEmpty(packet.ProxyId))
                {
                    // handle raw packages
                    if (DataReceived != null)
                    {
                        DataReceived(packet.Data, channelId);
                    }
                }
                else if (_proxyReceivers.ContainsKey(packet.ProxyId))
                {
                    // pass data to proxy, it'll deserialize it to proper type
                    // and fires own event
                    _proxyReceivers[packet.ProxyId].ReceivedInternal(packet.Data, connectionId);
                }
                else
                {
                    WakeNet.Log(string.Format("Unsupported or not registered proxy type : {0}", packet.ProxyId), NetworkLogLevel.Informational);
                }
                break;
            }
        }
示例#16
0
        internal void ProcessOutgoingEvents()
        {
            if (_proxySenders == null)
            {
                return;
            }
            foreach (var k in _proxySenders.Keys)
            {
                while (_proxySenders[k].SendQueueCount > 0)
                {
                    var m = _proxySenders[k].PopMessageFromQueue();
                    Send(m, _proxySenders[k].ChannelId, k, -1);
                    WakeNet.Log("Proxy[{0}] (Server) - Send : {1}b", NetworkLogLevel.Full, k, m.Length);
                }
            }

            foreach (var key in _clients.Keys)
            {
                _clients[key].ProcessOutgoingEvents();
            }
        }
示例#17
0
        public void Search()
        {
            if (IsSearching)
            {
                return;
            }

            Socket = WakeNet.AddSocket(1, _port);
            WakeNet.Log(string.Format("WakeDiscovery:{0}:Search()", Socket));
            WakeNet.RegisterSocket(Socket);

            byte error;

            NetworkTransport.SetBroadcastCredentials(Socket, _key, _version, _subversion, out error);
            if (error > 0)
            {
                Error = error;
            }
            else
            {
                IsSearching = true;
            }
        }
示例#18
0
        internal override void ProcessIncomingEvent(NetworkEventType netEvent, int connectionId, int channelId, byte[] buffer, int dataSize)
        {
            switch (netEvent)
            {
            case NetworkEventType.ConnectEvent:
                if (_clients.ContainsKey(connectionId))
                {
                    WakeNet.Log(WakeError.ClientAlreadyExists);
                    return;
                }

                _clients.Add(connectionId, new WakeClient(Socket, connectionId));
                if (ClientConnected != null)
                {
                    ClientConnected(_clients[connectionId]);
                }
                WakeNet.Log(string.Format("Server[{0}] connected [{1}].", Socket, connectionId), NetworkLogLevel.Informational);
                break;

            case NetworkEventType.DisconnectEvent:
                if (!_clients.ContainsKey(connectionId))
                {
                    WakeNet.Log(WakeError.ClientNotExists);
                    return;
                }

                if (ClientDisconnected != null)
                {
                    ClientDisconnected(_clients[connectionId]);
                }
                _clients.Remove(connectionId);

                WakeNet.Log(string.Format("Server[{0}] disconnected [{1}].", Socket, connectionId), NetworkLogLevel.Informational);
                break;

            case NetworkEventType.DataEvent:
                var packet = WakeNet.Deserialzie <Packet>(buffer, 0, dataSize);
                if (packet.Server)
                {
                    if (string.IsNullOrEmpty(packet.ProxyId))
                    {
                        // handle raw packages
                        if (DataReceived != null)
                        {
                            DataReceived(packet.Data, channelId);
                        }
                    }
                    else if (_proxyReceivers.ContainsKey(packet.ProxyId))
                    {
                        // pass data to proxy, it'll deserialize it to proper type
                        // and fires own event
                        _proxyReceivers[packet.ProxyId].ReceivedInternal(packet.Data, connectionId);
                        WakeNet.Log("Proxy[{0}] (Server) - Received : {1}b", NetworkLogLevel.Full, packet.ProxyId, packet.Data.Length);
                    }
                    else
                    {
                        WakeNet.Log(string.Format("Unsupported or not registered proxy type : {0}", packet.ProxyId));
                    }
                }
                else
                {
                    if (!_clients.ContainsKey(connectionId))
                    {
                        throw new Exception("Data received for disconnected client.");
                    }

                    // pass data to client presentation on server side, it'll handle it by itself
                    WakeNet.Log("Proxy[{0}] (Client) - Received : {1}b", NetworkLogLevel.Full, packet.ProxyId, packet.Data.Length);
                    _clients[connectionId].ProcessIncomingEvent(netEvent, connectionId, channelId, buffer, dataSize);
                }
                break;
            }
        }