Exemplo n.º 1
0
        private void onReceive(IAsyncResult ar)
        {
            //Get the socket
            Socket clientSocket = (Socket)ar.AsyncState;

            //Find the client info using the socket
            ClientInfo clientinfo = clientList.Find(x => x.Socket == clientSocket);

            if (clientinfo == null)
            {
                Log($"Error: 'null' client has sent data. This should be impossible.\r\n" +
                    $"The data will be ignored and the client will be removed.\r\n" +
                    $"IP->{clientSocket.RemoteEndPoint}");
                removeClient(clientSocket);
                return;
            }

            try
            {
                bool read_complete  = false;
                int  next_read_size = 1024;

                int bytesRead = clientSocket.EndReceive(ar);

                //We need to get 1024 out of the buffer
                //If we get less we store it and read for the remaining

                //Console.WriteLine($"Bytes read = {bytesRead} / {byteData.Length}");
                if (bytesRead < 1024)
                {
                    byte[] this_read = new byte[bytesRead];
                    Array.Copy(byteData, 0, this_read, 0, bytesRead);

                    clientinfo.partMessage.AddRange(this_read);

                    //Check if we have a complete message (1024 bytes)
                    if (clientinfo.partMessage.Count >= 1024)
                    {
                        if (clientinfo.partMessage.Count > 1024)
                        {
                            Log($"Warning: Read {clientinfo.partMessage.Count} bytes, expected 1024");
                        }

                        //Copy the collected bits to byteData to be processed
                        byteData = new byte[1024];
                        Array.Copy(clientinfo.partMessage.ToArray(), 0, byteData, 0, 1024);
                        clientinfo.partMessage = new List <byte>(); //Reset this
                        read_complete          = true;
                    }
                    else
                    {
                        //Not done, set the next read size to the remaining
                        next_read_size = 1024 - clientinfo.partMessage.Count;
                    }
                }
                else if (bytesRead == 1024)
                {
                    //everything is good to go
                    read_complete = true;
                }

                //Skip this until we have 1024
                if (read_complete)
                {
                    //Transform the array of bytes received from the user into an
                    //intelligent form of object Data

                    //Forward any complete messages
                    byte[] rawInternalData = new byte[0];
                    if (NetData.ReceiveData(clientinfo.User_ID, byteData, out rawInternalData))
                    {
                        try
                        {
                            InternalData d = new InternalData(rawInternalData);
                            Forward.Invoke(d, clientinfo.User_ID);
                        }
                        catch (Exception ex)
                        {
                            Log($"WARNING: Bad message data from user." +
                                $"ID->{clientinfo.User_ID}\r\n" +
                                $"IP->{clientSocket.RemoteEndPoint}.\r\n" +
                                $"Length of Data -> {rawInternalData.Length}\r\n{ex}");
                        }
                    }
                }

                byteData = new byte[next_read_size];
                clientSocket.BeginReceive(byteData, 0, next_read_size, SocketFlags.None, new AsyncCallback(onReceive), clientSocket);
            }
            catch (SocketException sx)
            {
                //Log($"SOCKET ERROR (OnReceive):\r\nLikely a bad dissconnect.\r\n{sx}");
                removeClient(clientSocket);
            }
        }
Exemplo n.º 2
0
        private void onReceive(IAsyncResult ar)
        {
            bool read_complete  = false;
            int  next_read_size = 1024;

            try
            {
                int bytesRead = clientSocket.EndReceive(ar);

                if (bytesRead < 1024)
                {
                    byte[] this_read = new byte[bytesRead];
                    Array.Copy(byteData, 0, this_read, 0, bytesRead);

                    parts.AddRange(this_read);

                    if (parts.Count >= 1024)
                    {
                        if (parts.Count > 1024)
                        {
                            LogEvent($"Warning: Read {parts.Count} bytes, expected 1024");
                        }

                        Array.Copy(parts.ToArray(), 0, byteData, 0, 1024);
                        parts         = new List <byte>();
                        read_complete = true;
                    }
                    else
                    {
                        //Not done, set the next read size to the remaining
                        next_read_size = 1024 - parts.Count;
                    }
                }
                else if (bytesRead == 1024)
                {
                    //next_read_size = 1024; //set above
                    //bytedata is fine
                    read_complete = true;
                }

                //we have the 1024 to send forward
                if (read_complete)
                {
                    byte[] internalData = new byte[0];

                    if (NetData.ReceiveData("server", byteData, out internalData))
                    {
                        InternalData ido = new InternalData(internalData);

                        ReceiveEvent(ido);
                    }
                }

                // Continue to listen
                byteData = new byte[1024];

                clientSocket.BeginReceive(byteData,
                                          0,
                                          next_read_size,
                                          SocketFlags.None,
                                          new AsyncCallback(onReceive),
                                          null); //should be socket?
            }
            catch (ObjectDisposedException)
            {
                // Chances are the object was disposed by Disconnect on another thread
                Disconnect("Socket disposed"); //Just in case
            }
            catch (Exception ex)
            {
                LogEvent($"Error: Unable to receive message (OnReceive).\r\n{ex}");
                Disconnect("Error recieving message. See log for details.");
            }
        }