コード例 #1
0
        public void asynFindingClient(TcpListener listener, string password, Action <ClientInfo> allocToConnectClient, Action <Exception> allocToCrashed)
        {
            threads.Add(StartProtectedThread(Find));

            void Find()
            {
                try
                {
                    Searching();
                }
                catch (ThreadAbortException e)
                {
                    if (e != null)
                    {
                        e = null;
                    }
                }
                catch (Exception e)
                {
                    lock (locker)
                    {
                        allocToCrashed(e);
                    }
                }
            }

            void Searching()
            {
                while (isWork)
                {
                    TcpClient clientTCP = listener.AcceptTcpClient();

                    NetworkStream stream = clientTCP.GetStream();
                    stream.ReadTimeout = 1000;

                    byte[] buffer = new byte[40];
                    try
                    {
                        stream.Read(buffer, 0, buffer.Length);
                    }
                    catch
                    {
                        continue;
                    }


                    byte[] clearBuffer = buffer.ToList().GetRange(2, buffer.Length - 2).ToArray();
                    string data        = Encoding.UTF8.GetString(clearBuffer);
                    var    info        = data.Split(' ');

                    if (CheckingOnFalseData(info))
                    {
                        clientTCP.Close();
                        stream.Close();
                        continue;
                    }

                    ClientInfo client = new ClientInfo(info[0], info[1], clientTCP);

                    lock (locker)
                    {
                        allocToConnectClient(client);
                    }
                }

                bool CheckingOnFalseData(string[] data)
                {
                    if (data.Length >= 2 && password != "" && data[1].CompareTo(password) == 0)
                    {
                        return(false);
                    }
                    else if (password == "" && data.Length > 0)
                    {
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
        }
コード例 #2
0
        public void asynListeningClient(ClientInfo client, Action <ClientInfo, byte[]> allocClientSendMessageFromServer, Action <ClientInfo, Exception> allocToCrashed)
        {
            threads.Add(StartProtectedThread(Listen));

            void Listen()
            {
                try
                {
                    while (isWork)
                    {
                        Listening();
                    }
                }
                catch (ThreadAbortException e)
                {
                    if (e != null)
                    {
                        e = null;
                    }
                }
                catch (Exception e)
                {
                    lock (locker)
                    {
                        allocToCrashed(client, e);
                    }
                }
            }

            void Listening()
            {
                NetworkStream stream = client.ClientTCP.GetStream();

                stream.ReadTimeout = Timeout.Infinite;

                List <byte> resutBuffer = new List <byte>();

                byte[] buffer = ReadStreemBuffer(stream, 2, resutBuffer);

                if (buffer[1] > 0)
                {
                    buffer = ReadStreemBuffer(stream, buffer[1], resutBuffer);
                }

                buffer = resutBuffer.ToArray();

                lock (locker)
                {
                    allocClientSendMessageFromServer(client, buffer);
                }

                Thread.Sleep(TickRate);
            }

            byte[] ReadStreemBuffer(NetworkStream stream, int countByte, List <byte> resutBuffer)
            {
                byte[] buffer = new byte[countByte];
                stream.Read(buffer, 0, buffer.Length);

                resutBuffer.AddRange(buffer);
                return(buffer);
            }
        }
コード例 #3
0
 private void allocSendedMessageFromClient(ClientInfo client, MessageDescript descript)
 {
 }