Пример #1
0
        public override void Write(NetData data)
        {
            if (stream == null)
            {
                return;
            }

            stream.Write(data.ToArray(), 0, data.DataLength);
        }
Пример #2
0
        public override void Run(int port)
        {
            if (client != null)
            {
                return;
            }

            Running = true;

            try
            {
                Log("Connecting client...");
                client = new TcpClient("127.0.0.1", port);

                Log("Connected!");
                stream = client.GetStream();

                UponConnected?.Invoke();

                int count;
                while (Running && client.Connected)
                {
                    count = stream.Read(inBuffer, 0, inBuffer.Length);

                    if (count == 0)
                    {
                        Log("Read message of length 0, quitting read.");
                        break;
                    }

                    NetData inData = new NetData(inBuffer, count);
                    UponMessage?.Invoke(inData);
                }
            }
            catch (Exception e)
            {
                Log(e.ToString());
            }
            finally
            {
                Log("Shutting down...");

                if (client != null)
                {
                    client.Close();
                    client = null;
                }
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                    stream = null;
                }

                Running = false;
            }
        }
Пример #3
0
        public override void Write(NetData data)
        {
            if (stream == null)
            {
                Log("ERROR: Cannot send data, client is not connected!");
                return;
            }

            stream.Write(data.ToArray(), 0, data.DataLength);
        }
Пример #4
0
        public override void Run(int port)
        {
            if (listener != null)
            {
                return;
            }

            Running = true;

            try
            {
                listener = new TcpListener(base.GetLocalAddress(), port);
                listener.Start(1);

                Log("Waiting for client connection...");
                client = listener.AcceptTcpClient();
                stream = client.GetStream();

                Log("Got client connection!");
                UponConnected?.Invoke();

                int count;
                while (Running && client.Connected)
                {
                    count = stream.Read(inBuffer, 0, inBuffer.Length);

                    if (count == 0)
                    {
                        Log("Read message of length 0, quitting read.");
                        break;
                    }

                    NetData inData = new NetData(inBuffer, count);
                    UponMessage?.Invoke(inData);
                }
            }
            catch (Exception e)
            {
                Log(e.ToString());
            }
            finally
            {
                Log("Shutting down...");

                if (client != null)
                {
                    client.Close();
                    client = null;
                }

                if (listener != null)
                {
                    listener.Stop();
                    listener = null;
                }

                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                    stream = null;
                }

                Running = false;
            }
        }
Пример #5
0
 public abstract void Write(NetData data);