Exemplo n.º 1
0
 /// <summary>
 /// Send data through a client using the name of the client
 /// </summary>
 /// <param name="name">Client name</param>
 /// <param name="sendData">Byet buffer with the data to be sent</param>
 /// <param name="nrbytes">Number of bytes to be sent</param>
 /// <returns></returns>
 public bool SendDataAsync(string name, byte[] sendData, int nrbytes)
 {
     if (sendData.Length < nrbytes)
     {
         nrbytes = sendData.Length;
     }
     if (AllConnections.ContainsKey(name))
     {
         TConnection C = AllConnections[name];
         try
         {
             C.ConnectionSocket.BeginSend(C.IncomingData, 0, C.IncomingData.Length, 0, new AsyncCallback(SendCallback), C.Address);
             C.DataReceived = false;
             return(true);
         }
         catch
         {
             ReportError?.Invoke("BeginSend failed in SendDataAsync", "name");
             return(false);
         }
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 2
0
        private void AcceptConnectCallback(IAsyncResult ar)
        {
            Listener = (Socket)ar.AsyncState;

            //acknowledge the connection
            Socket incomingSocket = null;

            try
            {
                incomingSocket = Listener.EndAccept(ar);
            }
            catch
            {
                ReportError?.Invoke("EndAccept failed on incoming connection", "");
            }

            //put the listener back to listening
            Listener.BeginAccept(new AsyncCallback(AcceptConnectCallback), Listener);

            if (incomingSocket == null)
            {
                return;
            }
            IPEndPoint ep = (IPEndPoint)incomingSocket.RemoteEndPoint;

            TConnection C = new TConnection();

            C.Setup(incomingSocket, ep.Address, ep.Port, ConnectionBufSize);

            if (AllConnections.ContainsKey(C.Address) == false)
            {
                AllConnections.Add(C.Address, C);
                AllConnectionsList.Add(C);
            }

            //Signal that a new connection has been created
            NewConnection?.Invoke(C);

            //configure the socket to receive incoming data and arm the data reception event
            try
            {
                C.ConnectionSocket.BeginReceive(C.IncomingData, 0, C.IncomingData.Length, 0, new AsyncCallback(ReadCallback), C.Address);
            }
            catch
            {
                ReportError?.Invoke("BeginReceive failed on new connection", C.Address);
            }
        }
Exemplo n.º 3
0
        ///<inheritdoc/>
        public ConnectionGene GetConnection(NodeGene In, NodeGene Out)
        {
            ConnectionGene connection = new ConnectionGene(In, Out);

            if (AllConnections.ContainsKey(connection))
            {
                connection.InnovationNumber = AllConnections[connection].InnovationNumber;
            }
            else
            {
                connection.InnovationNumber = AllConnections.Count + 1;
                AllConnections.Add(connection, connection);
            }

            return(connection);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Disconnect a client and remove it form the client list
        /// </summary>
        /// <param name="name">Client name</param>
        public void RemoveConnection(string name)
        {
            if (!AllConnections.ContainsKey(name))
            {
                return;
            }

            TConnection Conn = AllConnections[name];

            AllConnectionsList.Remove(Conn);

            Conn.IncomingData = null;
            Conn.OutgoingData = null;

            AllConnections.Remove(name);
        }