public void SetRequest(IRequest request, IResponse response, Session session, Params parameters, Client client)
 {
     Request = request;
     Response = response;
     Session = session;
     Params = parameters;
     Client = client;
     Library = Library.Instance;
 }
예제 #2
0
 private void SendResponse(HttpRequest request, Client client)
 {
     var response = _configuration.Handler.Respond(request);
     //Respond with encoded data from handler
     //var message = Encoding.Default.GetBytes(response.ToString());
     //var messageAndBody = message.Concat(response.Body).ToArray();
     client.Socket.Send(response, 0, response.Length, SocketFlags.None);
     OnMessageSent(new MessageEventArgs()
     {
         Message = Encoding.Default.GetString(response, 0, response.Length),
         From = client.Socket.LocalEndPoint.ToString(),
         To = client.Socket.RemoteEndPoint.ToString(),
         Type = "response"
     });
 }
예제 #3
0
 private void OnConnectionLost(Client client)
 {
     ConnectionLost?.Invoke(this, new ConnectionEventArgs { LocalEndPoint = client.Socket.LocalEndPoint, RemoteEndPoint = client.Socket.RemoteEndPoint });
 }
예제 #4
0
 //Callbacks
 private void AcceptCallback(IAsyncResult result)
 {
     try
     {
         var clientSocket = _socket.EndAccept(result);
         var client = new Client { Buffer = new byte[clientSocket.ReceiveBufferSize], Socket = clientSocket };
         OnConnectionEstablished(client);
         client.Socket.BeginReceive(client.Buffer, 0, client.Buffer.Length, SocketFlags.None, ReceiveCallback, client);
         _socket.BeginAccept(AcceptCallback, null);
     }
     catch (ObjectDisposedException){}
     catch (Exception e)
     {
         Debug.WriteLine(e.Message);
     #if DEBUG
         throw;
     #endif
     }
 }
        private Client GetClient(HttpListenerContext httpListenerContext)
        {
            CookieCollection cookies = httpListenerContext.Request.Cookies;
            Client client = null;

            // Try to find a session with the cookie provided
            for (int i = 0; i < cookies.Count; i++)
            {
                Cookie cookie = cookies[i];

                if (cookie.Name == COOKIE_SESSION_ID)
                {
                    Guid cookieSessionId = Guid.Empty;
                    if (Guid.TryParse(cookie.Value, out cookieSessionId))
                    {
                        for (int j = 0; j < Server.Clients.Count; j++)
                        {
                            Client serverClients = Server.Clients[j];
                            if (serverClients.ID == cookieSessionId)
                            {
                                client = serverClients;
                                break;
                            }
                        }
                    }

                    break;
                }
            }

            //If no client found, create one.
            if (client == null)
            {
                client = new Client(Guid.NewGuid());
                Server.Clients.Add(client);
                client.Dictionary.Add("index", Server.Clients.IndexOf(client));
                httpListenerContext.Response.SetCookie(new Cookie(COOKIE_SESSION_ID, client.ID.ToString()) { Expires = DateTime.Now.AddHours(Server.SESSION_EXPIRE_TIME) });
            }

            return client;
        }