/// <summary> /// bridge when receiving new message /// </summary> public void applyMessage(NwkMessageFull msg) { string header = msg.header.getHeader(); string[] split = header.Split(MSG_HEADER_SPEARATOR); // IID , CONN ID ? short iid = short.Parse(split[0]); NwkSyncableData data = getDataByIID(iid); if (data == null) { Debug.LogWarning("no sync data found for msg " + header); short oType = short.Parse(split[1]); data = solveUnknownData(msg.getIdCard().getMessageSender(), iid, oType); if (data == null) { log("don't have object " + msg.header.getHeader() + " sent by : " + msg.getIdCard().getMessageSender()); } } //must have data here ? if (data == null) { Debug.LogError("no data, must have some here"); return; } data.unpackMessage(msg); // tell object to treat inc data }
override protected void solveFull(NwkMessageFull message, int connID) { eNwkMessageType mtype = (eNwkMessageType)message.getIdCard().getMessageType(); switch (mtype) { case eNwkMessageType.CONNECTION: int broadcastedUid = int.Parse(message.header.getHeader()); if (broadcastedUid == nwkUid) { log("just received confirmation from server form local client connection | " + nwkUid + " VS " + broadcastedUid); //server asked, we sent the answer ... connection ready ? onNetworkLinkReady(); } else { log("just received info that client uid : " + broadcastedUid + " just connected"); } break; case eNwkMessageType.SYNC: //if match then it's mine, dont do anything with it //si y a modif d'un objet local par un autre client il faut passer par un msg type : SYNC_ONCE ! //Debug.Log(Time.frameCount + " | SYNC | " + message.getIdCard().getMessageSender() + " VS " + nwkUid); if (message.getIdCard().getMessageSender() != NwkClient.nwkUid) { Debug.Log(" --> not mine, applying nwk data to local context"); NwkSyncer.instance.applyMessage(message); } break; case eNwkMessageType.TICK: //Debug.Log(Time.frameCount+" received tick data"); float pingToMs = NwkClient.nwkClient.getModule <NwkModPing>().getCurrentPing(true); // return ms TickData td = (TickData)message.bytes.getObjectFromByteData(); getModule <NwkTick>().setupTick(td.tickRate, td.tick, td.tickRateTimer, pingToMs); break; case eNwkMessageType.NONE: break; default: throw new NotImplementedException("full not implem " + mtype); } }
protected override void solveFull(NwkMessageFull message, int connID) { log("msg " + message.getIdCard().toString(), message.isSilent()); eNwkMessageType mtype = (eNwkMessageType)message.getIdCard().getMessageType(); switch (mtype) { case eNwkMessageType.SYNC: //send new data to everybody //also specify sender to be able to filter on the other end sendWrapper.broadcastServerToAll(message, message.getIdCard().getMessageSender()); break; case eNwkMessageType.NONE: break; default: throw new NotImplementedException("full ; not implem " + mtype); } }
protected override void solveBasic(NwkMessageBasic message, int connID) { log("basic : " + message.getIdCard().toString(), message.isSilent()); NwkMessageBasic bMessage; eNwkMessageType mtype = (eNwkMessageType)message.getIdCard().getMessageType(); switch (mtype) { case eNwkMessageType.PING: // client sent ping //ref timestamp to solve timeout pingMessage(message.getIdCard().getMessageSender()); //setup pong message bMessage = new NwkMessageBasic(); bMessage.getIdCard().setMessageType(eNwkMessageType.PONG); //log("sending pong to " + connID + " , " + message.getIdCard().getMessageSender()); //Send pong message sendWrapper.sendToSpecificClient(bMessage, connID); break; case eNwkMessageType.DISCONNECTION: getClientData(message.getIdCard().getMessageSender()).setAsDisconnected(); //msg.clean(); break; case eNwkMessageType.TICK: //send tick data log("sending tick data to " + connID); NwkMessageFull mf = new NwkMessageFull(); mf.getIdCard().setupId(0, (int)eNwkMessageType.TICK); mf.bytes.setByteData(getModule <NwkTick>().data); sendWrapper.sendToSpecificClient(mf, connID); break; case eNwkMessageType.NONE: break; default: throw new NotImplementedException("base ; not implem " + mtype); } }
/// <summary> /// basic message for sync /// </summary> static public NwkMessageFull nwkSyncInject(NwkSyncableData syncData) { NwkMessageFull msg = new NwkMessageFull(); msg.getIdCard().setupId(NwkClient.nwkUid, (int)eNwkMessageType.SYNC); //header is body of message (not sender uid) string header = syncData.idCard.syncIID.ToString(); header += "-" + syncData.idCard.syncPID; msg.header.setupHeader(header); msg.bytes.setByteData(syncData.handle.pack()); return(msg); }
void solveMessageSize(NwkMessageFull msg) { if (msg == null) { return; } //solve size info if (msg.bytes.messageBytes.Length > 0) { int msgSize = msg.bytes.messageBytes.Length * 4; if (msgSize > 0) { int nUid = msg.getIdCard().getMessageSender(); //incoming message has sender ? if (nUid > 0) { getClientData(nUid).msgSizes.Add(msgSize); } } } }
void OnClientConnected(NetworkMessage clientConnectionMessage) { log("OnClientConnected ; sending ping pong transaction"); //server prepare message to ask for uid of newly connected client //to send only to new client server will use connectionId stored within origin conn message NwkMessageTransaction transMessage = new NwkMessageTransaction(); transMessage.getIdCard().setupId(0, (int)eNwkMessageType.CONNECTION_PINGPONG); //give message to listener system to plug a callback sendWrapper.sendTransaction(transMessage, clientConnectionMessage.conn.connectionId, delegate(NwkMessageTransaction waybackMessage) { // --- CALLBACK TRANSACTION log("received uid from client : " + waybackMessage.getIdCard().getMessageSender() + " , token ? " + waybackMessage.token); NwkClientData data = addClient(waybackMessage.getIdCard().getMessageSender(), clientConnectionMessage.conn.connectionId); // server ref new client in list data.setConnected(); // mark as connected //broadcast to all NwkMessageFull msg = new NwkMessageFull(); msg.getIdCard().setupId(0, (int)eNwkMessageType.CONNECTION); msg.header.setupHeader(waybackMessage.getIdCard().getMessageSender().ToString()); // msg will contain new client uid //send new client UID to everybody sendWrapper.broadcastServerToAll(msg, 0); // --- }); //log("asking to new client its uid"); //NwkUiView nView = qh.gc<NwkUiView>(); //if (nView != null) nView.onConnection(); }