-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOBasePackHandler.cs
More file actions
129 lines (112 loc) · 4.61 KB
/
IOBasePackHandler.cs
File metadata and controls
129 lines (112 loc) · 4.61 KB
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
using System;
namespace NETLIB
{
/// <summary>
/// Better manage the incoming and outgoing <see cref="BasePack"/> using a <see cref="Protocol{TPack}"/>
/// to redistribute the packs. It has an internal dictionary of <see cref="Protocol{TPack}"/>
/// that can be exchanged for the currently used.
/// </summary>
/// <seealso cref="IOPackHandler{TPack}"/>
/// <seealso cref="Consumer{TPack}"/>
/// <seealso cref="BasePack"/>
/// <seealso cref="Protocol{TPack}"/>
/// <example>
/// The following example shows how to implement a chat client using <see cref="IOBasePackHandler"/> to connect
/// to the server and manage packages.
/// <code>
///using NETLIB;
///using NETLIB.TCP;
///using System;
///
///namespace ChatExempleClient
///{
/// class Program
/// {
/// static IOBasePackHandler client;
/// static Protocol<BasePack> chatProtocol;
/// static string name;
///
/// static void Main(string[] args)
/// {
/// chatProtocol = new Protocol<BasePack>("chatProtocol");
/// chatProtocol[0] += MessagePackHandle;
///
/// client = new IOBasePackHandler(new TCPPublisher("127.0.0.1", 1975), chatProtocol);
/// client.Start();
///
/// Console.WriteLine("Your name please:");
/// name = Console.ReadLine();
///
/// string aux = Console.ReadLine();
/// while (aux != "exit")
/// {
/// var pack = new BasePack();
/// pack.ID = 0;
/// pack.PutString(name + ": " + aux);
/// client.SendPack(pack);
/// aux = Console.ReadLine();
/// }
///
/// client.CloseConnection();
/// }
///
/// private static void MessagePackHandle(Consumer<BasePack> consumer, BasePack receivedPack)
/// {
/// Console.WriteLine(receivedPack.GetString());
/// }
/// }
///}
/// </code>
/// </example>
public class IOBasePackHandler : IOPackHandler<BasePack>
{
#region Variables
#endregion
#region Constructor
/// <summary>
/// Initializes the handler with a publisher who will publish the
/// packages and a protocol that will be used initially by this connection.
/// </summary>
/// <param name="publisher">Publisher who will publish the packages to be managed.</param>
/// <param name="inicialProtocol">Initial <see cref="Protocol{TPack}"/> to be used by this connection.</param>
/// <seealso cref="IOPackHandler{TPack}"/>
/// <seealso cref="Consumer{TPack}"/>
/// <seealso cref="BasePack"/>
/// <seealso cref="Protocol{TPack}"/>
public IOBasePackHandler(Publisher publisher, Protocol<BasePack> inicialProtocol) : base(publisher, inicialProtocol) { }
#endregion
#region Attributes
#endregion
#region Methods
/// <summary>
/// Used by the <see cref="Consumer{TPack}.Consume"/> to obtain an instance of the <see cref="BasePack"/>
/// through <see cref="BasePack(BasePack, bool)"/> constructor.
/// </summary>
/// <param name="pack"><see cref="BasePack"/> to be based on.</param>
/// <returns>New <see cref="BasePack"/> based on <paramref name="pack"/></returns>
public override BasePack PackFactory(BasePack pack)
{
return pack;
}
/// <summary>
/// Used by the <see cref="Consumer{TPack}.Consume"/> to obtain an instance of the <see cref="BasePack"/>
/// through <see cref="BasePack(byte[], bool)"/> constructor.
/// </summary>
/// <param name="pack"><see cref="BasePack"/> to be based on.</param>
/// <returns>New <see cref="BasePack"/> based on <paramref name="pack"/></returns>
public override BasePack PackFactory(byte[] pack)
{
return pack;
}
/// <summary>
/// Used by the <see cref="Consumer{TPack}.Consume"/> to obtain an instance of the <see cref="BasePack"/>
/// through <see cref="BasePack(bool)"/> constructor.
/// </summary>
/// <returns>New <see cref="BasePack"/>.</returns>
public override BasePack PackFactory()
{
return new BasePack();
}
#endregion
}
}