2015-12-09 17:57:45 +00:00
|
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using Microsoft.DotNet.ProjectModel.Server.Models;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.ProjectModel.Server
|
|
|
|
|
{
|
|
|
|
|
internal class ConnectionContext
|
|
|
|
|
{
|
|
|
|
|
private readonly string _hostName;
|
|
|
|
|
private readonly ProcessingQueue _queue;
|
2016-01-02 08:44:59 +00:00
|
|
|
|
private readonly IDictionary<int, ProjectManager> _projects;
|
2015-12-09 17:57:45 +00:00
|
|
|
|
|
|
|
|
|
public ConnectionContext(Socket acceptedSocket,
|
|
|
|
|
string hostName,
|
|
|
|
|
ProtocolManager protocolManager,
|
|
|
|
|
WorkspaceContext workspaceContext,
|
2016-01-02 08:44:59 +00:00
|
|
|
|
IDictionary<int, ProjectManager> projects,
|
2015-12-09 17:57:45 +00:00
|
|
|
|
ILoggerFactory loggerFactory)
|
|
|
|
|
{
|
|
|
|
|
_hostName = hostName;
|
2016-01-02 08:44:59 +00:00
|
|
|
|
_projects = projects;
|
2015-12-09 17:57:45 +00:00
|
|
|
|
|
|
|
|
|
_queue = new ProcessingQueue(new NetworkStream(acceptedSocket), loggerFactory);
|
|
|
|
|
_queue.OnReceive += message =>
|
|
|
|
|
{
|
|
|
|
|
if (protocolManager.IsProtocolNegotiation(message))
|
|
|
|
|
{
|
|
|
|
|
message.Sender = this;
|
|
|
|
|
protocolManager.Negotiate(message);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
message.Sender = this;
|
2016-01-02 08:44:59 +00:00
|
|
|
|
ProjectManager projectManager;
|
|
|
|
|
if (!_projects.TryGetValue(message.ContextId, out projectManager))
|
2015-12-09 17:57:45 +00:00
|
|
|
|
{
|
2016-01-02 08:44:59 +00:00
|
|
|
|
projectManager = new ProjectManager(message.ContextId,
|
2015-12-09 17:57:45 +00:00
|
|
|
|
loggerFactory,
|
|
|
|
|
workspaceContext,
|
|
|
|
|
protocolManager);
|
|
|
|
|
|
2016-01-02 08:44:59 +00:00
|
|
|
|
_projects[message.ContextId] = projectManager;
|
2015-12-09 17:57:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-02 08:44:59 +00:00
|
|
|
|
projectManager.OnReceive(message);
|
2015-12-09 17:57:45 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void QueueStart()
|
|
|
|
|
{
|
|
|
|
|
_queue.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Transmit(Message message)
|
|
|
|
|
{
|
|
|
|
|
message.HostId = _hostName;
|
|
|
|
|
return _queue.Send(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|