private void RecvThread() { isRecv = true; IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(NetGlobal.Instance().serverIP), UdpManager.Instance.localPort); while (isRecv) { try { byte[] buf = sendClient.Receive(ref endpoint); byte packMessageId = buf[PackageConstant.PackMessageIdOffset]; //消息id (1个字节) Int16 packlength = BitConverter.ToInt16(buf, PackageConstant.PacklengthOffset); //消息包长度 (2个字节) int bodyDataLenth = packlength - PackageConstant.PacketHeadLength; byte[] bodyData = new byte[bodyDataLenth]; Array.Copy(buf, PackageConstant.PacketHeadLength, bodyData, 0, bodyDataLenth); delegate_analyze_message((SCID)packMessageId, bodyData); //是客户端,统计接收量 BattleData.Instance.recvNum += buf.Length; // Debug.Log("发送量:" + buf.Length.ToString() + "," + GameData.Instance().recvNum.ToString()); } catch (Exception ex) { Debug.Log("udpClient接收数据异常:" + ex.Message); } } Debug.Log("udp接收线程退出~~~~~"); }
public void OnCliclCancelMatch() { //取消匹配 TcpCancelMatch _mes = new TcpCancelMatch(); _mes.uid = NetGlobal.Instance().userUid; MarsTcp.Instance.SendMessage(CSData.GetSendMessage <TcpCancelMatch>(_mes, CSID.TCP_CANCEL_MATCH)); }
public void StartClientUdp() { mesNum = 0; _upd = new MarsUdp(); _upd.StartClientUdp(NetGlobal.Instance().serverIP); _upd.delegate_analyze_message = AnalyzeMessage; }
void Start() { Screen.sleepTimeout = SleepTimeout.NeverSleep; Application.targetFrameRate = 30; NetGlobal.Instance(); TcpPB.Instance().mes_login_result = Message_Login_Result; waitTip.SetActive(false); }
public void OnClickMatch() { //开始匹配 // SceneManager.LoadScene (GameConfig.battleScene); TcpRequestMatch _mes = new TcpRequestMatch(); _mes.uid = NetGlobal.Instance().userUid; _mes.roleID = 1; MarsTcp.Instance.SendMessage(CSData.GetSendMessage <TcpRequestMatch>(_mes, CSID.TCP_REQUEST_MATCH)); }
void CreatUpd() { _udpClient = new UdpClient(); IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(NetGlobal.Instance().serverIP), NetGlobal.Instance().udpSendPort); _udpClient.Connect(endpoint); IPEndPoint _localEnd = (IPEndPoint)_udpClient.Client.LocalEndPoint; localPort = _localEnd.Port; Debug.Log("udp参数:" + _localEnd.Address + "," + _localEnd.Port); }
void Message_Login_Result(TcpResponseLogin _mes) { if (_mes.result) { NetGlobal.Instance().userUid = _mes.uid; NetGlobal.Instance().udpSendPort = _mes.udpPort; Debug.Log("登录成功~~~" + NetGlobal.Instance().userUid); ClearSceneData.LoadScene(GameConfig.mainScene); } else { Debug.Log("登录失败~~~"); waitTip.SetActive(false); } }
public void UpdateBattleInfo(int _randseed, List <BattleUserInfo> _userInfo) { randSeed = _randseed; list_battleUser = new List <BattleUserInfo> (_userInfo); foreach (var item in list_battleUser) { if (item.uid == NetGlobal.Instance().userUid) { battleID = item.battleID; selfOperation.battleID = battleID; Debug.Log("自己的战斗id:" + battleID); } dic_rightOperationID [item.battleID] = 0; } }
public void AnalyzeMessage(SCID messageId, byte[] bodyData) { switch (messageId) { case SCID.TCP_RESPONSE_LOGIN: { TcpResponseLogin pb_ReceiveMes = CSData.DeserializeData <TcpResponseLogin>(bodyData); NetGlobal.Instance().AddAction(() => { mes_login_result(pb_ReceiveMes); }); } break; case SCID.TCP_RESPONSE_REQUEST_MATCH: { TcpResponseRequestMatch pb_ReceiveMes = CSData.DeserializeData <TcpResponseRequestMatch>(bodyData); NetGlobal.Instance().AddAction(() => { mes_request_match_result(pb_ReceiveMes); }); } break; case SCID.TCP_RESPONSE_CANCEL_MATCH: { TcpResponseCancelMatch pb_ReceiveMes = CSData.DeserializeData <TcpResponseCancelMatch>(bodyData); NetGlobal.Instance().AddAction(() => { mes_cancel_match_result(pb_ReceiveMes); }); } break; case SCID.TCP_ENTER_BATTLE: { TcpEnterBattle pb_ReceiveMes = CSData.DeserializeData <TcpEnterBattle>(bodyData); NetGlobal.Instance().AddAction(() => { mes_enter_battle(pb_ReceiveMes); }); } break; default: break; } }
public void AnalyzeMessage(SCID messageId, byte[] bodyData) { switch (messageId) { case SCID.UDP_BATTLE_START: { UdpBattleStart pb_ReceiveMes = CSData.DeserializeData <UdpBattleStart>(bodyData); NetGlobal.Instance().AddAction(() => { mes_battle_start(pb_ReceiveMes); }); } break; case SCID.UDP_DOWN_FRAME_OPERATIONS: { UdpDownFrameOperations pb_ReceiveMes = CSData.DeserializeData <UdpDownFrameOperations>(bodyData); NetGlobal.Instance().AddAction(() => { mes_frame_operation(pb_ReceiveMes); }); } break; case SCID.UDP_DOWN_DELTA_FRAMES: { UdpDownDeltaFrames pb_ReceiveMes = CSData.DeserializeData <UdpDownDeltaFrames>(bodyData); NetGlobal.Instance().AddAction(() => { mes_delta_frame_data(pb_ReceiveMes); }); } break; case SCID.UDP_DOWN_GAME_OVER: { UdpDownGameOver pb_ReceiveMes = CSData.DeserializeData <UdpDownGameOver>(bodyData); NetGlobal.Instance().AddAction(() => { mes_down_game_over(pb_ReceiveMes); }); } break; default: break; } }
private void requestConnectCallBack(IAsyncResult iar) { try { //还原原始的TcpClient对象 Socket client = (Socket)iar.AsyncState; // client.EndConnect(iar); Debug.Log("连接服务器成功:" + client.RemoteEndPoint.ToString()); isRun = true; NetGlobal.Instance().AddAction(() => { if (ac_connect != null) { ac_connect(true); } }); Thread myThread = new Thread(ReceiveMessage); myThread.Start(); } catch (Exception e) { NetGlobal.Instance().AddAction(() => { if (ac_connect != null) { ac_connect(false); } }); Debug.Log("tcp连接异常:" + e.Message); } finally { } }
public void OnClickLogin() { Debug.Log(inputField.text); waitTip.SetActive(true); string _ip = inputField.text; MarsTcp.Instance.ConnectServer(_ip, (_result) => { if (_result) { Debug.Log("连接成功~~"); NetGlobal.Instance().serverIP = _ip; TcpLogin _loginInfo = new TcpLogin(); _loginInfo.token = SystemInfo.deviceUniqueIdentifier; MarsTcp.Instance.SendMessage(CSData.GetSendMessage <TcpLogin>(_loginInfo, CSID.TCP_LOGIN)); } else { Debug.Log("连接失败~~"); waitTip.SetActive(false); } }); }
void Send_BattleReady() { UdpPB.Instance().SendBattleReady(NetGlobal.Instance().userUid, BattleData.Instance.battleID); }
void Update() { NetGlobal.Instance().DoForAction(); }