private void ReceiveCallback(byte[] data) { //Debug.Log(Encoding.UTF8.GetString(data)); NetworkEvent e = ReceiveEvent(data); Debug.Log("Parse event " + e.GetType().ToString()); if (e != null) { handlers[e.GetType()].Handle(this, e); } }
private void ServerThread() { Debug.Log("Started server"); while (_serverRunning) { try { IPEndPoint clientIP = null; Debug.Log("Wait for packet"); var data = _server.Receive(ref clientIP); Debug.Log("Received packet from " + clientIP.Address); //Debug.Log(Encoding.UTF8.GetString(data)); //if (!Encoding.UTF8.GetString(data).Equals("init")) // continue; Debug.Log(clientIP.Port); if (!clientList.Exists(c => Equals(c.Ip?.Address, clientIP.Address) && Equals(c.Ip?.Port, clientIP.Port))) { if (_count >= 4) { Debug.LogError("More than 4"); } Debug.Log("Create player"); Debug.Log(clientList.Select(er => er.NetworkID.ToString()).Aggregate((a, b) => a + ", " + b)); //NetworkPlayer new_player = Instantiate(playerPrefab).GetComponent<NetworkPlayer>(); //clientList.Add(new_player); NetworkData new_player = clientList[_count]; new_player.Ip = clientIP; new_player.NetworkID = _count; _count++; } NetworkData player = clientList.Find(c => Equals(c.Ip?.Address, clientIP.Address) && c.Ip?.Port == clientIP.Port); // player.Ip = clientIP; NetworkEvent e = ReceiveEvent(data); Debug.Log("Get event " + e.GetType().ToString()); ServerNetworkEventHandler handler = handlers[e.GetType()]; handler.Handle(this, e, player); //new Thread(() => { ClientProcessor(clientIP); }).Start(); } catch (Exception err) { Debug.Log(err.ToString()); } } }
/// <summary> /// Calls the corresponding method for each type of received message /// </summary> /// <param name="packet"> The packet that was received</param> /// <param name="handler"> The client handler that called this method</param> private void HandleMessage(Packet packet, ClientHandler handler) { //create a NetworkEvent from the received packet NetworkEvent networkEvent = NetworkEvent.Deserialize(packet.GetDataBytes()); Type type = networkEvent.GetType(); if (type == typeof(HeartBeat)) { RespondToHeartBeat(handler); } else if (type == typeof(InputUpdate)) { RespondToInputUpdate(networkEvent); } else if (type == typeof(ConnectPlayer)) { RespondToConnectPlayer(handler); } else if (type == typeof(DisconnectPlayer)) { RespondToDisconnectPlayer(networkEvent, handler); } else { RespondToUnknownMessage(handler); } }
// EventTypeとコンストラクタで渡されたイベントの型が一致していればfuncを実行 public override bool Dispatch <EventType>(EventFnction <EventType> func) { if (m_Event.GetType() == typeof(EventType)) { m_Event.m_Handled = func(m_Event as EventType); return(true); } return(false); }
// new data from network.. this effectively means we have a fork.. // can we implement merge rules? // Temporarily: only appending with new version nr at end. // this will very likely lead to corrupted stream if any real // concurrent usage of the drive occurs. // TODO: Needs A LOT of work. async Task <bool> TryMergeAsync(NetworkEvent evt, WALContent walContent) { var newData = await _service .LoadAsync(evt.SequenceNr - 1) .ToListAsync(); switch (evt) { case NetworkFileContentSet e when IsUnresolvableConflict(e, newData): return(false); case NetworkFileItemCreated e: break; case NetworkFileContentCleared e: break; case NetworkItemCopied e: break; case NetworkItemMoved e: break; case NetworkDirectoryItemCreated e: break; case NetworkItemRemoved e: break; case NetworkItemRenamed e: break; case null: throw new ArgumentNullException(nameof(evt)); default: throw new NotImplementedException(evt.GetType().Name); } // naively just try apply the remote changes var caughtUp = await _materializer.Materialize(newData.ToAsyncEnumerable()); if (!caughtUp) { return(false); } walContent.SequenceNr = newData.Max(c => c.SequenceNr) + 1; return(await Upload(walContent)); }
public void FireEventReceived(NetworkEvent networkEvent) { if (!IsPushEnabled) { return; } try { Trace($"{networkEvent.GetType().Name} received: {networkEvent}"); EventReceived?.Invoke(networkEvent); } catch (Exception e) { Trace(e); } }