blob: 91a2241902ca6f9b73b0bf93e57f5913bc2a4420 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
using System;
using Hik.Communication.Scs.Communication;
using Hik.Communication.Scs.Communication.EndPoints.Tcp;
using Hik.Communication.ScsServices.Client;
using Hik.Communication.ScsServices.Communication.Messages;
using Hik.Samples.Scs.IrcChat.Arguments;
using Hik.Samples.Scs.IrcChat.Contracts;
namespace Hik.Samples.Scs.IrcChat.Client
{
/// <summary>
/// This class is a mediator with view and SCS system.
/// </summary>
internal class ChatController : IChatController
{
#region Private fields
/// <summary>
/// Reference to login form.
/// </summary>
public ILoginFormView LoginForm { get; set; }
/// <summary>
/// Reference to chat room window.
/// </summary>
public IChatRoomView ChatRoom { get; set; }
/// <summary>
/// The object which handles remote method calls from server.
/// It implements IChatClient contract.
/// </summary>
private ChatClient _chatClient;
/// <summary>
/// This object is used to connect to SCS Chat Service as a client.
/// </summary>
private IScsServiceClient<IChatService> _scsClient;
#endregion
#region IChatController implementation
/// <summary>
/// Connects to the server.
/// It automatically Logins to server if connection success.
/// </summary>
public void Connect()
{
//Disconnect if currently connected
Disconnect();
//Create a ChatClient to handle remote method invocations by server
_chatClient = new ChatClient(ChatRoom);
//Create a SCS client to connect to SCS server
_scsClient = ScsServiceClientBuilder.CreateClient<IChatService>(new ScsTcpEndPoint(LoginForm.ServerIpAddress, LoginForm.ServerTcpPort), _chatClient);
//Register events of SCS client
_scsClient.Connected += ScsClient_Connected;
_scsClient.Disconnected += ScsClient_Disconnected;
//Connect to the server
_scsClient.Connect();
}
/// <summary>
/// Disconnects from server if it is connected.
/// </summary>
public void Disconnect()
{
if (_scsClient != null && _scsClient.CommunicationState == CommunicationStates.Connected)
{
try
{
_scsClient.Disconnect();
}
catch
{
}
_scsClient = null;
}
}
/// <summary>
/// Sends a public message to room.
/// It will be seen by all users in room.
/// </summary>
/// <param name="message">Message to be sent</param>
public void SendMessageToRoom(ChatMessage message)
{
_scsClient.ServiceProxy.SendMessageToRoom(message);
}
/// <summary>
/// Change status of user.
/// </summary>
/// <param name="newStatus">New status</param>
public void ChangeStatus(UserStatus newStatus)
{
_scsClient.ServiceProxy.ChangeStatus(newStatus);
}
/// <summary>
/// Sends a private message to a user.
/// </summary>
/// <param name="nick">Destination nick</param>
/// <param name="message">Message</param>
public void SendPrivateMessage(string nick, ChatMessage message)
{
_scsClient.ServiceProxy.SendPrivateMessage(nick, message);
}
#endregion
#region Private methods
/// <summary>
/// This method handles Connected event of _scsClient.
/// </summary>
/// <param name="sender">Source of event</param>
/// <param name="e">Event arguments</param>
private void ScsClient_Connected(object sender, EventArgs e)
{
try
{
_scsClient.ServiceProxy.Login(LoginForm.CurrentUserInfo);
ChatRoom.OnLoggedIn();
}
catch (ScsRemoteException ex)
{
Disconnect();
ChatRoom.OnLoginError(ex.InnerException != null ? ex.InnerException.Message : ex.Message);
}
catch
{
Disconnect();
ChatRoom.OnLoginError("Can not login to server. Please try again later.");
}
}
/// <summary>
/// This method handles Disconnected event of _scsClient.
/// </summary>
/// <param name="sender">Source of event</param>
/// <param name="e">Event arguments</param>
private void ScsClient_Disconnected(object sender, EventArgs e)
{
ChatRoom.OnLoggedOut();
}
#endregion
}
}
|