示例#1
0
        static void server_OnDataReceived(eSClient client, byte[] buffer)
        {
            PacketReader pr = new PacketReader(buffer);
            Byte ID = pr.ReadByte();

            if(ID == 1)
            {
                string message = pr.ReadString();
                PacketWriter pw = new PacketWriter();
                pw.WriteByte(ID);
                pw.WriteInt32(server.GetIDFromTcpClient(client.GetTcpClient()));
                pw.WriteString(message);
                server.SendToAll(pw.ToArray());

                Console.WriteLine("Message received from " + client.GetID() + " : " + message);
            }
        }
示例#2
0
        /// <summary>
        /// Handle client
        /// </summary>
        /// <param name="client">Client</param>
        /// <returns></returns>
        private async Task HandleClientAsync(eSClient client)
        {
            byte[] buffer, bufferSize = new byte[2];
            int size;

            try
            {
                NetworkStream clientStream = client.GetTcpClient().GetStream();

                while (client.GetTcpClient().Client.Connected)
                {
                    int bytesRead = await clientStream.ReadAsync(bufferSize, 0, bufferSize.Length);

                    if (bytesRead == 0)
                        break;

                    using (PacketReader pr = new PacketReader(bufferSize))
                    {
                        size = pr.ReadInt16();
                    }

                    buffer = new byte[size];
                    await clientStream.ReadAsync(buffer, 0, buffer.Length);
                    if (OnDataReceived != null)
                        OnDataReceived(client, buffer);
                }
            }
            catch (Exception ex)
            {
                DebugMessage("Failed to handle client : " + ex.Message);
            }
            finally
            {
                ClientList.Remove(client);
                if (OnClientDisconnected != null)
                    OnClientDisconnected(client);
            }
        }
示例#3
0
        /// <summary>
        /// Send buffer to one client
        /// </summary>
        /// <param name="buffer">Buffer to send</param>
        /// <param name="client">Client</param>
        /// <returns>Success</returns>
        public bool SendTo(byte[] buffer, eSClient client)
        {
            try
            {
                byte[] b = eUtils.GetBuffer(buffer);
                client.GetTcpClient().Send(b);

                DebugMessage("Buffer sent successfully.");

                return true;
            }
            catch (Exception ex)
            {
                DebugMessage("Failed to send buffer : " + ex.Message);
                return false;
            }
        }