private void OnRecvUpload(ICD.stHeader _msg, string _info) { if (_msg.GetType() != typeof(CMD_SongFile)) { return; } if (_msg.head.cmd != ICD.ICDDefines.CMD_Upload) { return; } ICD.CMD_SongFile msg = (ICD.CMD_SongFile)_msg; try { SongInfo = msg.song; byte[] buf = Utils.Serialize(msg.song); File.WriteAllBytes(Application.persistentDataPath + "/" + msg.song.FileNameNoExt + ".bytes", buf); } catch (Exception ex) { MessageBox.Show(Application.persistentDataPath, msg.song.FileNameNoExt, null); MessageBox.Show("DBID: " + msg.song.DBID, ex.ToString(), null); } NetworkClient.Inst().mOnRecv.RemoveListener(OnRecvUpload); }
public stHeader Copy() { byte[] copyMsg = Serialize(); Type type = GetType(); stHeader copiedMsg = (stHeader)Activator.CreateInstance(type); copiedMsg.Deserialize(copyMsg); return(copiedMsg); }
private void OnRecvNewUser(ICD.stHeader _msg, string _info) { if (_msg.head.cmd != ICD.ICDDefines.CMD_NewUser) { return; } ICD.CMD_UserInfo msg = (ICD.CMD_UserInfo)_msg; mSetting.SetUserName(msg.body.username); }
IEnumerator RunRecieve() { byte[] outbuf = new byte[PACKET_SIZE]; int nbytes = 0; NetworkStream stream = mClient.GetStream(); while (isWaitReceive) { yield return(new WaitForEndOfFrame()); if (!stream.DataAvailable) { continue; } try { nbytes = stream.Read(outbuf, 0, outbuf.Length); mFifoBuffer.Push(outbuf, nbytes); while (true) { byte[] buf = mFifoBuffer.readSize(mFifoBuffer.GetSize()); bool isError = false; ICD.stHeader msg = ICD.stHeader.Parse(buf, ref isError); if (isError) { mFifoBuffer.Clear(); } if (msg == null) { break; } else { mFifoBuffer.Pop(msg.head.len); } IPEndPoint ep = (IPEndPoint)mClient.Client.RemoteEndPoint; string ipAddress = ep.Address.ToString(); int port = ep.Port; string info = ipAddress + ":" + port.ToString(); if (mOnRecv != null) { mOnRecv.Invoke(msg, info); } } } catch (Exception ex) { Debug.Log(ex.ToString()); } } mFifoBuffer.Clear(); stream.Close(); }
public bool SendMsgToServer(ICD.stHeader msg) { if (mClient == null) { return(false); } if (msg.head.len > PACKET_SIZE * 32) { StartCoroutine("SendToServerAsync", msg.Serialize()); } else { SendToServer(msg.Serialize()); } return(true); }
private void OnRecvDownload(ICD.stHeader _msg, string _info) { if (_msg.GetType() != typeof(CMD_SongFile)) { return; } if (_msg.head.cmd != ICD.ICDDefines.CMD_Download) { return; } ICD.CMD_SongFile msg = (ICD.CMD_SongFile)_msg; msg.song.FilePath = Application.persistentDataPath + "/"; File.WriteAllBytes(Application.persistentDataPath + "/" + msg.song.FileNameNoExt + ".mp3", msg.stream.ToArray()); byte[] buf = Utils.Serialize(msg.song); File.WriteAllBytes(Application.persistentDataPath + "/" + msg.song.FileNameNoExt + ".bytes", buf); SongInfo = msg.song; NetworkClient.Inst().mOnRecv.RemoveListener(OnRecvDownload); }
static public stHeader Parse(byte[] buf, ref bool isError) { isError = false; int headSize = HeaderSize(); if (buf.Length < headSize) { return(null); } byte[] headBuf = new byte[headSize]; Array.Copy(buf, 0, headBuf, 0, headSize); stHeader header = new stHeader(); header.Deserialize(headBuf); int msgSize = (int)header.head.len; if (!header.IsValid()) { isError = true; return(null); } if (buf.Length < msgSize) { return(null); } if (!CmdTable.Pairs.ContainsKey(header.head.cmd)) { isError = true; return(null); } Type objType = CmdTable.Pairs[header.head.cmd].GetType(); stHeader obj = (stHeader)Activator.CreateInstance(objType); obj.Deserialize(buf); return(obj); }
void OnRecvMusicList(ICD.stHeader _msg, string _info) { if (_msg.head.cmd != ICD.ICDDefines.CMD_MusicList) { return; } ICD.CMD_MusicList msg = (ICD.CMD_MusicList)_msg; for (int i = 0; i < msg.musics.Count; ++i) { int DBID = msg.musics[i].DBID; if (mSongIDs.ContainsKey(DBID)) { continue; } GameObject obj = Instantiate(prefabListItem, new Vector2(0, 0), Quaternion.identity, transform); ItemDisplay item = obj.GetComponent <ItemDisplay>(); item.SongInfo = msg.musics[i]; } NetworkClient.Inst().mOnRecv.RemoveListener(OnRecvMusicList); }