private void OnPeerCachePeerExpired(PhotonHttpPeer httpPeer)
 {
     if (log.IsDebugEnabled)
     {
         log.DebugFormat("Peer expired: id={0}", new object[] { httpPeer.PeerId });
     }
     this.photonApplication.OnDisconnect(httpPeer, httpPeer.GetUserData(), DisconnectReason.ManagedDisconnect, string.Empty, 0, 0, 0);
 }
 private void ExecuteOnReceive(PhotonHttpPeer peer, byte[] data, int?invocationId)
 {
     if (log.IsDebugEnabled)
     {
         log.DebugFormat("Execute request: peerId={0}, invocationId={1}", new object[] { peer.PeerId, invocationId });
     }
     this.photonApplication.OnReceive(peer, peer.GetUserData(), data, MessageReliablity.Reliable, 0, MessageContentType.Binary, 0, 0, 0);
 }
Example #3
0
 public bool TryGetPeer(HttpContext context, string connectionId, out PhotonHttpPeer peer)
 {
     using (ReadLock.Enter(this.readerWriterLock))
     {
         peer = context.Cache.Get(connectionId) as PhotonHttpPeer;
     }
     return(peer != null);
 }
 private void OnInitRequest(RequestState request)
 {
     try
     {
         Guid   guid   = Guid.NewGuid();
         string peerId = guid.ToString();
         if ((request.Context.Request.QueryString.Count < 1) || (request.Context.Request.QueryString[0] != "init"))
         {
             if (log.IsDebugEnabled)
             {
                 log.DebugFormat("Received empty peer id", new object[0]);
             }
             RpcHttpHelper.SendErrorResponse(request.Context.Response, "Invalid rpc request");
             request.AsyncResult.SetCompleted(null, false);
         }
         else
         {
             if (log.IsDebugEnabled)
             {
                 log.DebugFormat("Received init request: pid={0}", new object[] { peerId });
             }
             PhotonHttpPeer photonPeer = new PhotonHttpPeer(peerId, this, request.Context);
             if (!this.peerCache.TryAddPeer(photonPeer))
             {
                 if (log.IsWarnEnabled)
                 {
                     log.WarnFormat("Failed to create peer: pid={0}", new object[] { peerId });
                 }
                 RpcHttpHelper.SendServerErrorResponse(request.Context.Response);
             }
             else
             {
                 byte[] data = RpcHttpHelper.ReadRequest(request.Context.Request);
                 this.photonApplication.OnInit(photonPeer, data, 1);
                 if (photonPeer.Queue.Count == 0)
                 {
                     request.AsyncResult.SetCompleted(null, false);
                 }
                 else
                 {
                     byte[] buffer = photonPeer.Queue.Dequeue();
                     request.Context.Response.OutputStream.Write(guid.ToByteArray(), 0, 0x10);
                     request.Context.Response.OutputStream.Write(buffer, 0, buffer.Length);
                     request.AsyncResult.SetCompleted(null, false);
                 }
             }
         }
     }
     catch (Exception exception)
     {
         log.Error(exception);
     }
 }
Example #5
0
 public bool TryAddPeer(PhotonHttpPeer photonPeer)
 {
     using (WriteLock.Enter(this.readerWriterLock))
     {
         if (HttpRuntime.Cache.Get(photonPeer.PeerId) is PhotonHttpPeer)
         {
             return(false);
         }
         HttpRuntime.Cache.Add(photonPeer.PeerId, photonPeer, null, Cache.NoAbsoluteExpiration, this.PeerExpiration, CacheItemPriority.Normal, new CacheItemRemovedCallback(this.OnPeerRemoveCallBack));
         return(true);
     }
 }
        private void SendQueuedMessages(PhotonHttpPeer peer, RequestState request)
        {
            List <byte[]> list = peer.DequeueAll();

            if (list.Count > 0)
            {
                using (BinaryWriter writer = new BinaryWriter(request.Context.Response.OutputStream))
                {
                    writer.Write((short)list.Count);
                    foreach (byte[] buffer in list)
                    {
                        writer.Write(buffer.Length);
                        writer.Write(buffer, 0, buffer.Length);
                    }
                    writer.Flush();
                }
            }
        }
Example #7
0
        private void OnPeerRemoveCallBack(string key, object value, CacheItemRemovedReason reason)
        {
            PhotonHttpPeer peer = (PhotonHttpPeer)value;

            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Removed peer: id={0}, reason={1}", new object[] { peer.GetConnectionID(), reason });
            }
            if ((reason == CacheItemRemovedReason.Expired) || (reason == CacheItemRemovedReason.Underused))
            {
                Interlocked.Decrement(ref currentConnectionCount);
                Action <PhotonHttpPeer> onPeerExpired = this.OnPeerExpired;
                if (onPeerExpired != null)
                {
                    onPeerExpired(peer);
                }
            }
        }
 internal void DisconnectPeer(PhotonHttpPeer httpPeer)
 {
     this.photonApplication.OnDisconnect(httpPeer, httpPeer.GetUserData(), DisconnectReason.ManagedDisconnect, string.Empty, 0, 0, 0);
     this.peerCache.RemovePeer(httpPeer.PeerId);
 }