コード例 #1
0
ファイル: Program.cs プロジェクト: EdgeKiller/eNetwork2
        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
ファイル: eServer.cs プロジェクト: EdgeKiller/eNetwork2
        /// <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
ファイル: eServer.cs プロジェクト: EdgeKiller/eNetwork2
 /// <summary>
 /// Start to handle client
 /// </summary>
 /// <param name="client">Client</param>
 private void StartHandleClient(eSClient client)
 {
     TaskList.Add(HandleClientAsync(client));
 }
コード例 #4
0
ファイル: eServer.cs プロジェクト: EdgeKiller/eNetwork2
        /// <summary>
        /// Listen for client
        /// </summary>
        /// <returns></returns>
        private async Task ListenAsync()
        {
            try
            {
                while (true)
                {

                    TcpClient client = await Listener.AcceptTcpClientAsync();
                    Int32 id = (Int32)randomID.Next(10000, 99999);
                    eSClient sClient = new eSClient(id, client);

                    byte[] idBuffer;

                    using (PacketWriter pw = new PacketWriter())
                    {
                        pw.WriteInt32(id);
                        idBuffer = pw.ToArray();
                    }

                    client.Send(idBuffer);

                    ClientList.Add(sClient);
                    if (OnClientConnected != null)
                        OnClientConnected(sClient);
                    StartHandleClient(sClient);
                }
            }
            catch (Exception ex)
            {
                DebugMessage("Failed to listen for new client : " + ex.Message);
            }
            finally
            {
                Stop();
            }
        }
コード例 #5
0
ファイル: eServer.cs プロジェクト: EdgeKiller/eNetwork2
        /// <summary>
        /// Send buffer to all client connected except one
        /// </summary>
        /// <param name="buffer">Buffer to send</param>
        /// <param name="exceptedClient">Excepted client</param>
        /// <returns>Success</returns>
        public bool SendToAllExcept(byte[] buffer, eSClient exceptedClient)
        {
            try
            {
                foreach (eSClient client in ClientList)
                {
                    if (client != exceptedClient)
                    {
                        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;
            }
        }
コード例 #6
0
ファイル: eServer.cs プロジェクト: EdgeKiller/eNetwork2
        /// <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;
            }
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: EdgeKiller/eNetwork2
 private static void Server_OnClientDisconnected(eSClient client)
 {
     Console.WriteLine("Client disconnected : " + client.GetID());
 }
コード例 #8
0
ファイル: Program.cs プロジェクト: EdgeKiller/eNetwork2
 private static void Server_OnClientDisconnected(eSClient client)
 {
     Log("Client disconnected with ID : " + client.GetID());
 }
コード例 #9
0
ファイル: Program.cs プロジェクト: EdgeKiller/eNetwork2
 private static void Server_OnClientConnected(eSClient client)
 {
     Log("New client connected with ID : " + client.GetID());
 }