コード例 #1
0
        public void Send(int protocol, params object[] args)
        {
            NetData writer = new NetData();

            writer.Put(protocol);
            for (int i = 0; i < args.Length; i++)
            {
                if (args[i] is int)
                {
                    writer.Put((int)args[i]);
                }
                else if (args[i] is uint)
                {
                    writer.Put((uint)args[i]);
                }
                else if (args[i] is double)
                {
                    writer.Put((double)args[i]);
                }
                else if (args[i] is string)
                {
                    writer.Put((string)args[i]);
                }
                else if (args[i] is byte)
                {
                    writer.Put((byte)args[i]);
                }
                else if (args[i] is byte[])
                {
                    writer.Put((byte[])args[i]);
                }
            }
            Send(writer);
        }
コード例 #2
0
 /// <summary>
 /// Send data to a specified client
 /// </summary>
 /// <param name="client">The client to send to</param>
 /// <param name="data">The data to send</param>
 public void SendToClient(NetClient client, NetData data)
 {
     try
     {
         _udpServer.Send(data.Data, data.Data.Length, client.EndPoint);
     }
     catch
     {
         throw new Exception("An error has occured in SendToClient");
     }
 }
コード例 #3
0
        private void receiveCallback(IAsyncResult AR)
        {
            NetClient client = null;

            try
            {
                IPEndPoint fromIP = null;
                byte[]     data   = _udpServer.EndReceive(AR, ref fromIP);

                client = GetNetClientFromEndPoint(fromIP);
                if (client == null)
                {
                    client = new NetClient(fromIP);
                }

                NetData reader = new NetData(data);
                int     cmd    = reader.GetInt();
                if (cmd == (int)Events.Connect && !_connectedClients.Contains(client))
                {
                    string key = reader.GetString();
                    if (key == _connectionKey)
                    {
                        AcceptClient(client);
                    }
                    else
                    {
                        DeclineClient(client);
                    }
                }
                else
                {
                    if (DataReceivedEvent != null)
                    {
                        DataReceivedEvent(client, new NetData(data));
                    }
                    else
                    {
                        throw new Exception("DataReceivedEvent has not been set");
                    }
                }
                _udpServer.BeginReceive(new AsyncCallback(receiveCallback), null);
            }
            catch (Exception ex)
            {
                if (client != null)
                {
                    if (ClientDisconnectedEvent != null)
                    {
                        ClientDisconnectedEvent(client);
                    }
                }
            }
        }
コード例 #4
0
 /// <summary>
 /// Send data to all connected clients
 /// </summary>
 /// <param name="data">The data to send</param>
 /// <param name="exclude">A client to exclude if needed</param>
 public void Broadcast(NetData data, NetClient exclude = null)
 {
     foreach (NetClient client in _connectedClients)
     {
         if (client != null)
         {
             if (!client.Equals(exclude))
             {
                 SendToClient(client, data);
             }
         }
     }
 }
コード例 #5
0
 private void receiveCallback(IAsyncResult AR)
 {
     try
     {
         IPEndPoint iPEndPoint = null;
         byte[]     data       = _client.EndReceive(AR, ref iPEndPoint);
         NetData    reader     = new NetData(data);
         int        cmd        = reader.GetInt();
         if (cmd == (int)Events.Connect && !_connectedToServer)
         {
             int res = reader.GetInt();
             if (res == 1)
             {
                 _connectedToServer = true;
                 if (ConnectionAcceptedEvent != null)
                 {
                     ConnectionAcceptedEvent();
                 }
                 else
                 {
                     throw new Exception("ConnectionAcceptedEvent has not been set");
                 }
             }
         }
         else if (_connectedToServer)
         {
             if (DataReceivedEvent != null)
             {
                 DataReceivedEvent(new NetData(data));
             }
             else
             {
                 throw new Exception("DataReceivedEvent has not been set");
             }
         }
         _client.BeginReceive(new AsyncCallback(receiveCallback), null);
     }
     catch (Exception ex)
     {
         throw new Exception(ex.ToString());
     }
 }
コード例 #6
0
 public void Send(NetData writer)
 {
     _client.Send(writer.Data, writer.Data.Length);
 }
コード例 #7
0
 public NetData(NetData data)
 {
     _data     = data.Data;
     _position = 0;
     _size     = _data.Length;
 }