public Client(IPAddress iPAddress, int portNum) { tcpClient = new TcpClient(); try { tcpClient.Connect(iPAddress, portNum); SharedStateObj = new ClientSharedStateObject() { ContinueProcess = true, Ev = new AutoResetEvent(false), InBoundMessageQueue = new Queue <BaseMessage>(), OutBoundMessageQueue = new Queue <BaseMessage>() }; handlerDictionary = new Dictionary <Type, List <BaseMessageHandler> >(); #region Building message handling dictionary var packageHolder = ConfigurationManager.AppSettings["RulesPackages"].Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList(); packageHolder.Add("NetworkingCore"); List <Type> typeHolder = new List <Type>(); foreach (var currentPackage in packageHolder) { typeHolder.AddRange(Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + currentPackage + @".dll").GetTypes().ToList()); } List <BaseMessageHandler> handlerHolder = new List <BaseMessageHandler>(); foreach (var item in typeHolder) { if (item.BaseType != null && item.BaseType.Equals(typeof(BaseMessageHandler)) && !item.GetConstructor(Type.EmptyTypes).Equals(null)) { handlerHolder.Add((BaseMessageHandler)Activator.CreateInstance(item)); } } foreach (var item in handlerHolder) { if (!handlerDictionary.Keys.Contains(item.HandledMessageType)) { handlerDictionary.Add(item.HandledMessageType, new List <BaseMessageHandler>()); } handlerDictionary[item.HandledMessageType].Add(item); } #endregion Listener listener = new Listener(tcpClient); ThreadPool.QueueUserWorkItem(new WaitCallback(listener.Listen), SharedStateObj); MessageSender messageSender = new MessageSender(tcpClient); ThreadPool.QueueUserWorkItem(new WaitCallback(messageSender.SendMessages), SharedStateObj); } catch (SocketException e) { Console.WriteLine("Server not available!"); } catch (IOException e) { Console.WriteLine("Server not available!"); } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public bool SendBytes(byte[] bytes) { return(MessageSender.Send(this, bytes)); }