Witam,
po raz pierwszy zabrałem się za zaprojektowanie architektury klient-server i na starcie pojawił się problem opisany poniżej:
Posiadam bibliotekę z klasą servera, aplikację okienkową hosta, gdzie również chciałem pokazywać listę zalogowanych klientów.
Posiadam aplikację konsolową dla klientów.
Aby umożniwić podgląd klientów w aplikacji hosta po utworzeniu niezbędnych obiektów utworzyłem również obiekt servera, którego odpytuję w pętli o stan zalogowanych użytkowników, i updateuje kontrolki. Niestety niezależnie od ich liczby, host zawsze pokazuje 0 (na klientach te same metody zwracają poprawne wartości). Przypuszczam, że posiadam pewnie jakaś sprzeczność założeń z tworzeniem obiektu servera po stronie hosta. Prośba o nakierowanie na dobrą drogę.
public partial class Form1 : Form
{
private STServerState _ServerState;
private HttpChannel _HTTPC;
private STServer.STServer _Server;
public Form1()
{
InitializeComponent();
_ServerState = STServerState.Stopped;
InitializeHost();
}
private void serverStateCaption_Click(object sender, EventArgs e)
{
if (_ServerState == STServerState.Stopped)
{
timer1.Enabled = true;
_ServerState = STServerState.Running;
serverStateCaption.Text = "Stop server";
}
else
{
timer1.Enabled = false;
_ServerState = STServerState.Stopped;
serverStateCaption.Text = "Start server";
}
}
private void Run(object sender, EventArgs e)//metoda wywoływana co 2 sekundy przez obiekt klasy Timer
{
LoadClients();
}
private void InitializeHost()
{
_HTTPC = new HttpChannel(3200);
ChannelServices.RegisterChannel(_HTTPC, false);
Type serverType = typeof(STServer.STServer);
RemotingConfiguration.RegisterWellKnownServiceType(serverType, "MyObject", WellKnownObjectMode.Singleton);
_Server = new STServer.STServer();
}
private void LoadClients()
{
label1.Text = _Server.GetAgentList().Length.ToString();
label2.Text = _Server.GetActorList().Length.ToString();
}
}
enum STServerState
{
Running,
Stopped,
}
Kod hosta powyżej, kod servera poniżej.
public enum STClientType : byte
{
Actor,
Agent,
}
public enum STAgentState
{
Active,
Disabled,
StandBy,
}
public class STServer : MarshalByRefObject
{
private List<STClient> _ClientList;
private int _AvailablClientId;
public STServer()
{
_AvailablClientId = 1;
_ClientList = new List<STClient>();
}
public int Login(STClientType clientType, string pcId, string userId)
{
_ClientList.Add(new STClient(_AvailablClientId++, pcId, userId, clientType));
return _AvailablClientId;
}
public void Logout(int clientId)
{
for (int i = 0; i < _ClientList.Count; i++)
{
if (_ClientList[i].ClientId == clientId) _ClientList.Remove(_ClientList[i]);
}
}
public int[] GetAgentList()
{
List<int> agentList = new List<int>();
foreach (STClient cl in _ClientList)
{
if (cl.ClientType == STClientType.Agent) agentList.Add(cl.ClientId);
}
return agentList.ToArray();
}
public int[] GetActorList()
{
List<int> actorList = new List<int>();
foreach (STClient cl in _ClientList)
{
if (cl.ClientType == STClientType.Actor) actorList.Add(cl.ClientId);
}
return actorList.ToArray();
}
}