protected virtual void onReceiveMessage(GalaxyID peer, Stream messageStream) { if (bandwidthLogger != null) { bandwidthLogger.RecordBytesDown(messageStream.Length); } using IncomingMessage message = new IncomingMessage(); using BinaryReader reader = new BinaryReader(messageStream); message.Read(reader); if (peers.ContainsLeft(message.FarmerID) && peers[message.FarmerID] == peer.ToUint64()) { gameServer.processIncomingMessage(message); } else if (message.MessageType == 2) { NetFarmerRoot farmer = Game1.multiplayer.readFarmer(message.Reader); GalaxyID capturedPeer = new GalaxyID(peer.ToUint64()); gameServer.checkFarmhandRequest(createUserID(peer), getConnectionId(peer), farmer, delegate(OutgoingMessage msg) { sendMessage(capturedPeer, msg); }, delegate { peers[farmer.Value.UniqueMultiplayerID] = capturedPeer.ToUint64(); }); } }
private void onPersonaDataChanged(GalaxyID userID, uint avatarCriteria) { if (peers.ContainsRight(userID.ToUint64())) { long farmerID = peers.GetLeft(userID.ToUint64()); Game1.multiplayer.broadcastUserName(farmerID, GalaxyInstance.Friends().GetFriendPersonaName(userID)); } }
public virtual void onReceiveDisconnect(GalaxyID peer) { Console.WriteLine("{0} disconnected", peer); onDisconnect(getConnectionId(peer)); if (peers.ContainsRight(peer.ToUint64())) { playerDisconnected(peers[peer.ToUint64()]); } }
private void onGalaxyLobbyLeft(GalaxyID lobbyID, ILobbyLeftListener.LobbyLeaveReason leaveReason) { if (leaveReason != ILobbyLeftListener.LobbyLeaveReason.LOBBY_LEAVE_REASON_USER_LEFT) { Program.WriteLog(Program.LogType.Disconnect, "Forcibly left Galaxy lobby at " + DateTime.Now.ToLongTimeString() + " - " + leaveReason, append: true); } if (Game1.chatBox != null) { string lobby_lost_reason = ""; switch (leaveReason) { case ILobbyLeftListener.LobbyLeaveReason.LOBBY_LEAVE_REASON_CONNECTION_LOST: lobby_lost_reason = Game1.content.LoadString("Strings\\UI:Chat_LobbyLost_ConnectionLost"); break; case ILobbyLeftListener.LobbyLeaveReason.LOBBY_LEAVE_REASON_LOBBY_CLOSED: lobby_lost_reason = Game1.content.LoadString("Strings\\UI:Chat_LobbyLost_LobbyClosed"); break; case ILobbyLeftListener.LobbyLeaveReason.LOBBY_LEAVE_REASON_USER_LEFT: lobby_lost_reason = Game1.content.LoadString("Strings\\UI:Chat_LobbyLost_UserLeft"); break; } Game1.chatBox.addInfoMessage(Game1.content.LoadString("Strings\\UI:Chat_LobbyLost", lobby_lost_reason).Trim()); } Console.WriteLine("Left lobby {0} - leaveReason: {1}", lobbyID.ToUint64(), leaveReason); lobby = null; recreateTimer = getTimeNow() + 20000; isRecreatedLobby = true; isFirstRecreateAttempt = true; }
public void Send(GalaxyID peer, byte[] data) { if (!connections.ContainsKey(peer.ToUint64())) { return; } if (data.Length <= 1100) { byte[] packet2 = new byte[data.Length + 4]; data.CopyTo(packet2, 4); GalaxyInstance.Networking().SendP2PPacket(peer, packet2, (uint)packet2.Length, P2PSendType.P2P_SEND_RELIABLE); return; } int chunkSize = 1096; int messageOffset = 0; byte[] packet = new byte[1100]; packet[0] = 1; while (messageOffset < data.Length) { int thisChunkSize = chunkSize; if (messageOffset + chunkSize >= data.Length) { packet[0] = 0; thisChunkSize = data.Length - messageOffset; } Buffer.BlockCopy(data, messageOffset, packet, 4, thisChunkSize); messageOffset += thisChunkSize; GalaxyInstance.Networking().SendP2PPacket(peer, packet, (uint)(thisChunkSize + 4), P2PSendType.P2P_SEND_RELIABLE); } }
public IEnumerable <GalaxyID> LobbyMembers() { if (lobby == null) { yield break; } uint lobby_members_count; try { lobby_members_count = GalaxyInstance.Matchmaking().GetNumLobbyMembers(lobby); } catch (Exception) { yield break; } uint i = 0u; while (i < lobby_members_count) { GalaxyID lobbyMember = GalaxyInstance.Matchmaking().GetLobbyMemberByIndex(lobby, i); if (!(lobbyMember == selfId) && !ghosts.Contains(lobbyMember.ToUint64())) { yield return(lobbyMember); } uint num = i + 1; i = num; } }
private void onGalaxyLobbyEnter(GalaxyID lobbyID, LobbyEnterResult result) { connectingLobbyID = null; if (result != 0) { return; } Console.WriteLine("Lobby entered: {0}", lobbyID.ToUint64()); lobby = lobbyID; lobbyOwner = GalaxyInstance.Matchmaking().GetLobbyOwner(lobbyID); if (Game1.chatBox != null) { string invite_code_string = ""; if (Program.sdk.Networking != null && Program.sdk.Networking.SupportsInviteCodes()) { invite_code_string = Game1.content.LoadString("Strings\\UI:Chat_LobbyJoined_InviteCode", GetInviteCode()); } if (isRecreatedLobby) { Game1.chatBox.addInfoMessage(Game1.content.LoadString("Strings\\UI:Chat_LobbyRecreated", invite_code_string).Trim()); } else { Game1.chatBox.addInfoMessage(Game1.content.LoadString("Strings\\UI:Chat_LobbyJoined", invite_code_string).Trim()); } } if (lobbyOwner == selfId) { foreach (KeyValuePair <string, string> pair in lobbyData) { GalaxyInstance.Matchmaking().SetLobbyData(lobby, pair.Key, pair.Value); } updateLobbyPrivacy(); } }
private void onLobbyDataUpdated(GalaxyID lobbyID, GalaxyID memberID) { foreach (LobbyUpdateListener lobbyUpdateListener in lobbyUpdateListeners) { lobbyUpdateListener.OnLobbyUpdate(lobbyID.ToUint64()); } }
private string getConnectionString() { if (lobby == null) { return(""); } return("-connect-lobby-" + lobby.ToUint64()); }
protected virtual void onReceiveDisconnect(GalaxyID peer) { if (peer != serverId) { Game1.multiplayer.playerDisconnected((long)peer.ToUint64()); return; } timedOut = true; pendingDisconnect = Multiplayer.DisconnectType.HostLeft; }
private void onLobbyDataUpdate(LobbyDataUpdate_t pCallback) { CSteamID steamLobby = new CSteamID(pCallback.m_ulSteamIDLobby); GalaxyID lobbyID = parseConnectionString(SteamMatchmaking.GetLobbyData(steamLobby, "connect")); lobbyOwners[lobbyID.ToUint64()] = SteamMatchmaking.GetLobbyOwner(steamLobby); if (lobbyID != null) { GalaxyInstance.Matchmaking().RequestLobbyData(lobbyID); } }
private void onReceiveDisconnect(GalaxyID peer) { if (peer != this.serverId) { ModCore.multiplayer.playerDisconnected((long)peer.ToUint64()); } else { this.timedOut = true; } }
protected override void onReceiveMessage(GalaxyID peer, Stream messageStream) { using (IncomingMessage message = new IncomingMessage()) using (BinaryReader reader = new BinaryReader(messageStream)) { message.Read(reader); this.OnProcessingMessage(message, outgoing => this.sendMessage(peer, outgoing), () => { if (this.peers.ContainsLeft(message.FarmerID) && (long)this.peers[message.FarmerID] == (long)peer.ToUint64()) { this.gameServer.processIncomingMessage(message); } else if (message.MessageType == StardewValley.Multiplayer.playerIntroduction) { NetFarmerRoot farmer = this.Multiplayer.readFarmer(message.Reader); GalaxyID capturedPeer = new GalaxyID(peer.ToUint64()); this.gameServer.checkFarmhandRequest(Convert.ToString(peer.ToUint64()), farmer, msg => this.sendMessage(capturedPeer, msg), () => this.peers[farmer.Value.UniqueMultiplayerID] = capturedPeer.ToUint64()); } }); } }
public long GetPingWith(GalaxyID peer) { long time = 0L; lastMessageTime.TryGetValue(peer.ToUint64(), out time); if (time == 0L) { return(0L); } if (getTimeNow() - time > 30000) { return(long.MaxValue); } return(GalaxyInstance.Networking().GetPingWith(peer)); }
private void onGalaxyLobbyEnter(GalaxyID lobbyID, LobbyEnterResult result) { if (result == LobbyEnterResult.LOBBY_ENTER_RESULT_SUCCESS) { Console.WriteLine("Lobby entered: {0}", lobbyID.ToUint64()); lobby = lobbyID; lobbyOwner = GalaxyInstance.Matchmaking().GetLobbyOwner(lobbyID); if (lobbyOwner == selfId) { foreach (KeyValuePair <string, string> pair in lobbyData) { GalaxyInstance.Matchmaking().SetLobbyData(lobby, pair.Key, pair.Value); } updateLobbyPrivacy(); } } }
protected override void onReceiveMessage(GalaxyID peer, Stream messageStream) { using IncomingMessage message = new IncomingMessage(); using BinaryReader reader = new BinaryReader(messageStream); message.Read(reader); ulong peerID = peer.ToUint64(); // note: GalaxyID instances get reused, so need to store the underlying ID instead this.OnProcessingMessage(message, outgoing => this.SendMessageToPeerID(peerID, outgoing), () => { if (this.peers.ContainsLeft(message.FarmerID) && (long)this.peers[message.FarmerID] == (long)peerID) { this.gameServer.processIncomingMessage(message); } else if (message.MessageType == StardewValley.Multiplayer.playerIntroduction) { NetFarmerRoot farmer = this.Multiplayer.readFarmer(message.Reader); GalaxyID capturedPeer = new GalaxyID(peerID); this.gameServer.checkFarmhandRequest(Convert.ToString(peerID), this.getConnectionId(peer), farmer, msg => this.sendMessage(capturedPeer, msg), () => this.peers[farmer.Value.UniqueMultiplayerID] = capturedPeer.ToUint64()); } }); }
public string getConnectionId(GalaxyID peer) { return("GN_" + Convert.ToString(peer.ToUint64())); }
public void Receive(Action <GalaxyID> onConnection, Action <GalaxyID, Stream> onMessage, Action <GalaxyID> onDisconnect, Action <string> onError) { long timeNow = getTimeNow(); if (lobby == null) { if (lobbyOwner == selfId && recreateTimer > 0 && recreateTimer <= timeNow) { recreateTimer = 0L; tryCreateLobby(); } return; } string lobbyVersion = GalaxyInstance.Matchmaking().GetLobbyData(lobby, "protocolVersion"); if (lobbyVersion != "" && lobbyVersion != protocolVersion) { onError("Strings\\UI:CoopMenu_FailedProtocolVersion"); Close(); return; } foreach (GalaxyID lobbyMember in LobbyMembers()) { if (!connections.ContainsKey(lobbyMember.ToUint64()) && !ghosts.Contains(lobbyMember.ToUint64())) { connections.Add(lobbyMember.ToUint64(), lobbyMember); onConnection(lobbyMember); } } ghosts.IntersectWith(from peer in LobbyMembers() select peer.ToUint64()); byte[] buffer = new byte[1300]; uint packetSize = 1300u; GalaxyID sender = new GalaxyID(); while (GalaxyInstance.Networking().ReadP2PPacket(buffer, (uint)buffer.Length, ref packetSize, ref sender)) { lastMessageTime[sender.ToUint64()] = timeNow; if (!connections.ContainsKey(sender.ToUint64()) || buffer[0] == byte.MaxValue) { continue; } bool incomplete = buffer[0] == 1; MemoryStream messageData2 = new MemoryStream(); messageData2.Write(buffer, 4, (int)(packetSize - 4)); if (incompletePackets.ContainsKey(sender.ToUint64())) { messageData2.Position = 0L; messageData2.CopyTo(incompletePackets[sender.ToUint64()]); if (!incomplete) { messageData2 = incompletePackets[sender.ToUint64()]; incompletePackets.Remove(sender.ToUint64()); messageData2.Position = 0L; onMessage(sender, messageData2); } } else if (incomplete) { messageData2.Position = messageData2.Length; incompletePackets[sender.ToUint64()] = messageData2; } else { messageData2.Position = 0L; onMessage(sender, messageData2); } } List <GalaxyID> disconnectedPeers = new List <GalaxyID>(); foreach (GalaxyID peer3 in connections.Values) { if (!lobbyContains(peer3) || ghosts.Contains(peer3.ToUint64())) { disconnectedPeers.Add(peer3); } } foreach (GalaxyID peer2 in disconnectedPeers) { onDisconnect(peer2); close(peer2); } }
public void Kick(GalaxyID user) { ghosts.Add(user.ToUint64()); }
private void close(GalaxyID peer) { connections.Remove(peer.ToUint64()); incompletePackets.Remove(peer.ToUint64()); }
private void onGalaxyLobbyLeft(GalaxyID lobbyID, bool ioFailure) { Console.WriteLine("Left lobby {0} - ioFailure: {1}", lobbyID.ToUint64(), ioFailure); lobby = null; recreateTimer = getTimeNow() + 20000; }
private string createUserID(GalaxyID peer) { return(Convert.ToString(peer.ToUint64())); }