/* The MIT License Copyright (c) 2008 Ferreri Alessio Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using TCPLibrary.MessageBased.Core; using TCPLibrary.Core; using System; namespace TCPLibrary.MessageBased.Core { /// /// TCP Client Class that work with Message structures. /// public class BaseMessageClient : Client { /// /// Delegate for the event of receiving a message structure from the server. /// /// Sender of the event. /// Message received. public delegate void MessageReceivedHandler(Client sender, TCPMessage Mess); /// /// Occurs when the client receive a message structure from the server. /// public event MessageReceivedHandler OnMessageReceived; /// /// Delegate for the event of sending a message structure to the server. /// /// Sender of the event. /// Message sent. public delegate void MessageSentHandler(Client sender, TCPMessage Mess); /// /// Occurs before the client send a message structure to the server. /// public event MessageSentHandler OnBeforeMessageSent; /// /// Occurs after the client send a message structure to the server. /// public event MessageSentHandler OnAfterMessageSent; /// /// Delegate for the event of connection fail for max users number reached. /// /// Sender of the event. public delegate void MaxUsersReached(Client sender); /// /// Occurs when the connection fail as the server reached the maximum number of clients allowed. /// public event MaxUsersReached OnMaxUsersConnectionFail; /// /// Base constructor of the class. /// public BaseMessageClient() { OnDataReceived += new DataCommunicationHandler(BaseMessageClient_OnDataReceived); OnAfterDataSent += new DataCommunicationHandler(BaseMessageClient_OnDataSent); OnConnectFailed += new ConnectionFailedHandler(BaseMessageClient_OnConnectFailed); } /// /// When the connection is rejected by the server raise the correct event. /// /// Sender of the event. /// Message of the server. void BaseMessageClient_OnConnectFailed(Client sender, byte[] Message) { if (TCPLibrary.MessageBased.Core.TCPMessage.FromByteArray(Message).MessageType == MessageType.MaxUsers) if (OnMaxUsersConnectionFail != null) OnMaxUsersConnectionFail(sender); } /// /// Parse the raw data sent to the server and create Message structures. /// /// Sender of the event. /// Line of data sent. void BaseMessageClient_OnDataSent(Client sender, Data Data) { TCPMessage msg = TCPMessage.FromByteArray(Data.Message); if (OnAfterMessageSent != null) OnAfterMessageSent(sender, msg); } /// /// Parse the raw data received from the server and create Message structures. /// /// Sender of the event. /// Line of data received. void BaseMessageClient_OnDataReceived(Client sender, Data Data) { TCPMessage msg = null; try { msg = TCPMessage.FromByteArray(Data.Message); } catch (Exception) { } if (msg != null) if (OnMessageReceived != null) OnMessageReceived(sender, msg); } /// /// Send a message structure to the server. /// /// Message structure to be send. /// public void Send(TCPMessage msg) { if (OnBeforeMessageSent != null) OnBeforeMessageSent(this, msg); base.Send(new Data(msg.ToByteArray())); } } }