void SendPositionInformation() { float currentTime = Time.time * 1000; if (GameStarted() && currentTime - lastSendPosition > cooldownSendPosition) { if (gameController != null && gameController.player != null) { Debug.Log(myPort + " SendPositionInformation: " + gameController.player.transform.position + ", lane: " + myLane + ", health:" + gameController.player.Health); } else { Debug.Log(myPort + " SendPositionInformation: null, lane: " + myLane); } lastSendPosition = Time.time * 1000; PositionMessage message = new PositionMessage(); message.hp = 0; message.lane = 10; message.position = new Vector2(0, 0); if (gameController.player != null) { message.lane = System.Convert.ToUInt32(myLane); message.position = gameController.player.transform.position; if (gameController.player.Health > 0) { message.hp = System.Convert.ToUInt32(gameController.player.Health); } } P2PSender.SendToAll(P2PChannels.UnreliableChannelId, message, MessageTypes.Position); } }
public static void ApplyAndSpreadConsentResult(AnswerConsentMessage message) { ApplyConsentMessage applyMessage = new ApplyConsentMessage(); applyMessage.consentAction = message.consentAction; applyMessage.result = message.result; applyMessage.parameters = message.parameters; P2PSender.SendToAll(P2PChannels.ReliableChannelId, applyMessage, MessageTypes.ApplyConsent); p2PController.ApplyConsent(applyMessage, true); }
public static void ConnectEvent(int hostId, int connectionId) { int port; string ip; UnityEngine.Networking.Types.NetworkID netId; UnityEngine.Networking.Types.NodeID nodeId; NetworkTransport.GetConnectionInfo(hostId, connectionId, out ip, out port, out netId, out nodeId, out P2PController.error); P2PConnection connection = P2PConnectionManager.GetConnection(ip, port); if (connection == null) { //new connection from targeted ip or new player connection = new P2PConnection(hostId, connectionId); connection.ip = ip; connection.port = port; connections.Add(connection); Debug.Log("New connection with " + connection); if (!JoinRequestSend) //I'm wanting to join { Debug.Log("Sending Join Request"); JoinRequestSend = true; JoinRequestMessage message = new JoinRequestMessage(); P2PSender.Send(hostId, connectionId, P2PChannels.ReliableChannelId, message, MessageTypes.JoinRequest); } else if (JoinAnswerReceived && !p2PController.GameStarted()) { connection.SuccessfullyConnect(); } } else if (!connection.ConnectionSuccessful()) { //successfully connect to an existing player. Connection requested previously connection.hostId = hostId; connection.connectionId = connectionId; connection.SuccessfullyConnect(); JoinAnnounceMessage announceMessage = new JoinAnnounceMessage(); P2PSender.Send(hostId, connectionId, P2PChannels.ReliableChannelId, announceMessage, MessageTypes.JoinAnnounce); } if (!p2PController.GameStarted()) { if (JoinAnswerReceived) { CheckConnectionsStatus(); } } }
private void AudioIcon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { string srcId = App.user.userName; string destId = chatViewModel.selectedChatRoom.id; string targetIP = chatViewModel.selectedChatRoom.addressInfo.IPAddress; byte[] data = AppProtocol.PackControl(AppProtocol.ControlType.QAUDIO, srcId, destId); P2PSender.getInstance().SendData(data, targetIP, P2PListener.GENERALLISTENPORT); var audio = new Audio(targetIP); audioWindow = new audioWindow(audio); audioWindow.Show(); }
public AnswerConsentMessage OnAskForConsentMsg(int hostId, int connectionId, AskConsentMessage message) { Debug.Log("P2P: OnAskForConsentMsg for: " + message.consentAction + ", parameter0: " + message.parameters[0]); AnswerConsentMessage answerMessage = new AnswerConsentMessage(); answerMessage.consentId = message.consentId; answerMessage.consentAction = message.consentAction; if (message.consentAction == ConsentAction.SpawnRocket) { answerMessage.result = gameController.lanes[message.parameters[1]].spawnManager.GetRandomSpawnerIndex(); answerMessage.parameters = message.parameters; //will I cheat? int randomInt = UnityEngine.Random.Range(0, 100); if (randomInt < gameController.cheaterProfileContainer.cheaterProfile.cheatingRate) { if (Recorder.session != null) { Recorder.session.cheatsTried++; } //sending a wrong spawner index answerMessage.result = 100; } if (hostId != -1 && connectionId != -1) //not imposing consent (see AskForConsent) { P2PSender.Send(hostId, connectionId, P2PChannels.ReliableChannelId, answerMessage, MessageTypes.AnswerConsent); } } if (message.consentAction == ConsentAction.JoinGame) { answerMessage.parameters = message.parameters; Lane freeLane = gameController.GetFirstUnoccupiedLane(); answerMessage.result = 10; if (freeLane != null) { answerMessage.result = freeLane.id; } if (hostId != -1 && connectionId != -1) //not imposing consent (see AskForConsent) { P2PSender.Send(hostId, connectionId, P2PChannels.ReliableChannelId, answerMessage, MessageTypes.AnswerConsent); } } return(answerMessage); }
void SendPositionInformation() { float currentTime = Time.time * 1000; if (GameStarted() && currentTime - lastSendPosition > cooldownSendPosition) { lastSendPosition = Time.time * 1000; PositionMessage message = new PositionMessage(); message.lane = System.Convert.ToUInt32(myLane); message.position = gameController.player.transform.position; message.hp = System.Convert.ToUInt32(gameController.player.Health); P2PSender.SendToAll(P2PChannels.UnreliableChannelId, message, MessageTypes.Position); } }
// 发送文字消息,调用应用层AppProtocol的接口封包,调用传输层P2PSender/UDPSender发送 private void SendWordMsg() { if (chatRoomList.SelectedIndex == -1) { MessageBox.Show("请选择聊天对象"); return; } if (msgTbx.Text == "") { MessageBox.Show("所发消息不能为空"); return; } string targetIP = chatViewModel.selectedChatRoom.addressInfo.IPAddress; int targetPort = P2PListener.GENERALLISTENPORT; string srcId = App.user.userName; string destId = chatViewModel.selectedChatRoom.id; if (groupChat.isGroupChatMsg(destId)) { // 群聊消息单独处理 groupChat.SendGroupWord(msgTbx.Text, destId); } else { byte[] data = AppProtocol.PackWord(msgTbx.Text, srcId, destId); if (isUDPCbx.IsChecked == true) { UDPSender.getInstance().ReliableSendData(data, targetIP); // udp 发送不支持指定端口 } else { P2PSender.getInstance().SendData(data, targetIP, targetPort); } } // 本地显示自己的话 chatViewModel.selectedChatRoom.msgList.Add(new Model.Message { description = msgTbx.Text, src = "我" }); updateUI(); msgTbx.Text = ""; }
public void AskForConsent(ConsentMessage consentMessage) { AskConsentMessage message = new AskConsentMessage(); message.consentId = P2PConsentManager.GetNextConsentIdAndIncrement(); message.consentAction = consentMessage.consentAction; message.result = consentMessage.result; message.parameters.AddRange(consentMessage.parameters); if (consensusAlgorithm && P2PConnectionManager.SuccessfulConnectionsCount() > 0) { Debug.Log("P2P: Asking consent for: " + consentMessage.consentAction); P2PConsentManager.AddPendingConsent(message); P2PSender.SendToAll(P2PChannels.ReliableChannelId, message, MessageTypes.AskConsent); } else { Debug.Log("P2P: Imposing consent for: " + consentMessage.consentAction); P2PConsentManager.ApplyAndSpreadConsentResult(OnAskForConsentMsg(-1, -1, message)); } }
public void ApplyConsent(ConsentMessage message) { Debug.Log("P2P: Applying consent for: " + message.consentAction); if (message.consentAction == ConsentAction.SpawnRocket) { bool cheating = !gameController.lanes[message.parameters[1]].spawnManager.ValidIndex(message.result); if (!cheating) { gameController.lanes[message.parameters[1]].spawnManager.Spawn(message.result); } else { Debug.Log("Cheat!"); if (Recorder.session != null) { Recorder.session.cheatsPassed++; } } } else if (message.consentAction == ConsentAction.JoinGame) { P2PConnection connection = P2PConnectionManager.GetConnection(message.parameters[0], message.parameters[1]); if (connection != null) { JoinAnswerMessage answerMessage = new JoinAnswerMessage(); answerMessage.lane = message.result; answerMessage.successfulConnections = P2PConnectionManager.GetSuccessfulConnections(); answerMessage.r = Convert.ToInt32(gameColor.r * 255); answerMessage.g = Convert.ToInt32(gameColor.g * 255); answerMessage.b = Convert.ToInt32(gameColor.b * 255); P2PSender.Send(message.parameters[0], message.parameters[1], P2PChannels.ReliableChannelId, answerMessage, MessageTypes.JoinAnswer); connection.SuccessfullyConnect(); Debug.Log("Sending JoinAnswer with lane: " + answerMessage.lane + "and " + answerMessage.successfulConnections.Count + " connections"); } } }
// 发送图片消息,调用应用层AppProtocol的接口封包,调用传输层P2PSender发送 private void SendPhoto() { // 选择表情包 OpenFileDialog dlg = new OpenFileDialog(); dlg.Filter = "图片文件|*.jpg;*.gif;*.bmp;*.png"; Nullable <bool> result = dlg.ShowDialog(); string filename = null; if (result != true) { return; } filename = dlg.FileName; System.Drawing.Bitmap bitmap; try { bitmap = new System.Drawing.Bitmap(filename); } catch (Exception ex) { MessageBox.Show(ex.Message, "图片不存在"); return; } // 封包发送 string srcId = App.user.userName; string destId = chatViewModel.selectedChatRoom.id; string targetIP = chatViewModel.selectedChatRoom.addressInfo.IPAddress; int targetPort = P2PListener.GENERALLISTENPORT; byte[] data = AppProtocol.PackPhoto(bitmap, srcId, destId); P2PSender.getInstance().SendData(data, targetIP, targetPort); chatViewModel.selectedChatRoom.msgList.Add(new Model.Message { src = "我", msg = bitmap }); updateUI(); }
// 发送文件消息,调用应用层AppProtocol的接口封包,调用传输层P2PSender发送 private void SendFileMsg() { // 选择文件 OpenFileDialog dlg = new OpenFileDialog(); Nullable <bool> result = dlg.ShowDialog(); string filename = null; if (result != true) { return; } filename = dlg.FileName; FileStream fstream = null; try { fstream = new FileStream(filename, FileMode.Open, FileAccess.Read); } catch (Exception e) { MessageBox.Show(e.Message, "文件不存在"); return; } // 调用应用层封包,调用传输层发送 string srcId = App.user.userName; string destId = chatViewModel.selectedChatRoom.id; string targetIP = chatViewModel.selectedChatRoom.addressInfo.IPAddress; int targetPort = P2PListener.FILELISTENPORT; byte[][] dataGroup = AppProtocol.PackFile(fstream, srcId, destId); fstream.Close(); P2PSender.getInstance().SendDataGroup(dataGroup, targetIP, targetPort); chatViewModel.selectedChatRoom.msgList.Add(new Model.Message { description = $"文件:{filename.Substring(filename.LastIndexOf('\\') + 1)}", src = "我" }); updateUI(); }
// 控制报文的发送散落于音视频会话,群聊创建等有需要的过程中 // --------------------------- 收到控制报文 ------------------------- private void RecvControl(byte[] newData) { string srcId = null; string destId = null; string optional = null; var ctype = AppProtocol.UnPackControl(newData, ref srcId, ref destId, ref optional); if (ctype == AppProtocol.ControlType.NEWGROUP) { App.addressBook.items.Add(new Model.AddressBook.Item() { UserName = srcId, GroupUserName = optional.Split(';'), Alias = srcId, isGroup = true }); return; } string targetIP = null; foreach (var item in chatViewModel.chatRoomViewModels) { if (item.addressInfo.UserName == srcId) { targetIP = item.addressInfo.IPAddress; } } targetIP = chatViewModel.chatRoomViewModels[0].addressInfo.IPAddress; // debugTemp if (targetIP == null) // 收到非好友视频邀请不处理 { return; } switch (ctype) { case AppProtocol.ControlType.QAUDIO: var result = MessageBox.Show($"{srcId}邀请您进行音频聊天,是否接受?", "新音频邀请", MessageBoxButton.YesNo); if (result == MessageBoxResult.No) { P2PSender.getInstance().SendData(AppProtocol.PackControl(AppProtocol.ControlType.RAUDIO, destId, srcId), targetIP, P2PListener.GENERALLISTENPORT); } else { P2PSender.getInstance().SendData(AppProtocol.PackControl(AppProtocol.ControlType.AAUDIO, destId, srcId), targetIP, P2PListener.GENERALLISTENPORT); var audio = new Audio(targetIP); audioWindow = new audioWindow(audio); audioWindow.Show(); } break; case AppProtocol.ControlType.AAUDIO: break; case AppProtocol.ControlType.RAUDIO: audioWindow.audio.isChatting = false; audioWindow.Close(); break; case AppProtocol.ControlType.QVIDEO: result = MessageBox.Show($"{srcId}邀请您进行视频聊天,是否接受?", "新视频邀请", MessageBoxButton.YesNo); if (result == MessageBoxResult.No) { P2PSender.getInstance().SendData(AppProtocol.PackControl(AppProtocol.ControlType.RVIDEO, destId, srcId), targetIP, P2PListener.GENERALLISTENPORT); } else { P2PSender.getInstance().SendData(AppProtocol.PackControl(AppProtocol.ControlType.AVIDEO, destId, srcId), targetIP, P2PListener.GENERALLISTENPORT); var video = new Video(targetIP); var videoWindow = new videoWindow(video); videoWindow.Show(); } break; case AppProtocol.ControlType.AVIDEO: break; case AppProtocol.ControlType.RVIDEO: videoWindow.video.isReject = true; videoWindow.Close(); MessageBox.Show("对方拒绝了你的视频邀请"); break; default: break; } }