예제 #1
0
        /// <summary>
        /// start accepting data for a specific client
        /// </summary>
        private void DataAccept(Socket sock)
        {
            ClientReference client = new ClientReference(sock);

            lock (_clients)
            {
                _clients.Add(client);
            }

            try
            {
                //dispatch call into the proper thread
                _context.Post(s =>
                {
                    ClientConnected?.Invoke(client.Id);
                }, null);
                //ClientConnected?.Invoke(client.Id);
            }
            catch (Exception ex)
            {
                //dont trust user code
                Console.WriteLine("Calling server ClientConnected event handler threw an exception");
            }

            try
            {
                //connection successful, so begin receiving data sent from client
                sock.BeginReceive(client.Buffer, 0, client.Buffer.Length, SocketFlags.None, cbRxDone, client);
            }
            catch (Exception ex)
            {
                Console.WriteLine("DataAccept:BeginReceive " + ex.ToString());
            }
        }
예제 #2
0
        private ClientReference GetClientById(Guid id)
        {
            ClientReference client = null;

            lock (_clients)
            {
                client = _clients.Find(x => x.Id == id);
            }
            return(client);
        }
예제 #3
0
        /// <summary>
        /// Data showed up from the client
        /// </summary>
        private void cbRxDone(IAsyncResult ar)
        {
            ClientReference client = (ClientReference)(ar.AsyncState);

            try
            {
                client.BytesReceived        = client.sock.EndReceive(ar);
                ClientReference.TotalBytes += client.BytesReceived;
            }
            catch (Exception ex)
            {
                //set client up for removale
                Console.WriteLine("cbRxDone:EndReceive " + ex.ToString());
                client.BytesReceived = 0;
            }
            //Invoke(new delVoidClientReference(DataReceived), client);
            DataReceived(client);
        }
예제 #4
0
        /// <summary>
        /// Check if data is a complete package
        /// </summary>
        private void DataReceived(ClientReference client)
        {
            //remove client if it is required
            if (client.BytesReceived == 0)
            {
                try
                {
                    //dispatch call into the proper thread
                    _context.Post(s =>
                    {
                        ClientDisconnected?.Invoke(client.Id);
                    }, null);
                    //ClientDisconnected?.Invoke(client.Id);
                }
                catch (Exception ex)
                {
                    //dont trust user code
                    Console.WriteLine("Calling server ClientDisconnected event handler threw an exception");
                }

                lock (_clients)
                {
                    _clients.Remove(client);
                }

                return;
            }

            //LineSegment LS; //Getting ready for some Lines received
            //List<LineSegment> lsList = new List<LineSegment>();
            //Putting the memory stream pointer in the correct place
            long lPos = client.ms.Position;

            client.ms.Seek(0, SeekOrigin.End);
            client.ms.Write(client.Buffer, 0, client.BytesReceived);
            client.ms.Position = lPos;

            do //Defragmenting copy pasta
            {
                long lStartPos = client.ms.Position;
                try
                {
                    object o = _bf.Deserialize(client.ms); //deserialize into object to check for types

                    if (o is BaseDataPackage)
                    {
                        //Data received is of the correct type
                        ClientReference.TotalRecieved++;
                        client.Recieved++;
                        //calls the method registered to handle this package
                        DataHandlers.CallMethod(_context, (o as BaseDataPackage), client.Id);
                    }
                    else
                    {
                        //what the heck was this type? Someone trying to break our server!
                        Console.WriteLine("unknown shiz");
                    }
                }
                catch (SerializationException)
                {
                    //Get out of loop and wait for more data
                    Console.WriteLine("fragmentation of data");
                    client.ms.Position = lStartPos;
                    client.Fragments++; //increases the number of fragments that happened per connection

                    break;
                }
            }while (client.ms.Position < client.ms.Length);

            //client.RecieveCalls++;


            //display traffic information
            //string s;
            //if (ClientReference.TotalBytes < 1024)
            //    s = "Bytes RX'ed: B" + ClientReference.TotalBytes.ToString();
            //else
            //    if (ClientReference.TotalBytes < 1048576)
            //    s = "Bytes RX'ed: kB" + (ClientReference.TotalBytes / 1024.0).ToString("f2");
            //else
            //        if (ClientReference.TotalBytes < 1073741824)
            //    s = "Bytes RX'ed: MB" + (ClientReference.TotalBytes / 1048576.0).ToString("f2");
            //else
            //    s = "Bytes RX'ed: MB" + (ClientReference.TotalBytes / 1073741824.0).ToString("f2");

            //Text = "TolalBytes: " + s + " TotalFrames: " + CSam.TotalRecieved.ToString();

            //all data in stream has been read, reset stream
            if (client.ms.Position == client.ms.Length)
            {
                //reset the memory stream pointer and length
                client.ms.Position = 0;
                client.ms.SetLength(0);
                Console.WriteLine("Resetting RX Memory stream");
            }

            try
            {
                //Receive more stuff!
                client.sock.BeginReceive(client.Buffer, 0, client.Buffer.Length, SocketFlags.None, cbRxDone, client);
            }
            catch (Exception ex)
            {
                Console.WriteLine("DataReceived:BeginReceive at end " + ex.ToString());
            }
        }