public void Reply() { if (Done) { throw Dj.Crash("Can only REPLY or REJECT one time on a PostAssistant"); } message.Respond(Capn.Crunchatize(reply), ResponseStatus.Success); Done = true; }
public PostAssistant(IIncommingMessage message) { this.message = message; try { this.action = Capn.Decrunchatize <ACTION>(message.AsBytes()); } catch (System.InvalidCastException ex) { Dj.Errorf("Still gonna crash, but just a heads up: expecting " + typeof(ACTION).ToString() + ", not getting it."); throw ex; } this.reply = new REPLY(); this.Done = false; }
public void Post(int peerId, short opCode, object serializableObject) { IPeer peer = master.GetPeer(peerId); if (peer == null) { Dj.Errorf("Post to peer#{0} failed", peerId); } else { peer.SendMessage(opCode, Capn.Crunchatize(serializableObject)); } }
public IMessage Post <ReplyType>(short opCode, object serializableObject, System.Action <ReplyType> successCallback, System.Action <ResponseStatus> nonSuccessCallback = null) { return(Post(opCode, serializableObject, (status, response) => { if (status == ResponseStatus.Success) { successCallback(Capn.Decrunchatize <ReplyType>(response.AsBytes())); } else if (nonSuccessCallback != null) { nonSuccessCallback(status); } })); }
public Story Decode(ServeStory serveAction) { Pages pages = Capn.Decrunchatize <Pages>(serveAction.storyBytes); foreach (var decoder in decoders) { var story = decoder.Decode(pages); if (story != null) { return(story); } } throw Dj.Crashf("Decode failed on Story with unknown OPCODE '{0}' at address '{1}'. (OPCODE was not caught by any pushed IStorydecoder)", pages.op, pages.address); }
public string CreateWorldBackup() { var sanitizedData = this.worldData.Clone(); foreach (var sessionKVP in sessionToTowerEntities) { sanitizedData.entitiesById.Remove(sessionKVP.Value.EntityId); } var backupBytes = Capn.Crunchatize(sanitizedData); string backupName = "lastbackup"; File.WriteAllBytes(Application.streamingAssetsPath + "/" + backupName, backupBytes); return(backupName); }
////constructor public CModelPlaysession(ClientsideLink link, System.Action <Story <SessionPage> > RequestNewSessionPages, System.Action <Story <PlayerPage> > ShowNewPlayer) : base(link) { this.RequestNewSessionPages = RequestNewSessionPages; this.ShowNewPlayer = ShowNewPlayer; this.link.OnDisconnected += () => { ServersideSetPlayer(null); // TODO: automatically handle reconnection attempts and session dropping. }; this.link.SetPostHandler( bc.PlayerDelta.op, message => { var action = Capn.Decrunchatize <bc.PlayerDelta>(message.AsBytes()); if (this.myPlayer != null) { this.myPlayer.ListenDelta(action.idelta); this.myPlayer.ApplyAllPendingDeltas(); } } ); }
public IMessage Post(short opCode, object serializableObject, ResponseCallback responseCallback) { IMessage message = MessageHelper.Create(opCode, Capn.Crunchatize(serializableObject)); if (IsConnected) { Peer.SendMessage( message, responseCallback, 3, DeliveryMethod.Reliable ); } else { Dj.Warn("Post to server failed (Not connected)"); message.Status = ResponseStatus.NotConnected; } // Dj.Temp("Message status = "+message.Status); return(message); }
public byte[] ToBytes() { return(Capn.Crunchatize(this.pages)); }
public void Set <T>(string key, T value) { this.pages.data[key] = Capn.Crunchatize(value); }
public T Get <T>(string key) { return(Capn.Decrunchatize <T>(this.pages.data[key])); }
private void Start() { string s = "hello"; Dj.Temp("Begin size test"); byte[] b = Capn.Crunchatize(s); for (var i = 0; i < 5; i++) { Dj.Tempf("Iter #{0}, size {1}", i, b.Length); b = Capn.Crunchatize(b); } backupTimer = backupFreqInSeconds; worldData = new WorldData(true); link = GetComponent <ServersideLink>(); sessions = new ServersideSessions(link); sessionToTowerEntities = new Dictionary <string, TowerEntity>(); entGenerator = new EntGenerator(); decoder = new TowerStoryDecoder(); sessions.SetFunctionToAddStoryListeners <TowerEntity>(TowerEntity.OPCODE, this.GetWhoCanSeeMe_UpdateTracking); sessions.SetFunctionToAddStoryListeners <TowerZone>(TowerZone.OPCODE, this.GetWhosListeningHere); sessions.storyteller.AddStoryfan(Session.OPCODE, new LambdaStoryfan <Session>(session => { if (sessionToTowerEntities.ContainsKey(session.address)) { if (session.PeerId < 0) { // you're disconnected but your body is still in the world. // bye now! } } else { if (session.PeerId >= 0) { // you're connected but don't have a body. let's remedy that! // int EntityId = (nextTowerEntityId++); var playerTowerEntity = entGenerator.NewPlayerEntity(); //playerTowerEntity.EntityId = EntityId; //playerTowerEntity.WorldPos = twin.zero; //playerTowerEntity.Position = twin.zero; session.EntityId = playerTowerEntity.EntityId; sessionToTowerEntities[session.address] = playerTowerEntity; session.WriteChanges(); sessions.storyteller.Write(playerTowerEntity); // suddenly, you're embodied } } } )); link.SetPostHandler <RequestStories, OK>(RequestStories.OPCODE, poster => { if (sessions.UsingPeer(poster.Peer.Id, out var session)) { Dj.Tempf("Server heard request from client"); poster.Reply(); PushStoriesToSession(session); } }); link.SetPostHandler <WorldMove, OK>(WorldMove.OPCODE, poster => { try { sessions.UsingPeer(poster.Peer.Id, out var session); var ent = sessionToTowerEntities[session.address]; ent.WorldPos += poster.action.dir; ent.WriteChanges(); poster.Reply(); PushEntitiesToSession(session); } catch { poster.Reject(Barebones.Networking.ResponseStatus.Failed); } }); SetupStartingZones(); }
public void LoadFromWorldBackup(string backupName) { var backupBytes = File.ReadAllBytes(Application.streamingAssetsPath + "/" + backupName); this.worldData = Capn.Decrunchatize <WorldData>(backupBytes); }
public void SetPostHandler <BC>(short opCode, System.Action <BC> HandleBroadcast) where BC : struct { SetPostHandler(opCode, message => { HandleBroadcast(Capn.Decrunchatize <BC>(message.AsBytes())); }); }
public void LINKOPS_SetPostHandlers() { this.link.SetPostHandler(post.SessionLogin.op, message => { var login = Capn.Decrunchatize <post.SessionLogin>(message.AsBytes()); var reply = new post.SessionLogin_Reply(); if (login.playerid > 0 && login.sessionkey != default(string)) { // try login via userid x sessionkey var session = sessionsTeller.GetStory(login.playerid); if (session != null && session.GetPage().sessionkey == login.sessionkey && !session.GetPage().ServerOnly_IsExpired()) { Story <PlayerPage> player; if (BANG_Login(session, message.Peer)) { reply.okay = true; reply.sessionPage = session.GetPage(); } else { reply.bad_credentials = true; // idk } } else { reply.bad_credentials = true; } } else if (login.username.Length > 0 && login.hashpass.Length > 0) { // try login via username x hashpass int dbuserid; string dbhashpass; if (userdb.TryGetUsernameDetails(ref login.username, out dbuserid, out dbhashpass) && (login.hashpass == dbhashpass || dbhashpass == "*")) { Story <SessionPage> session; Story <PlayerPage> player; if (BANG_Login(dbuserid, login.username, message.Peer, out session)) { reply.okay = true; reply.sessionPage = session.GetPage(); } else { // login failed } } else { // user does not exist. login failed. } if (!reply.okay) { reply.bad_credentials = true; } } else { reply.bad_params = true; } message.Respond(Capn.Crunchatize(reply), ResponseStatus.Success); }); this.link.SetPostHandler(post.SessionLogout.op, message => { // var action = Capn.Decrunchatize<post.SessionLogout>(message.AsBytes()); var reply = new post.SessionLogout_Reply(); reply.okay = BANG_LogoutPeer(message.Peer); message.Respond(Capn.Crunchatize(reply), ResponseStatus.Success); }); this.link.SetPostHandler(post.SessionRefresh.op, message => { // var action = Capn.Decrunchatize<post.SessionRefresh>(message.AsBytes()); var reply = new post.SessionRefresh_Reply(); var story = GetSession(message.Peer); if (story != null) { reply.okay = true; reply.session = story.GetPage(); } message.Respond(Capn.Crunchatize(reply), ResponseStatus.Success); }); }