private async void OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMethod clientDeliveryMethod) { NList msg = ProtoUtils.Deserialize <NList>(reader.GetBytesWithLength()); UdpSession session = null; if (_Sessions.TryGetValue(peer.Id, out session)) { await session.IncomingMessage(msg); } _NetManager.TriggerUpdate(); reader.Recycle(); }
void ReceivePackage(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod) { NetDataWriter writer = new NetDataWriter(); ushort msgid = reader.GetUShort(); print("receivepackage event: " + msgid); switch (msgid) { case 1: { ColorPacket result = reader.GetBytesWithLength().ToStructure <ColorPacket>(); currentColor = result.ToColor(); UnityThread.executeInUpdate(() => { RefreshColor(); }); } break; } }
void ReceivePackage(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod) { NetDataWriter writer = new NetDataWriter(); ushort msgid = reader.GetUShort(); print("receivepackage event: " + msgid); switch (msgid) { case 1: //register to host { RegisterPlayer(reader); } break; case 101: //create networkObject { int objectId; int lobjectId = reader.GetUShort(); ObjectData objectData = reader.GetBytesWithLength().ToStructure <ObjectData>(); do { objectId = UnityEngine.Random.Range(1000000, 9999999); } while (NetworkObjects.ContainsKey(objectId)); objectData.objectId = objectId; var netObj = new NetworkObjectData(objectId, objectData); NetworkObjects.Add(objectId, netObj); netObj.SendObjectData(peer, writer); writer.Put(lobjectId); client.SendToAll(writer, DeliveryMethod.ReliableOrdered); } break; } }
public void NetworkReceived(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod) { try { if (connectionActive(peer)) { string uuid = clientIDs.Forward[peer.Id]; string partnerUuid = activeConnections[uuid]; long partnerID = clientIDs.Reverse[partnerUuid]; NetPeer partnerPeer = peers[partnerID]; NetDataWriter writer = new NetDataWriter(); writer.Put(reader.GetRemainingBytes()); partnerPeer.Send(writer, DeliveryMethod.ReliableOrdered); } else { int packetId = reader.GetInt(); string uuid = reader.GetString(); byte[] data = reader.GetBytesWithLength(); int activity = reader.GetInt(); string toUuid = reader.GetString(); int toActivity = reader.GetInt(); //stop the connection if it's not CLIENT_INFO if (packetId != CLIENT_INFO) { peer.Disconnect();//bad packet! } else if (clientIDs.Reverse.Contains(uuid)) { peer.Disconnect(createDCMsg(DC_CODE_ALREADY_CONNECTED)); } else { clientIDs.Add(peer.Id, uuid); info.Add(uuid, new ClientInfo(data, activity, toActivity)); if (searchingConnections.ContainsKey(toUuid)) { searchingConnections.Remove(toUuid); activeConnections.Add(uuid, toUuid); activeConnections.Add(toUuid, uuid); ClientInfo selfInfo = info[uuid]; ClientInfo partnerInfo = info[toUuid]; long partnerID = clientIDs.Reverse[toUuid]; NetPeer partnerPeer = peers[partnerID]; if (selfInfo.Activity != partnerInfo.ToActivity || selfInfo.ToActivity != partnerInfo.Activity) { peer.Disconnect(createDCMsg(DC_CODE_DIFFERENT_ACTIVITY)); partnerPeer.Disconnect(createDCMsg(DC_CODE_DIFFERENT_ACTIVITY)); } else { { NetDataWriter writer = new NetDataWriter(); writer.Put(SERVER_CONNECTED); writer.PutBytesWithLength(partnerInfo.Data); peer.Send(writer, DeliveryMethod.ReliableOrdered); } { NetDataWriter writer = new NetDataWriter(); writer.Put(SERVER_CONNECTED); writer.PutBytesWithLength(selfInfo.Data); partnerPeer.Send(writer, DeliveryMethod.ReliableOrdered); } } } else { searchingConnections.Add(uuid, toUuid); } } } reader.Recycle(); } catch (Exception ex) { DiagManager.Instance.LogError(ex); } }
private void OnNetworkReceived(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod) { //rely on current connection state to determine what packets are coming in. if (Status == OnlineStatus.Connecting) { int packetId = reader.GetInt(); //TODO: stop the connection if it's not SERVER_INTRO? string serverName = reader.GetString(); //if (!P2P) Activity.Server.ServerName = serverName; //else // Activity.TargetInfo.Data.TeamName = serverName; DiagManager.Instance.SaveSettings(DiagManager.Instance.CurSettings); NetDataWriter writer = new NetDataWriter(); writer.Put(CLIENT_INFO); writer.Put(Activity.SelfInfo.UUID); writer.PutBytesWithLength(Activity.SelfInfo.SerializeData()); writer.Put((int)Activity.Activity); writer.Put(Activity.TargetInfo.UUID); writer.Put((int)Activity.CompatibleActivity); peer.Send(writer, DeliveryMethod.ReliableOrdered); Status = OnlineStatus.FindingPartner; } else if (Status == OnlineStatus.FindingPartner) { int packetId = reader.GetInt(); //TODO: stop the connection if it's not SERVER_CONNECTED? byte[] partnerData = reader.GetBytesWithLength(); Activity.TargetInfo.DeserializeData(partnerData); Activity.TargetInfo.LastContact = String.Format("{0:yyyy-MM-dd}", DateTime.Now); DiagManager.Instance.SaveSettings(DiagManager.Instance.CurSettings); Activity.SetPeer(peer); Status = OnlineStatus.Connected; } else if (Status == OnlineStatus.ReceivingPartner) { int packetId = reader.GetInt(); //TODO: stop the connection if it's not CLIENT_INFO? string partnerUuid = reader.GetString(); if (partnerUuid != Activity.TargetInfo.UUID) { peerDisconnect(peer, NetCloseReason.PeerIDMismatch); } else { byte[] partnerData = reader.GetBytesWithLength(); Activity.TargetInfo.DeserializeData(partnerData); Activity.TargetInfo.LastContact = String.Format("{0:yyyy-MM-dd}", DateTime.Now); DiagManager.Instance.SaveSettings(DiagManager.Instance.CurSettings); //compare activity for validity ActivityType theirActivity = (ActivityType)reader.GetInt(); string yourUuid = reader.GetString(); if (yourUuid != Activity.SelfInfo.UUID) { peerDisconnect(peer, NetCloseReason.PeerIDMismatch); } else { ActivityType yourActivity = (ActivityType)reader.GetInt(); if (yourActivity != Activity.Activity || theirActivity != Activity.CompatibleActivity) { peerDisconnect(peer, NetCloseReason.DifferentActivity); } else { //Send your own info NetDataWriter writer = new NetDataWriter(); writer.Put(SERVER_CONNECTED); writer.PutBytesWithLength(Activity.SelfInfo.SerializeData()); peer.Send(writer, DeliveryMethod.ReliableOrdered); Activity.SetPeer(peer); Status = OnlineStatus.Connected; } } } } else if (Status == OnlineStatus.Connected) { Activity.NetworkReceived(peer, reader, deliveryMethod); } reader.Recycle(); }